“Python代码本身有什么问题吗?”

2024-04-29 13:29:13 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我从Make Games with Python.2-Raspberry Pi复制的原始代码,第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()    

调试的时候出现了很多错误提示,然后我在网站上听取了nice interpal的建议,加了括号来修正“or”是无效语法的错误,并试图重写代码来修复系统:

^{pr2}$

当我终于可以运行它时,结果是一个白屏窗口,并通知了新的错误。我是Python和编码的新手,所以我甚至不能诊断代码本身是否有问题(它来自一本书)。在

第一个新错误是:

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

名单上有13个错误。在

你能花点时间读一下代码并告诉我代码本身是否不正确吗?在


Tags: thetokeyeventfalseifourpygame
1条回答
网友
1楼 · 发布于 2024-04-29 13:29:13

潜在答案

这很难说,但看起来Python3正在使用中,我怀疑这本书需要Python2。如果可以用python2重新运行程序,您可能会发现错误(如果有的话)要少得多。如果您确实有新的错误,那么您可以提出一个新的堆栈溢出问题。记住对问题要精确,并包括您得到的错误消息。在

元建议

值得注意的是,提出好问题是相当困难的。这不仅仅是关于英语的精确性,尽管这无疑有帮助。最大的问题是要理解读者对你在屏幕上看到的问题一无所知,所以“它不工作”这句话对程序员来说是非常有意义的,但对其他人(甚至是有经验的程序员)来说意义不大。读more about this here。在

另一件要注意的事情是,堆栈溢出在因特网上的免费编程帮助资源方面是相当不寻常的。第一次,那些想帮助他人的工程师们有了一种过滤那些要求志愿者太多的请求的方法(问题结束)。因此,“请在您的系统上运行此程序并为我调试它”的请求不太可能得到满足,而且在任何情况下,人们都非常警惕在他们的计算机上运行不可信的代码。在

然而,“我在Y行得到了错误X,我看不出原因”这样的问题要好得多,因为它们表明了个人的努力,而且他们不会假设(或要求)助手有半小时的空闲时间。矛盾的是,有时你会发现帮助者会花很长时间在这类问题上。在

相关问题 更多 >