以下是我使用Python.2-Raspberry Pi从Make Games复制的原始代码,第33页:
import pygame, sys
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
# pygame variables
pygame.init()
windowWidth = 800
windowHeight = 800
surface = 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 = False
def move():
global playerX, playerY, playerVX, playerVY, haveJumped, gravity
#move left
if leftDown:
# if we are already moving to the right,reset
#the moving speed and invert the direction
if playerVX > 0.0:
playerVX = moveSpeed
playerVX = -playerVX
# make sure our square does not leave our
#window to the left
if playerX > 0:
playerX += playerVX
#move right
if rightDown:
#if we are already moving to the left,reset
#the moving speed again
if playerVX < 0.0:
playerVX = moveSpeed
#make sure our square does not leave our
#window to the right
if playerX + playerSize < windowWidth:
playerX += playerVX
if playerVY > 1.0:
playerVY = player * 0.9
else:
playerVY = 0.0
haveJumped - False
# is our square in the air?
# better add some gravity to bring it back down
if playerY < windowHeight - playerSize:
playerY += gravity
gravity = gravity * 1.1
else:
playerY = windowHeight - playerSize
gravity = 1.0
playerY -= playerVY
if (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 redraw
for event in GAME_EVENTS.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.k_LEFT:
leftDown = True
if event.key == pygame.K_RIGHT:
rightDown = True
if event.key == pygame.K_UP:
if not haveJumped:
haveJumped = True
playerVY += jumpHeight
if event.key == pygame.K_ESCAPE:
quitGame()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
leftDown = False
playerVX = moveSpeed
if event.key == pygame.K_RIGHT:
rightDown = False
playerVX = moveSpeed
if event.type == GAME_GLOBALS.QUIT:
quitGame()
move()
pygame.display.update()
当我调试它时有很多错误指示,然后我接受了网站上很好的interpal给出的建议并添加括号来修复关于“或”的错误是无效语法并试图重写代码来修复系统:< / p>
import pygame, sys
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
# pygame variables
pygame.init()
windowWidth = 800
windowHeight = 800
surface = 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 = False
def move():
global playerX, playerY, playerVX, playerVY, haveJumped, gravity
#move left
if leftDown:
# if we are already moving to the right,reset
#the moving speed and invert the direction
if playerVX > 0.0:
playerVX = moveSpeed
playerVX = -playerVX
# make sure our square does not leave our
#window to the left
if playerX > 0:
playerX += playerVX
#move right
if rightDown:
#if we are already moving to the left,reset
#the moving speed again
if playerVX < 0.0:
playerVX = moveSpeed
#make sure our square does not leave our
#window to the right
if playerX + playerSize < windowWidth:
playerX += playerVX
if playerVY > 1.0:
playerVY = player * 0.9
else:
playerVY = 0.0
haveJumped - False
# is our square in the air?
# better add some gravity to bring it back down
if playerY < windowHeight - playerSize:
playerY += gravity
gravity = gravity * 1.1
else:
playerY = windowHeight - playerSize
gravity = 1.0
playerY -= playerVY
if ((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 redraw
for event in GAME_EVENTS.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.k_LEFT:
leftDown = True
if event.key == pygame.K_RIGHT:
rightDown = True
if event.key == pygame.K_UP:
if not haveJumped:
haveJumped = True
playerVY += jumpHeight
if (event.key == pygame.K_ESCAPE):
quitGame()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
leftDown = False
playerVX = moveSpeed
if event.key == pygame.K_RIGHT:
rightDown = False
playerVX = moveSpeed
if event.type == GAME_GLOBALS.QUIT:
quitGame()
move()
pygame.display.update()
当我最终可以运行它时,结果结果是一个白色的屏幕窗口并通知了新的错误。我是Python和编码的新手,所以我甚至无法诊断代码本身是否有问题(它来自一本书)。
第一个新错误是:
E1101:模块'pygame'没有'init'成员
列表中有13个错误。
您是否需要时间阅读代码并告诉我代码本身是否不正确?
答案 0 :(得分:3)
很难说,但看起来Python 3正在使用中,我怀疑这本书需要Python 2.如果你可以用Python 2重新运行程序,你可能会发现你的错误要少得多(如果有的话)。如果您确实遇到新错误,则可以询问有关该问题的新Stack Overflow问题。记住确切地说明问题,并包括你得到的错误信息。
值得注意的是,提出好问题很难。这不仅仅是关于英语的精确度,尽管这无疑是有帮助的。最大的问题是理解读者没有关于你可以在屏幕上看到的问题的知识和背景,因此虽然“它不工作”这个短语对程序员来说非常有意义,但对任何人来说都没有多大意义。别的(即使是有经验的程序员)。阅读more about this here。
另外需要注意的是,Stack Overflow在互联网上的免费编程帮助方面相当不寻常。有史以来第一次,想要帮助他人的工程师有一种过滤请求的方法,这些请求对于志愿者来说太多了(问题关闭)。因此,“请在您的系统上运行并为我调试它”的请求不太可能,并且无论如何,人们都非常担心在他们的计算机上运行不受信任的代码。然而,“我在Y线上遇到错误X并且我看不到原因”这一形式的问题要好得多,因为它们表明了个人的努力,并且他们没有假设(或要求)帮助者有半个小时腾出。矛盾的是,你有时会发现助手将花费很长时间来处理这些问题。