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?