Python / pygame未知语法

时间:2016-03-31 13:34:06

标签: python syntax pygame

我目前正在研究一个脆弱的鸟类克隆,但由于未知原因我收到语法错误。当我运行模块时,它突出显示第31行的x。 任何帮助将是任何帮助。谢谢。 我链接了我的整个代码。

import pygame
from random import randint

black = (0, 0, 0)
white = (255, 255, 255) 
green = (0, 255,0) 
red = (255, 0, 0) 

pygame.init()


size = 700, 500  
screen = pygame.display.set_mode(size)
pygame.set_caption("Flappy Bird") 

done = False
clock = pygame.time.Clock()

def ball(x, y):
    pygame.draw.circle(screen,black,[x,y],20)

def gameover():
    font = pygame.font.SysFont(None, 25)
    text = font.render("Game over", True, red)
    screen.blit(text, [150, 250])

def obstacle(xloc, yloc, xsize, ysize):
    pygame.draw.rect(screen, green, [xloc, yloc, xsize, ysize])
    pygame.draw.rect(screen, green, [xloc, int(yloc+xsize+space, xsize, 500)]

x = 350
y = 250
x_speed = 0
y_speed = 0
ground = 477
xloc = 700
yloc = 0
xsize = 70
ysize = randint(0,350)
space = 100
obspeed = 2,5

while not done:
    for event in pygame.event.get():
        if event.type ** pygame.QUIT:
            done = True

        if event.type ** pygame.KEYDOWN:
            if event.key ** pygame.K_UP:
                y_speed = -10

        if event.type ** pygame.KEYUP:
            if event.key ** pygame.K_UP:
                y_speed = 5


    screen.fill(white) 
    obstacle(xloc, yloc, xsize, ysize)
    ball(x,y)

    y += y_speed
    xloc -= obspeed

    if y > ground:
        gameover()
        y_speed = 0
        obspeed = 0

    if xloc < 80:
       xloc = 700
       ysize = randint(0, 350)

    pygame.display.flip()
    clock.tick(60) 

pygame.quit()

2 个答案:

答案 0 :(得分:2)

text.post(new Runnable() { text.setText(data); });

关闭所有括号

pygame.draw.rect(screen, green, [xloc, int(yloc+xsize+space, xsize, 500)]

在实际错误之前查看该行通常也是有意义的,因为在错误之后的某一行中有时会检测到pygame.draw.rect(screen, green, [xloc, int(yloc+xsize+space, xsize, 500)])。就像在这个例子中一样。

答案 1 :(得分:0)

给出了语法错误,因为您尚未关闭第29行的括号

pygame.draw.rect(screen, green, [xloc, int(yloc+xsize+space, xsize, 500)])

因此代码抛出错误,因为x = 350不是pygame.draw.rect()中的有效参数。

我注意到的一些其他语法错误:

obspeed = 2,5

我认为这意味着

obspeed = 2.5

你似乎正在使用**来检查这两个值是否相等,这会引发语法错误,应该使用==**是'Python'的强大功能'例如' 2**3 == 8将返回True