pygame相关代码中的2个函数不起作用。 (可能是语法)

时间:2014-03-27 17:04:25

标签: python python-3.x pygame

我写了一个代码来制作一个游戏,玩家向上和向下移动,并且必须躲避火焰。但是waitforkey()和drawtext()函数根本不起作用。 因此,一旦游戏结束,代码就会出错。

这是代码:

    #Import modules

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


    #intialising variables for ease

    window_height=600 
    window_width=600
    blue = (0,0,255)
    black = (0,0,0)

    fps=30
    flamesize=20
    speed = 5
    addnewflamerate =20

    #defining the required function

    def terminate():        #to end the program
        pygame.quit()
        sys.exit()

    def waitforkey():       #to wait for user to start
            for event in pygame.event.get():
                if event.type == "QUIT":
                    terminate()
                if event.type == "KEYDOWN":     #to terminate if the user presses the escape key
                    if event.key == "K_ESCAPE":
                        terminate()
                    return



    def flamehitsmario(playerrect, flames):      #to check if flame has hit mario or not
        for f in flames:
            if playerrect.colliderect(f['rect']):
                return True
            return False

    def drawtext(text, WindowSurface, font, x, y):        #to display text on the screen
        textobj = font.render(text, 1, blue)
        textrect = textobj.get_rect()
        textrect.topleft = (x,y)
        WindowSurface.blit(textobj, textrect)


    #end of functions, begin to start the main code

    pygame.init()
    mainClock = pygame.time.Clock()
    WindowSurface = pygame.display.set_mode((window_width,window_height))
    pygame.display.set_caption('MARIO')

    #setting up font and sounds and images

    font = pygame.font.SysFont(None, 48)

    playerimage = pygame.image.load('player.png')
    playerrect = playerimage.get_rect()
    flameimage = pygame.image.load('flame.png')

    #getting to the start screen

    #waitforkey() : if i remove the '#', the game hangs.

    drawtext("Mario", WindowSurface, font, (window_width/3), (window_height/3))
    drawtext("Press any key to start", WindowSurface, font, (window_width/3-50), (window_height/3+50))

    pygame.display.update()
    #start for the main code

    topscore = 0
    while True:

        score = 0
        flames = []
        playerrect.topleft = (50,window_height/2)
        moveup = movedown = False
        flameaddcounter = 0

        while True:     #the main game loop
            score+=1
            for event in pygame.event.get():
                if event.type == QUIT:
                    terminate()

                if event.type == KEYDOWN:
                    if event.key == K_UP:
                        movedown = False
                        moveup = True
                    if event.key == K_DOWN:
                        movedown = True
                        moveup = False

                if event.type == KEYUP:
                    if event.key == K_UP:
                        moveup = False
                    if event.key == K_DOWN:
                        movedown = False
                    if event.key == K_ESCAPE:
                        terminate()

            flameaddcounter += 1


            if flameaddcounter == addnewflamerate:

                flameaddcounter = 0

                newflame = {'rect' : pygame.Rect(window_width - flamesize,random.randint(0, window_height - flamesize), flamesize, flamesize),
                            'speed' : speed,
                            'Surface' : pygame.transform.scale(flameimage, (flamesize,flamesize))
                            }
                flames.append(newflame)

            if (moveup and (playerrect.top > 0)):
                playerrect.top -= speed
            if (movedown and (playerrect.bottom < window_height)):
                playerrect.bottom += speed

            for f in flames:
                f['rect'].left -= speed

            for f in flames:
                if f['rect'].left <= 0:
                    flames.remove(f)

            WindowSurface.fill(black)
            WindowSurface.blit(playerimage, playerrect)

            for f in flames:
                WindowSurface.blit(f['Surface'], f['rect'])

            pygame.display.update()

            if flamehitsmario(playerrect, flames):
                if score > topscore:
                    topscore = score
                drawtext('GAME OVER', font, WindowSurface, (window_width / 3), (window_height / 3))
                drawtext('Press a key to play again.', font, WindowSurface, (window_width / 3) - 80, (window_height / 3) + 50)
                pygame.display.update()
                waitforkey()

            mainClock.tick(fps)

所需图片:

http://inventwithpython.com/player.png http://haryanacitygas.com/wp-content/uploads/2010/12/flame.png

感谢:D。

2 个答案:

答案 0 :(得分:0)

if event.type == "QUIT":

您看到event.type是否等于字符串“QUIT”。它不会。任何其他事件也不会等同于字符串。

pygame定义了一些你应该测试的常量(int s)。

if event.type == pygame.QUIT:
    #etc

同样适用于其他event.type次检查。

答案 1 :(得分:0)

我可以看到你切换传递给函数的参数的顺序 drawtext。在你的游戏循环中,你用这些参数调用函数

drawtext('GAME OVER', font, WindowSurface, (window_width / 3), (window_height / 3))

但你定义了这样的函数:

def drawtext(text, WindowSurface, font, x, y):

正如您所看到的那样,在WindowSurface参数的位置传递了font参数,因此您尝试基本上从表面而不是字体渲染:)。 只需修改将参数传递给函数的顺序,它就可以顺利运行:)