Pygame随机输入代码

时间:2016-02-10 09:52:15

标签: python-2.7 pygame

我是一个绝对的Pygame新手。我想做一个小测试,它接受用户的输入并随机调整其字母的顺序,直到意外地再次捕获正确的顺序。然后它用这个词做一个小小的花哨表演,等待用户关闭窗口。我不知道怎么做程序的最后一部分(做show并等待QUIT事件)。我现在的版本是在捕获正确的字母顺序之后,将节目播放8秒然后pygame.quit()

代码如下,我正在讨论的部分在块中开始: if shuffledWord == word:

# Shuffle a word...
# -*- coding: utf-8 -*-
import random
import pygame
from pygame.locals import *
pygame.init()

word = raw_input("Typ a name...\n") #get the word to play with
wordLen = len(word) * 100 + (len(word) - 1) * 10 + 200  # in pixel
screen_size = (wordLen, 400)
screen = pygame.display.set_mode(screen_size)
font = pygame.font.SysFont(None, 200)

runs = True


while runs:
     for e in pygame.event.get():
        if e.type == QUIT:
            runs = False

    listedWord = [x for x in word] #shuffle the word and send it for rendering
    random.shuffle(listedWord)
    shuffledWord = "".join(listedWord)
    txtRightOn = font.render("-" + shuffledWord + "-", True, (random.randint(
    0, 255), random.randint(0, 255), random.randint(0, 255)))
    txtRightOff = font.render("." + shuffledWord + ".", True, (random.randint(
    0, 255), random.randint(0, 255), random.randint(0, 255)))
    txtWrong = font.render(shuffledWord, True, (random.randint(
    0, 255), random.randint(0, 255), random.randint(0, 255)))

    if shuffledWord == word: #if got the correct word again: do this little show here!
        for i in range(10):
            screen.fill((0, 0, 0))
            screen.blit(txtRightOn, (0, 100))
            pygame.display.update()
            pygame.time.wait(400)
            screen.fill((0, 0, 0))
            screen.blit(txtRightOff, (0, 100))
            pygame.display.update()
            pygame.time.wait(400)
        pygame.quit()

    else:
        pygame.time.wait(450)
        screen.fill((0, 0, 0))
        screen.blit(txtWrong, (0, 100))
        pygame.display.update()


pygame.quit()

PS:除了有任何建议,从pygamic的角度来看,这个脚本更高效,更好。非常感谢。

1 个答案:

答案 0 :(得分:1)

我使用statetime创建了工作示例,以控制应该执行哪部分代码 - 但这并不会阻止while runs循环和for event循环。< / p>

import pygame
import random

word = raw_input("Typ a name...\n") #get the word to play with
wordLen = len(word) * 100 + (len(word) - 1) * 10 + 200  # in pixel
screen_size = (wordLen, 400)

# --- init ---

pygame.init()
screen = pygame.display.set_mode(screen_size)

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

# --- mainloop ---

runs = True
state = 'shuffle'

while runs:

    current_time = pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runs = False

    if state == 'shuffle':
        listedWord = list(word)
        random.shuffle(listedWord)
        shuffledWord = "".join(listedWord)
        if shuffledWord == word:
            txtRightOn = font.render("-" + shuffledWord + "-", True, (random.randint(
            0, 255), random.randint(0, 255), random.randint(0, 255)))
            txtRightOff = font.render("." + shuffledWord + ".", True, (random.randint(
            0, 255), random.randint(0, 255), random.randint(0, 255)))

            # change state
            txt = txtRightOn
            state = 'show_right_on'
            state_time = current_time + 400
            state_repeat = 10
        else:
            # change state
            txt = font.render(shuffledWord, True, (random.randint(
        0, 255), random.randint(0, 255), random.randint(0, 255)))
            state = 'show_wrong'
            state_time = current_time + 450

    if state == 'show_right_on':
        if state_time <= current_time:
            # change state
            txt = txtRightOff
            state = 'show_right_off'
            state_time = current_time + 400

    if state == 'show_right_off':
        if state_time <= current_time:
            # change state
            txt = txtRightOn
            state = 'show_right_on'
            state_time = current_time + 400
            state_repeat -= 1
        if state_repeat == 0:
            # change state
            state = 'wait for quit'
            print('DEBUG: wait for quit')

    if state == 'show_wrong':
        if state_time <= current_time:
            # change state
            state = 'shuffle'

    screen.fill((0, 0, 0))
    screen.blit(txt, (0, 100))
    pygame.display.update()

pygame.quit()