使用pygame.key.get_pressed()时,我在python / pygame中的游戏没有响应

时间:2012-10-28 22:46:13

标签: python pygame keypress

所以我的游戏工作正常,但精灵不会处理重复的按键操作。我以为我修好了但是现在一旦我通过了开始画面我什么都做不了。我认为问题的代码在playgame()下。如果有人知道这将是伟大的!

import pygame 
from Classes import PlaneClass
pygame.init()
import sys,random,os
from pygame.locals import*
#import pdb

menuscreen=pygame.display.set_mode((640, 480))#FULLSCREEN
def highscores():
    WHITE=(255,255,255)
    global menuscreen
    highscoresFile= open('data/highscores.txt','r')
    filecontentstoread=''
    for line in highscoresFile:
        filecontentstoread+=str(line)+'\n'

    menuscreen.fill(WHITE)
    my_font = pygame.font.SysFont('Courier', 20)
    the_text = my_font.render(filecontentstoread, True, (0,0,0))
    menuscreen.blit(the_text, (20, 40))
    pygame.display.update()

def playgame():

    BLACK=(0,0,0)#Define the color black, later used as backgound
    plane_x=0  #Define the planes x and y coordinates
    plane_y=0


    gamescreen=pygame.display.set_mode((640, 480))#FULLSCREEN
    gamescreen.fill(BLACK) #Fill screen with black, as background
    plane_img=pygame.image.load('data/plane.png')
    plane=PlaneClass(plane_x,plane_y,plane_img)

    plane.move(plane_x,plane_y)


    gamerunning=True

    while gamerunning==True:
        #Move plane according to keyboard input
            keys=pygame.key.get_pressed()
            if keys[K_LEFT]:
                    plane_x -=1
                    plane.move(plane_x,plane_y)
            if keys[K_RIGHT]:
                    plane_x +=1
                    plane.move(plane_x,plane_y)
            if keys[K_UP]:
                    plane_y -=1
                    plane.move(plane_x,plane_y)
            if keys[K_DOWN]:
                    plane_y+=1
                    plane.move(plane_x,plane_y)



            #gamescreen.fill(BLACK)

clock=pygame.time.Clock()
clock.tick(30)
def menu_screen_options():
    menuvalue=main_menu()
    laserstartup=pygame.mixer.Sound('data/laserstartup.wav')
    laserstartup.play()
    if menuvalue==0:
        playgame()
    if menuvalue==1:
        highscores()
    if menuvalue==2:
        credits()
    if menuvalue==3:
        pygame.quit()
        sys.exit()


def main_menu():
    menuclock=pygame.time.Clock()
    clock.tick(30)


    pygame.display.set_caption('Dog Fight')
    #pygame.mouse.set_visible(False)

    WHITE=(255,255,255)
    GREEN=(0,255,0)
    BLUE=(0,0,255)
    background=pygame.image.load('data/background.png')
    lasersound=pygame.mixer.Sound('data/lasershot.wav')


    arrow=pygame.image.load('data/arrow.png')

    arrowpos = { 0 : (140,147) , 1:(140,210) , 2:(140,270) , 3 :(140,330) }



    menu=True
    counter = 0
    menuscreen.blit(arrow,arrowpos[counter])
    menuscreen.blit(background,(0,0))
    pygame.display.update()

    while menu == True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    menu = False






                if event.key == K_UP:
                    if counter > 0:
                        counter -= 1



                if event.key == K_DOWN:
                    if counter < 3:
                        counter += 1





                if event.key == K_RETURN:
                    return counter


            menuscreen.fill(WHITE)
            menuscreen.blit(background,(0,0))

            menuscreen.blit(arrow,arrowpos[counter])
        pygame.display.update()




menu_screen_options()

另外,我不知道这是否有帮助,但sprite类的代码是。

import pygame


class PlaneClass(pygame.sprite.Sprite):




    # -- Methods
    # Constructor function
    def __init__(self,x,y,sprite_image):
        self.image=sprite_image

        self.x=x
        self.y=y
        pygame.sprite.Sprite.__init__(self)

    def move(self,new_x,new_y):
        screen=pygame.display.get_surface()
        self.new_x=new_x
        self.new_y=new_y
        screen.blit(self.image,(new_x,new_y))
        pygame.display.update()

2 个答案:

答案 0 :(得分:0)

您在get_pressed循环内进行while True调用,因此您永远不会回到主事件循环(for event in pygame.event.get():位)。这可以防止程序处理更多事件,这意味着程序会锁定。

相反,您需要等待主事件循环中的键事件,并且只有在知道要处理的按键时才调用get_pressed

答案 1 :(得分:0)

注意,您正在使用keydown轮询。您可能需要按键事件。请参阅:https://stackoverflow.com/a/11918084/341744