在python上使用def时,我总是遇到语法错误

在python上使用def时,我总是遇到语法错误,python,function,Python,Function,注意:这是一个非常基本的游戏,我正在做一个实践项目,我在定义爆炸时遇到语法错误(在代码列表的末尾…我对编程也很陌生,所以是的…如果有人能帮我,那就太好了,因为我是新来的,所以非常感谢你的帮助 import pygame, aya, random from pygame.locals import * from threading import Timer #set up pygame pygame.init() mainClock = pygame.time.Clock() #set up

注意:这是一个非常基本的游戏,我正在做一个实践项目,我在定义爆炸时遇到语法错误(在代码列表的末尾…我对编程也很陌生,所以是的…如果有人能帮我,那就太好了,因为我是新来的,所以非常感谢你的帮助

import pygame, aya, random
from pygame.locals import *
from threading import Timer

#set up pygame
pygame.init()
mainClock = pygame.time.Clock()

#set up the window
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WindowSurface = pygame.display.set_mode ( (WINDOW_WIDTH,
WINDOW_HEIGHT),0)
pygame.display.set_caption("Get Home!!")

#set up color constants
BLACK = (0,0,0)
BLUE = (0, 0, 255)

#set winning text
textFont = pygame.font.sysFont ("impact", 60)
text = textFont.render ("Welcome Home!", True, (193, 0, 0))

#set up the player and breadcrumbs
mapCounter = 0
NEW_GHOST = 20
GHOST_SIZE = 64
playerImage = pygame.image.load("playerimage.jpg")
playerImageTwo = pygame.image.load("playerimage.jpg")
ghostImage = pygame.image.load("ghost image.jpg")
ghostImageTwo = pygame.image.load("ghost image2.jpg")

player = pygame.Rect (300, 100,40, 40)
ghost = []
for i in range(20):
    ghost.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - GHOST_SIZE),
                             random.randint(0, WINDOW_HEIGHT - GHOST_SIZE),
                             GHOST_SIZE, GHOST_SIZE))
#movement variables
moveLeft = False
moveRight = False
moveDown = False

MOVE_SPEED = 6

