Katie-Rose
Katie-Rose

Reputation: 1

Pausing a game using pygame by pressing two keys simultaneously - tamagotchi project

I'm trying to implement a pause function in my tamagotchi clone (I'm practising for my controlled assessment next year) and I can't get the left and up keys to work simultaneously as a pause button. If possible I want to stay as true to the original game as possible so I would prefer not to use on-screen buttons. Thanks!

    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()

Upvotes: 0

Views: 233

Answers (2)

srattigan
srattigan

Reputation: 664

Here is a method that I have found really useful when writing games in 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

An interesting tid-bit I only recently discovered is that pygame.key.get_pressed() returns a tuple of 1s and 0s representing all of the keys on the keyboard, and these can be used as booleans or indexed to get the effective "value" of a key. Some great tuts at http://programarcadegames.com/

Upvotes: 1

Ethanol
Ethanol

Reputation: 370

A way to make it pause when both left iey and up key are pressed is this:

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
        …

This code should be correct, but if you find any weird things, try to research the pygame key module. Keep in note that K_LEFT and K_UP are from pygame.locals, which is imported seperately from pygame

Upvotes: 1

Related Questions