2012-10-28 44 views
0

所以我的游戏工作正常,但精灵不会处理重复按键。我以为我修好了它,但现在一旦我通过开始屏幕,我什么也做不了。我认为问题的代码是在playgame()下。如果有人知道这将是伟大的!我的python/pygame游戏在使用pygame.key.get_pressed()时没有响应

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

此外,我不知道这是否会有所帮助,但代码为精灵类是。

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

回答

0

您有一个while True循环中调用get_pressed,所以你永远不会回到主事件循环(与for event in pygame.event.get():位)。这可以防止您的程序处理更多事件,这意味着您的程序被锁定。

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

相关问题