-1

注意:这是一个非常基本的游戏,我正在作为一个练习项目工作我在定义爆炸时遇到语法错误(靠近代码列表的末尾......我也是编程新手,所以是的......如果有人可以很好的帮助我被困住了,因为我是新手,所以非常感谢您的帮助

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()
4

1 回答 1

2

正如@BurhanKhalid 指出的那样,您)在第 111 行()的末尾丢失了windowSurface.blit(ghostImageTwo,ghost,这导致了您注意到的错误。

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

Python 是一种宽容的语言,但不是那么宽容。找到一个编辑器并使用它,学习如何调试你的代码,不要再马虎了。您将无法编写其他工作的代码。

于 2013-08-08T04:20:19.080 回答