首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在pygame中分配密钥的便捷方法

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

Stack Overflow用户
提问于 2020-09-18 16:31:49
回答 1查看 20关注 0票数 0

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

下面是我的代码片段:

代码语言:javascript
复制
    # Program: Import Library, Pygame, for initialization of this program
import pygame
import time
# Initialize the game engine
pygame.init()

# Define Colours

BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)
BLUE     = (   0,   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, 255, 0), textColor=(0, 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, True, 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, 50], "About", [380, 402])
button3 = Button([215, 50], "Settings", [380, 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,0))
        time.sleep(1)
        screen.blit(img_q1, (0,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,0))
        screen.blit(about_image, (0,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()
EN

回答 1

Stack Overflow用户

发布于 2020-09-28 02:41:53

基本上,您需要做的是使用for event in pygame.events.get (以下称为更新节)并使用key属性编写。如果你这样写:

代码语言:javascript
复制
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文档)。然而,我建议您使用函数或类来组织您的项目,因为它们使一些问题变得非常容易,便于以后使用(例如,您可以创建一个类,让它显示每个问题和答案,而不是硬编码)。

无论如何,祝你的项目好运。如果你需要更多的帮助,尽管说。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63952084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档