将keypressed与char进行比较

时间:2017-12-28 09:48:54

标签: python python-3.x pygame

我想使用pygame在python中制作某种打字游戏。因此,如果按下的按键字符与单词中的字符相同,则应该返回true ...有没有办法在python中执行此操作?

例如: 单词是" cat",如果用户按下键' c',则返回true ...依此类推其余字符。

这是我的main.py文件

from time import sleep
import pygame
import random
import winsound
from words import Words

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

pygame.init()

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

done = False

clock = pygame.time.Clock()

screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

w1 = Words(screen) #making a single word (for now) to see if typing works

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True


    screen.fill(WHITE)
    w1.draw()

    #attempting to write code here to compare word and user input



    pygame.display.flip()
    clock.tick(60)


pygame.init()
exit()

这是我的words.py文件

from random_words import RandomWords
import pygame
import random
from queue import *

rw = RandomWords()

class Words():
    def __init__(self, screen):
        self.screen = screen
        self.x_point = 400
        self.y_point = 400
        self.word = rw.random_word() #generates a random word
        self.queue = Queue() #was hoping to use the queue so that if the user types the char correctly in the right order, then the letter would change color or something (but that's further down the line)
        for c in self.word: #iterate through randomized word.. 
            self.queue.put(c) #add each char in randomized word to queue, for typing reasons

    def getY(self):
        return self.y_point

    def draw(self):
        #creates a new object
        myfont = pygame.font.SysFont('Comic Sans MS' ,30)
        #creates a new surface with text drawn on it
        textsurface = myfont.render(self.word, False, (0,0,0))

        self.screen.blit(textsurface,(self.x_point,self.y_point))

1 个答案:

答案 0 :(得分:1)

活动KEYDOWNevent.unicodeevent.keyevent.mod

你可以比较

if event.type == pygame.KEYDOWN:
    if event.unicode == "a":

甚至

if event.type == pygame.KEYDOWN:
    if event.unicode.lower() == "a":

检查"a""A"

检查单词中的字符

if event.type == pygame.KEYDOWN:  
    if event.unicode.lower() in your_word.lower():

示例代码使用event.unicode使用按下的键呈现文本。

顺便说一句:它不是某些Entry小部件,因此当您按backspace时,它不会删除字符。

import pygame

# --- constants ---

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

SCREEN_WIDTH = 300
SCREEN_HEIGHT = 200

FPS = 5  # `FPS = 25` is enough for human eye to see animation.
         # If your program don't use animation
         # then `FPS = 5` or even `FPS = 1` can be enough

# --- main ---

# - init -

pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
screen_rect = screen.get_rect()

# - objects -

font = pygame.font.SysFont(None, 30)

text = ""

text_image = font.render(text, True, GREEN)
text_rect = text_image.get_rect()     # get current size
text_rect.center = screen_rect.center # center on screen

# - mainloop -

clock = pygame.time.Clock()

done = False

while not done:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            text += event.unicode

            text_image = font.render(text, True, GREEN)
            text_rect = text_image.get_rect()     # get current size
            text_rect.center = screen_rect.center # center on screen

    # - draws -

    screen.fill(BLACK)

    screen.blit(text_image, text_rect)

    pygame.display.flip()
    clock.tick(FPS)

# - end -
pygame.quit() # <-- quit(), not init()

enter image description here