微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在pygame中分配密钥的便捷方法

如何解决在pygame中分配密钥的便捷方法

所以,我一直在构建这个琐事游戏,我想将键盘上的字母键分配给pygame。简单来说,我希望用户在选择答案时按a,b或c,然后pygame接听并告诉他们是对还是错。请让我知道,因为我是基于pygame平台构建游戏的初学者。

这是我的代码段:

    # Program: Import Library,Pygame,for initialization of this program
import pygame
import time
# Initialize the game engine
pygame.init()

# Define Colours

BLACK    = (   0,0)
WHITE    = ( 255,255,255)
GREEN    = (   0,0)
RED      = ( 255,0)
BLUE     = (   0,255)

display_width = 1080
display_height = 720
size = (display_width,display_height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("MiniConomy Trivia By Devang SAHANI")

# Button Program

class Button:
    def __init__(self,size,text,pos,bgColor=(0,0),textColor=(0,0)):
        self.pos  = pos
        self.size = size
        self.text = text
        self.font = pygame.font.Font(pygame.font.get_default_font(),size[1])
        self.textSurf = self.font.render(f"{text}",True,textColor)
        self.button = pygame.Surface((size[0],size[1])).convert()
        self.button.fill(bgColor)

    def render(self,window):
        window.blit(self.button,(self.pos[0],self.pos[1]))
        window.blit(self.textSurf,(self.pos[0]+1,self.pos[1]+5))

    def clicked(self,events):
        mousePos = pygame.mouse.get_pos()#  get the mouse position
        for event in events:
            if self.button.get_rect(topleft=self.pos).collidepoint(mousePos[0],mousePos[1]):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    return True
        return False

# Setting a Title Screen
def text_objects(text,font):
    textSurface = font.render(text,BLACK)
    return textSurface,textSurface.get_rect()
largeText = pygame.font.Font('freesansbold.ttf',90)

# Setting background sound
def background_sound():
    pygame.mixer.sound.play(start_sound)
 
# Creating a Title Screen
TextSurf,TextRect = text_objects("MiniConomy",largeText)
TextRect.center = (540,150)

# Button Control

button = Button([280,50],"Let's Begin",[380,302])
button2 = Button([190,"About",402])
button3 = Button([215,"Settings",502])

#Loop until the user clicks the close button
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# Menu Settings

background_image = pygame.image.load("Miniconomy.PNG").convert()
about_image = pygame.image.load("abouthtp.PNG").convert()
start_sound = pygame.mixer.sound("start.ogg")

# Question Image Sources

img_q1 = pygame.image.load("question_1.PNG").convert()
img_q2 = pygame.image.load("question_2.PNG").convert()
img_q3 = pygame.image.load("question_3.PNG").convert()
img_q4 = pygame.image.load("question_4.PNG").convert()
img_q5 = pygame.image.load("question_5.PNG").convert()
img_q6 = pygame.image.load("question_6.PNG").convert()
img_c = pygame.image.load("correct.PNG").convert()
img_ic = pygame.image.load("incorrect.PNG").convert()
img_exi = pygame.image.load("exitscreen.PNG").convert()

# -------- Main Program Loop -----------
screen.blit(background_image,(0,0))
keep_buttons = True
while not done:
    events = pygame.event.get()
    for event in events: # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop

    # --- Game logic should go here
    # --- Drawing code should go here
    
 
    # Set the screen background
    if keep_buttons:
        screen.blit(TextSurf,TextRect)
    
    # Button 1 Control
    if keep_buttons:
        button.render(screen)
    if button.clicked(events):
        pygame.mixer.music.stop()
        screen.fill((0,0))
        time.sleep(1)
        screen.blit(img_q1,0)) #This is where I want the answer options to be
        keep_buttons = False

    if keep_buttons:
        button2.render(screen)
    if button2.clicked(events):
        pygame.mixer.music.stop()
        screen.fill((0,0))
        screen.blit(about_image,0))
        keep_buttons = False

    if keep_buttons:
        button3.render(screen)
    if button3.clicked(events):
        pygame.mixer.music.stop()
        print("Game logic goes here")
        pass

    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    # --- Limit to 60 frames per second
    clock.tick(60)

pygame.quit()
quit()

解决方法

基本上,您需要做的是使用for event in pygame.events.get(此后称为更新部分),并使用key属性进行写操作。如果您写:

if event.type == pygame.KEYUP: # checking if the event has a key that was pressed - it's key up 
# since you don't want someone to accidentally skip through questions through long-pressing it

    if event.key == pygame.K_a: # checking if the key is 'a'

        # code to do if the 'a' key is pressed

使用它,您可以使每个键都可以执行任何操作(您随时可以查看PyGame文档)。不过,我还是建议您使用函数或类来组织项目,因为它们会使某些内容确实更容易供以后使用(例如,您可以制作一个问题类并让其显示每个问题,为每个答案而不是对其进行硬编码。)

无论如何,祝您项目顺利。如果您需要更多帮助,请这样说。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。