通过同时按两个键使用pygame暂停游戏 - tamagotchi项目

时间:2018-03-17 22:33:45

标签: python python-2.7 events pygame keyboard-events

我正在尝试在我的tamagotchi克隆中实现暂停功能(我正在为明年的受控评估练习)并且我无法让左右键同时作为暂停按钮工作。如果可能的话,我希望尽可能保持原始游戏的真实性,所以我宁愿不使用屏幕按钮。谢谢!

    import pygame
    import time

    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)

    size =(200, 200)
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('Tama v4.5')
    screen.fill(WHITE)
    pygame.init()

    clock = pygame.time.Clock()

    sprites = ['AdultSpriteAAA.png']

    up_pressed = False
    left_pressed = False
    right_pressed = False

    def main():
        controls()
        if up_pressed == True and left_pressed == True:
            time.sleep(2)
            pause()
        player_position = pygame.mouse.get_pos()
        x = player_position[0]
        y = player_position[1]
        screen.blit(background, [0,0])
        screen.blit(sprite, [x, y])

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

    def controls():
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

            elif event.type == pygame.MOUSEBUTTONDOWN:
                print(animate())
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    global up_pressed
                    right_pressed = True
                if event.key == pygame.K_LEFT:
                    global left_pressed
                    left_pressed = True
                if event.key == pygame.K_RIGHT:
                    global right_pressed
                    right_pressed = True

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    global up_pressed
                    right_pressed = False
                if event.key == pygame.K_LEFT:
                    global left_pressed
                    left_pressed = False
                if event.key == pygame.K_RIGHT:
                    global right_pressed
                    right_pressed = False

    def info():
        return 0

    def food():
        return 1

    def toilet():
        return 2

    def game():
        return 3

    def connect():
        return 4

    def talk():
        return 5

    def medic():
        return 6

    def post():
        return 7

    def history():
        return 8

    def animate():
        return 9

    def pause():
        time.sleep(2)
        while True:
            controls()
            if up_pressed == True and left_pressed == True:
                time.sleep(2)
                break


    sprite = pygame.image.load(sprites[0]).convert()
    background = pygame.image.load('Background 200x200.png').convert()

    sprite.set_colorkey(BLACK)

    while True:
        main()

2 个答案:

答案 0 :(得分:1)

按下左上键和上键时使其暂停的方法是:

import pygame
from pygame.locals import *
#game code
…
def pause():
    keyspressed = pygame.keys.get_pressed()
    if keyspressed[K_LEFT] and keyspressed[K_UP]:
        #pause code
        …

此代码应该是正确的,但如果您发现任何奇怪的事情,请尝试研究pygame密钥模块。请注意,K_LEFT和K_UP来自pygame.locals,它是从pygame单独导入的

答案 1 :(得分:1)

在PyGame中编写游戏时,我发现这是一个非常有用的方法:

if event.type == pygame.KEYDOWN 
    if event.key == (pygame.K_RIGHT and pygame.K_LEFT):
        while True: # Infinite loop that will be broken when the user press the space bar again
            event = pygame.event.wait()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:  # decid how to unpause?
                break #Exit infinite loop

我最近才发现的一个有趣的tid-bit是pygame.key.get_pressed()返回一个1s和0s的元组,表示键盘上的所有键,这些可以用作布尔值或索引以获得有效钥匙的“价值”。 http://programarcadegames.com/

的一些重要内容