0

所以我的游戏运行良好,但精灵无法处理重复的按键。我以为我修好了它,但现在一旦我通过开始屏幕,我什么也做不了。我认为问题所在的代码在 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()

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

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

2 回答 2

0

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

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

于 2012-10-28T22:54:25.727 回答
0

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

于 2012-10-29T03:25:14.293 回答