#run the game loop
startGame = True
while startgame == True:
    #check for quit
    for event in pygame.event.get () :
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            #keyboard variables
             if event.key ++ K_LEFT:
                 moveRight = False
                 moveLeft = True
             if event.key ++ K_RIGHT:
                 moveRight = False
                 moveLeft = False
             if event.key ++ K_UP:
                 moveUp = True
                 moveDown = False

             if event.key ++ K_DOWN:
                 moveUp = False
                 moveDown = True
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                ays.exit()
            if event.key == K_LEFT:
                moveLeft = False
            if event.key == K_RIGHT:
                moveRight = False
            if event.key == K_UP:
                moveUP = False
            if event.key == K_DOWN:
                moveDown = False

    ghostCounter += 1
    if ghostcounter >= NEW_GHOST:
        #clear ghost array and add new ghost
        ghostCounter = 0
        ghost.append(pygame.Rect(random.randint(0, WINDOW_WIDTH - GHOST_SIZE),
                                 random.randint(0, WINDOW_HEIGHT - GHOST_SIZE),
                                 GHOST_SIZE, GHOST_SIZE))
    #draw black background
        windowSurface.fill(BLACK)

        #move player
        if moveDown and play.bottom < WINDOW_HEIGHT:
            player.top += MOVE_SPEED
        if moveUp and play.top > 0:
            player.top -= MOVE_SPEED                         
        if moveleft and play.left > 0:
            player.left -= MOVE_SPEED                         
        if moveRight and play.right < WINDOW_HEIGHT:
            player.right += MOVE_SPEED


        windowSurface.blit(playerImage, player)
        for ghost in ghosts:
            windowSurface.blit(ghostImage, ghost)

        #check if player has intersected with ghost
        for ghost in ghosts[:]:

            if player.colliderect(ghost):
                windowSurface.blit(ghostImageTwo,ghost


                def explosion():
                     for ghost in ghosts:
                         if player.colliderect(ghost) and (moveLeft == False and
                              moveRight == False and moveUp == False and
                             moveDown == False):
                                 ghosts.remove(ghost)
                if player.colliderect(ghost) and (moveLeft == false and
            moveRight == False and moveUp == False and moveDown == False): 
                     t = Timer(3, explosion)
                     t.start()

            if len(ghost == 0:
                ghostCounter = 0
                windowSurface.blit(text, (90, 104))
                startgame = False

            #draw the window
            pygame.display.update()
            mainClock.tick(40)

        while startgame == False
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
导入pygame、aya、random
从pygame.locals导入*
从线程导入计时器
#设置pygame
pygame.init()
mainClock=pygame.time.Clock()
#开窗
窗宽=400
窗户高度=400
WindowSurface=pygame.display.set_模式((窗口宽度,
窗高(0)
pygame.display.set_标题(“回家!!”)
#设置颜色常量
黑色=(0,0,0)
蓝色=(0,0255)
#设置获胜文本
textFont=pygame.font.sysFont(“影响”,60)
text=textFont.render(“欢迎回家!”,True,(193,0,0))
#设置播放器和面包屑
映射计数器=0
NEW_GHOST=20
重影大小=64
playerImage=pygame.image.load(“playerImage.jpg”)
playerImageTwo=pygame.image.load(“playerimage.jpg”)
ghostImage=pygame.image.load(“ghost-image.jpg”)
ghostmagetwo=pygame.image.load(“ghostmage2.jpg”)
player=pygame.Rect(300100,40,40)
鬼=[]
对于范围(20)内的i:
append(pygame.Rect(random.randint(0,窗口宽度-重影大小),
random.randint(0,窗口高度-重影大小),
重影大小,重影大小)
#运动变量
moveLeft=False
moveRight=False
向下移动=错误
移动速度=6
#运行游戏循环
startGame=True
当startgame==True时:
#检查是否退出
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
#键盘变量
如果event.key++K_离开:
moveRight=False
moveLeft=True
如果event.key++K_正确:
moveRight=False
moveLeft=False
如果event.key++K_启动:
向上移动=真
向下移动=错误
如果event.key++K_关闭:
moveUp=False
向下移动=真
如果event.type==KEYUP:
如果event.key==K_转义:
pygame.quit()
2.exit()
如果event.key==K_LEFT:
moveLeft=False
如果event.key==K_RIGHT:
moveRight=False
如果event.key==K_UP:
moveUP=False
如果event.key==K_DOWN:
向下移动=错误
幽灵计数器+=1
如果重影计数器>=新重影:
#清除重影阵列并添加新重影
重影计数器=0
append(pygame.Rect(random.randint(0,窗口宽度-重影大小),
random.randint(0,窗口高度-重影大小),
重影大小,重影大小)
#画黑色背景
窗口表面填充(黑色)
#移动播放器
如果向下移动并播放.bottom<窗口高度:
player.top+=移动速度
如果moveUp和play.top>0:
player.top-=移动速度
如果moveleft和play.left>0:
player.left-=移动速度
如果向右移动并播放.right<窗口高度:
player.right+=移动速度
windowSurface.blit(玩家图像,玩家)
对于鬼魂中的鬼魂:
windowSurface.blit(重影图像、重影)
#检查玩家是否与鬼魂相交
对于鬼魂中的鬼魂[:]:
如果player.collide rect(重影):
窗口表面。blit(鬼影图像二,鬼影
def爆炸()
对于鬼魂中的鬼魂:
如果player.collide rect(ghost)和(moveLeft==False和
moveRight==False和moveUp==False和
moveDown==False):
幽灵。移除(幽灵)
如果player.collide rect(ghost)和(moveLeft==false和
moveRight==False和moveUp==False和moveDown==False):
t=计时器(3,爆炸)
t、 开始()
如果len(ghost==0:
重影计数器=0
windowSurface.blit(文本,(90104))
startgame=False
#画窗
pygame.display.update()
主时钟滴答(40)
而startName==False
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()

正如@BurhanKhalid所指出的,您在第111行(
windowSurface.blit(ghostmagetwo,ghost
)的末尾缺少了
,这导致了您注意到的错误

此外,您还有大量的语法错误。您在不同的情况下定义变量,而不是使用它们(
startGame
用作
startGame
,您忘记关闭其他几个
s(第124行等)。列表继续


Python是一种宽容的语言,但并非如此宽容。找到一个编辑器并使用它,学习如何调试代码,并停止草率。否则,您将无法编写有效的代码。

错误发生在哪里?所有这些代码真的相关吗?您缺少一个
此处
windowSurface.blit(GoSimTimeToWo,Ghost 。考虑将您的文本编辑器升级为与Python兼容的东西,这样它将在您的代码中突出这些错误。您也丢失了<代码>)<代码> >如果LeN(Ghost=0:和<<代码>:<代码> > <代码> StordPask==false < /Cord>好的眼睛。我真的在上午制造了一个可怜的蟒蛇皮条。