Is there anything wrong with the Python code itself? [closed]

2024/7/7 6:29:34

Here is the original code I copied from Make Games with Python.2-Raspberry Pi, page 33:

import pygame, sys
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS# pygame variables
pygame.init()windowWidth = 800
windowHeight = 800surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Pygame Keyboard!')# square variables
playerSize = 20
playerX = (windowWidth / 2) - (playSize / 2)
playerY = windowHeight - playerSize
playerVX = 1.0
playerVY = 0.0
jumpHeight = 25.0
moveSpeed = 1.0
maxSpeed = 10.0
gravity = 1.0#keybpard variables
leftDown = False
rightDown = False
haveJumped = Falsedef move():global playerX, playerY, playerVX, playerVY, haveJumped, gravity#move leftif leftDown:# if we are already moving to the right,reset#the moving speed and invert the directionif playerVX > 0.0:playerVX = moveSpeedplayerVX = -playerVX# make sure our square does not leave our#window to the leftif playerX > 0:playerX += playerVX#move rightif rightDown:#if we are already moving to the left,reset#the moving speed againif playerVX < 0.0:playerVX = moveSpeed#make sure our square does not leave our#window to the rightif playerX + playerSize < windowWidth:playerX += playerVXif playerVY > 1.0:playerVY = player * 0.9else:playerVY = 0.0haveJumped - False# is our square in the air?# better add some gravity to bring it back downif playerY < windowHeight - playerSize:playerY += gravitygravity = gravity * 1.1else:playerY = windowHeight - playerSizegravity = 1.0playerY -= playerVYif (playerVX > 0.0 and playerVX < maxSpeed) or(playerVX < 0.0 and playerVX > -maxSpeed):if not haveJumped and (leftDown or rightDown)playerVX = playerVX * 1.1# how to quit our program
def quitGame():pygame.quit()sys.exit()while True:surface.fill((0,0,0))pygame.draw.rect(surface, (255,0,0),(playerX, playerY, playerSize, playerSize))#get a list of all enets that happened since#the last redrawfor event in GAME_EVENTS.get():if event.type == pygame.KEYDOWN:if event.key == pygame.k_LEFT:leftDown = Trueif event.key == pygame.K_RIGHT:rightDown = Trueif event.key == pygame.K_UP:if not haveJumped:haveJumped = TrueplayerVY += jumpHeightif event.key == pygame.K_ESCAPE:quitGame()if event.type == pygame.KEYUP:if event.key == pygame.K_LEFT:leftDown = FalseplayerVX = moveSpeedif event.key == pygame.K_RIGHT:rightDown = FalseplayerVX = moveSpeedif event.type == GAME_GLOBALS.QUIT:quitGame()move()
pygame.display.update()    

and there were many error indications when I debugged it, then I took advice given by nice interpal on website and added parentheses to fix the the error about "or" is invalid syntax and tried to rewrote code to fix the system:

import pygame, sys
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS# pygame variables
pygame.init()windowWidth = 800
windowHeight = 800surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Pygame Keyboard!')# square variables
playerSize = 20
playerX = (windowWidth / 2) - (playSize / 2)
playerY = windowHeight - playerSize
playerVX = 1.0
playerVY = 0.0
jumpHeight = 25.0
moveSpeed = 1.0
maxSpeed = 10.0
gravity = 1.0#keybpard variables
leftDown = False
rightDown = False
haveJumped = Falsedef move():global playerX, playerY, playerVX, playerVY, haveJumped, gravity#move leftif leftDown:# if we are already moving to the right,reset#the moving speed and invert the directionif playerVX > 0.0:playerVX = moveSpeedplayerVX = -playerVX# make sure our square does not leave our#window to the leftif playerX > 0:playerX += playerVX#move rightif rightDown:#if we are already moving to the left,reset#the moving speed againif playerVX < 0.0:playerVX = moveSpeed#make sure our square does not leave our#window to the rightif playerX + playerSize < windowWidth:playerX += playerVXif playerVY > 1.0:playerVY = player * 0.9else:playerVY = 0.0haveJumped - False# is our square in the air?# better add some gravity to bring it back downif playerY < windowHeight - playerSize:playerY += gravitygravity = gravity * 1.1else:playerY = windowHeight - playerSizegravity = 1.0playerY -= playerVYif ((playerVX > 0.0 and playerVX < maxSpeed) or(playerVX < 0.0 and playerVX > -maxSpeed)):if not haveJumped and (leftDown or rightDown):playerVX = playerVX * 1.1# how to quit our program
def quitGame():pygame.quit()sys.exit()while True:surface.fill((0,0,0))pygame.draw.rect(surface, (255,0,0),(playerX, playerY, playerSize, playerSize))#get a list of all enets that happened since#the last redrawfor event in GAME_EVENTS.get():if  event.type == pygame.KEYDOWN:if  event.key == pygame.k_LEFT:leftDown = Trueif  event.key == pygame.K_RIGHT:rightDown = Trueif  event.key == pygame.K_UP:if not haveJumped:haveJumped = TrueplayerVY += jumpHeightif (event.key == pygame.K_ESCAPE):quitGame()if event.type == pygame.KEYUP:if event.key == pygame.K_LEFT:leftDown = FalseplayerVX = moveSpeedif event.key == pygame.K_RIGHT:rightDown = FalseplayerVX = moveSpeedif event.type == GAME_GLOBALS.QUIT:quitGame()move()
pygame.display.update()

When I finally could run it, the result turned out to be a white screen window and informed new errors. I am a newbie of Python and coding, so I cannot even diagnose whether the code itself has problem (it is from a book).

The first new error is:

E1101:Module 'pygame' has no 'init' member

And there are 13 errors on the list.

Would you take time to read the code and tell me whether the code itself is incorrect?

Answer

It is hard to tell, but it looks like Python 3 is in use, and I suspect the book is requiring Python 2. If you can re-run the program with Python 2, you may find you have far fewer errors (if any). If you do have a new error, you can then ask a new Stack Overflow question about that. Remember to be precise about the problem, and to include the error message(s) you get.

https://en.xdnf.cn/q/120699.html

Related Q&A

Python Iterate Over String

So Ive got a string e.g "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN".I want to be able to loop over 16 characters starting and print it. Then move up 1 letter, loop over 16 characters a…

what does with open do in this situation [duplicate]

This question already has answers here:What is the Python "with" statement used for?(3 answers)Closed 7 years ago.sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN D…

How to perform HTTP GET operation in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python Error TypeError: cannot concatenate str and float objects [duplicate]

This question already has answers here:Making a string out of a string and an integer in Python [duplicate](5 answers)Closed 7 years ago.I am new with Python programming. I keep getting the below error…

Countif function in python

enter image description here In excel file i can do countif funtion like attached picture but How can i do this countif function in Python Pandas,please help me by providing the code

How do i implement these algorithms below

Alogrithm 1:Get a list of numbers L1, L2, L3....LN as argumentAssume L1 is the largest, Largest = L1Take next number Li from the list and do the followingIf Largest is less than LiLargest = LiIf Li is …

How to run a shell script once a day?

I am trying to run this particular shell script only one time, daily. Heres my code for runLucene.py:#!/usr/bin/env pythonimport os from extras.download_datos_desambiguar import news_Lucenex=datetime.t…

How to fix Error: Please select a valid Python interpreter in Pycharm?

Error:Error: Please select a valid Python interpreterScreenshot:How to fix this?

Tuples conversion into JSON with python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Python continue with while [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…