Python 代码本身有什么问题吗? [英] Is there anything wrong with the Python code itself?

查看:60
本文介绍了Python 代码本身有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我从 Make Games with Python.2-Raspberry Pi,第 33 页复制的原始代码:

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 = 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"语法无效的错误,并尝试重新编写代码来修复系统:

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 = 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 和编码的新手,所以我什至无法诊断代码本身是否有问题(来自书中).

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).

第一个新错误是:

E1101:Module 'pygame' 没有 'init' 成员

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

列表中有 13 个错误.

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?

推荐答案

很难说,但看起来 Python 3 正在使用中,我怀疑这本书需要 Python 2.如果您可以重新运行使用 Python 2 编写程序,您可能会发现错误少得多(如果有的话).如果您确实遇到了新错误,则可以就此提出新的 Stack Overflow 问题.请记住要准确说明问题,并包括您收到的错误消息.

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.

这篇关于Python 代码本身有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