游戏菜单中的Python问题

时间:2016-01-12 20:10:20

标签: python python-3.x pygame

到目前为止,这是完成我的第一个游戏的最后一步(除了我已经知道的背景音乐),但是,当我尝试添加菜单图像时,python会抱怨以下错误(&#39) ; Dodger.py",第84行,in     screen.blit(menu_position,menu_image) TypeError:参数1必须是pygame.Surface,而不是list')我对此感到很遗憾,这是我游戏的代码:

import pygame, random, sys
from pygame.locals import *


TEXTCOLOR = (0, 0, 0)
FPS = 60
BADDIEMINSIZE = 10
BADDIEMAXSIZE = 40
BADDIEMINSPEED = 5
BADDIEMAXSPEED = 15
ADDNEWBADDIERATE = 1
PLAYERMOVERATE = 6
WINDOWWIDTH = 1280
WINDOWHEIGHT = 768
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
menu_position = [0, 0]
menu_image = pygame.image.load("Menu.png")
background = pygame.image.load("background.png")
backgroundRect = background.get_rect
background_position = [0, 0]
gameover_position = [0, 0]
gameover_image = pygame.image.load("Gameover.png")

def terminate():
    pygame.quit()
    sys.exit()

def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    terminate()
                return

def playerHasHitBaddie(playerRect, baddies):
    for b in baddies:
        if playerRect.colliderect(b['rect']):
            return True
    return False

def drawText(text, font, surface, x, y):
     textobj = font.render(text, 1, TEXTCOLOR)
     textrect = textobj.get_rect()
     textrect.topleft = (x, y)
     surface.blit(textobj, textrect)

# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Dodger')
pygame.mouse.set_visible(False)

# set up fonts
font = pygame.font.SysFont(None, 48)

# set up sounds
gameOverSound = pygame.mixer.Sound('gameover.ogg')




# set up images
playerImage = pygame.image.load('ship.png')
playerRect = playerImage.get_rect()
baddieImage = pygame.image.load('baddie.png')
background_image = pygame.image.load("background.png") .convert()
gameover = pygame.image.load("Gameover.png") .convert()
menu = pygame.image.load("Menu.png") .convert()


# Copy image to screen
screen.blit(background_image, background_position)

# Set positions of graphics
background_position = [0, 0]
gameover_position = [0, 0]
menu_position = [0, 0]

# show the "Start" screen
screen.blit(menu_position, menu_image)
pygame.display.update()
waitForPlayerToPressKey()


topScore = 0
while True:
    # set up the start of the game
    baddies = []
    score = 0
    playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
    moveLeft = moveRight = moveUp = moveDown = False
    reverseCheat = slowCheat = False
    baddieAddCounter = 0



    while True: # the game loop runs while the game part is playing
        score += 1 # increase score



        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()



            if event.type == KEYDOWN:
                if event.key == ord('z'):
                    reverseCheat = False
                if event.key == ord('x'):
                    slowCheat = False
                if event.key == K_LEFT or event.key == ord('a'):
                    moveRight = False
                    moveLeft = True
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveLeft = False
                    moveRight = True
                if event.key == K_UP or event.key == ord('w'):
                    moveDown = False
                    moveUp = True
                if event.key == K_DOWN or event.key == ord('s'):
                    moveUp = False
                    moveDown = True

            if event.type == KEYUP:
                if event.key == ord('z'):
                    reverseCheat = False
                    score = 0
                if event.key == ord('x'):
                    slowCheat = False
                    score = 0
                if event.key == K_ESCAPE:
                        terminate()

                if event.key == K_LEFT or event.key == ord('a'):
                    moveLeft = False
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveRight = False
                if event.key == K_UP or event.key == ord('w'):
                    moveUp = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveDown = False




        # Add new baddies at the top of the screen, if needed.
        if not reverseCheat and not slowCheat:
            baddieAddCounter += 1
        if baddieAddCounter == ADDNEWBADDIERATE:
            baddieAddCounter = 0
            baddieSize = random.randint(BADDIEMINSIZE, BADDIEMAXSIZE)
            newBaddie = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-baddieSize), 0 - baddieSize, baddieSize, baddieSize),
                        'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED),
                        'surface':pygame.transform.scale(baddieImage, (baddieSize, baddieSize)),
                        }

            baddies.append(newBaddie)

        # Move the player around.
        if moveLeft and playerRect.left > 0:
            playerRect.move_ip(-1 * PLAYERMOVERATE, 0)
        if moveRight and playerRect.right < WINDOWWIDTH:
            playerRect.move_ip(PLAYERMOVERATE, 0)
        if moveUp and playerRect.top > 0:
            playerRect.move_ip(0, -1 * PLAYERMOVERATE)
        if moveDown and playerRect.bottom < WINDOWHEIGHT:
            playerRect.move_ip(0, PLAYERMOVERATE)



        # Move the baddies down.
        for b in baddies:
            if not reverseCheat and not slowCheat:
                b['rect'].move_ip(0, b['speed'])
            elif reverseCheat:
                b['rect'].move_ip(0, -5)
            elif slowCheat:
                b['rect'].move_ip(0, 1)

         # Delete baddies that have fallen past the bottom.
        for b in baddies[:]:
            if b['rect'].top > WINDOWHEIGHT:
                baddies.remove(b)

        # Draw the game world on the window.


        screen.blit(background_image, background_position)



        # Draw the score and top score.
        drawText('Score: %s' % (score), font, windowSurface, 10, 0)
        drawText('Top Score: %s' % (topScore), font, windowSurface, 10, 40)

        # Draw the player's rectangle
        windowSurface.blit(playerImage, playerRect)

        # Draw each baddie
        for b in baddies:
            windowSurface.blit(b['surface'], b['rect'])

        pygame.display.update()

        # Check if any of the baddies have hit the player.
        if playerHasHitBaddie(playerRect, baddies):
            if score > topScore:
                topScore = score # set new top score
            break

        mainClock.tick(FPS)

    # Stop the game and show the "Game Over" screen.
    gameOverSound.play()
    screen.blit(gameover_image, gameover_position)
    pygame.display.update()
    waitForPlayerToPressKey()

    gameOverSound.stop()   

有人可以帮我这个吗?我非常感激。这是我在运行游戏时遇到的唯一问题。

1 个答案:

答案 0 :(得分:0)

重现错误的代码摘录:

WINDOWWIDTH = 1280
WINDOWHEIGHT = 768
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.init()
background_image = pygame.image.load("background.png").convert()
menu_image = pygame.image.load("Menu.png").convert()

# Set positions of graphics
background_position = [0, 0]
menu_position = [0, 0]

# Copy image to screen
screen.blit(background_image, background_position)

# show the "Start" screen
screen.blit(menu_position, menu_image)


for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    pygame.display.update()

没有你的代码的其余部分的噪音,显然是

screen.blit(menu_position, menu_image)

列表[0,0]作为第一个参数,而第二个参数是Surface。

signature of Surface.blit不同。

您应该反转这两个参数来解决您的问题(如错误消息所述)。

<强>声明
我没有运行这个例子,你至少要导入所需的模块并重新检查缩进。

@MattDMo的暗示是纯金,如果你能接受的话。如果没有它我就不会厌烦回答你的问题,因为有很多代码需要阅读,而且错误是微不足道的(我做的最糟糕的错误通常是微不足道的。)