繁体   English   中英

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

[英]My game in python/pygame is not responding when using pygame.key.get_pressed()

因此我的游戏运行正常,但该精灵无法处理重复的按键操作。 我以为我已修复它,但现在一旦通过开始屏幕,我将无法执行任何操作。 我认为问题所在的代码在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()

您会while True循环中调用get_pressed ,因此您再也不会回到主事件循环( for event in pygame.event.get():位)。 这样可以防止您的程序处理更多事件,这意味着您的程序已锁定。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM