0

So basically I'm making an escape room game. In the game, you have to interact with a desk and a key would spawn on the map. Then you would interact with the key and pick it up and it would go on top of your head, and you are free to take it anywhere to escape the escape room. Now the problem I'm having, is I'm able to interact with the desk, and the collision works. But the key doesn't spawn.

Here is my code:

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
#VARIABLES
velocity = 1
yellow = (255,255,0)
clock = pygame.time.Clock()
white = (255,255,255)
blue = (0,0,255)
black = (0,0,0)
brown = (110,38,14)
x = 300
y = 300
#VARIABLES
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()  
    def Room():
        global Player
        global x
        global y
        screen.fill(black)
        Player = pygame.draw.rect(screen,blue,(x,y,50,50))
        wall_1 = pygame.draw.rect(screen,white,(10,50,20,500))
        wall_2 = pygame.draw.rect(screen,white,(10,30,580,20))
        wall_3 = pygame.draw.rect(screen,white,(570,40,20,520))
        wall_4 = pygame.draw.rect(screen,white,(10,540,580,20))
        if Player.colliderect(wall_1):
            x = x + 70
        if Player.colliderect(wall_2):
            y = y + 70
        if Player.colliderect(wall_3):
            x = x - 70
        if Player.colliderect(wall_4):
            y = y - 70
    def Movement():
        global velocity
        global x
        global y
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                x = x - velocity
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                x = x + velocity
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP or event.key == ord('w'):
                y = y - velocity
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                y = y + velocity
    def Text():
        font = pygame.font.Font("freesansbold.ttf",15)
        interact = font.render("E To Interact!",True, (255,255,255))
        screen.blit(interact,(75,275))
    def FirstKey():
        global Player
        global x
        global y
        KeyX = 300
        KeyY = 400
        key = pygame.draw.rect(screen,yellow,(KeyX,KeyY,15,15))
        if Player.colliderect(key) == True:
            if key[K_e]:
                KeyX = x
                KeyY = y - 35
    def Desk():
        global x
        global velocity
        Desk = pygame.draw.rect(screen,brown,(75,300,20,60))
        if Player.colliderect(Desk) == True:
            velocity = 0
            Text()
            keys = pygame.key.get_pressed()
            if keys[K_e]:
                x = 300
                velocity = 1
                FirstKey()
        else:
            None




    #Function Calls#
    Room()
    Movement()
    Desk()
    #Function Calls#
    pygame.display.update()
    #FPS#
    clock.tick(120)
    #FPS#

I would appreciate an answer as well as an explanation on what I'm doing wrong. P.S. I'm using the latest version of Python(3.10.5) and the Pygame module.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ravishankar D
  • 131
  • 10

1 Answers1

0

Try to encapsulate things in a class, instead of calling the functions secuentially, as it happens in the game, split the functions on what they need to do, and call them on every cycle. So you need to check for input, check for collisions, check inventary, check states, draw the objects, and so on.

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
#VARIABLES
velocity = 1
yellow = (255,255,0)
clock = pygame.time.Clock()
white = (255,255,255)
blue = (0,0,255)
black = (0,0,0)
brown = (110,38,14)

#VARIABLES

class mygame():
    x = 300
    y = 300
    velocity = 1
    state = 'idle'
    draw_key = False
    def draw_objects(self):
        screen.fill(black)
        self.player = pygame.draw.rect(screen,blue,(self.x,self.y,50,50))
        self.wall_1 = pygame.draw.rect(screen,white,(10,50,20,500))
        self.wall_2 = pygame.draw.rect(screen,white,(10,30,580,20))
        self.wall_3 = pygame.draw.rect(screen,white,(570,40,20,520))
        self.wall_4 = pygame.draw.rect(screen,white,(10,540,580,20))
        self.desk = pygame.draw.rect(screen,brown,(75,300,20,60))
        if self.draw_key:
            self.key = pygame.draw.rect(screen,yellow,(200,200,20,20))
        
    def check_collisions(self):
        if self.player.colliderect(self.desk) == True:
            font = pygame.font.Font("freesansbold.ttf",15)
            interact = font.render("E To Interact!",True, (255,255,255))
            screen.blit(interact,(75,275))
        if self.player.colliderect(self.wall_1) == True:
            self.x = 10
        if self.player.colliderect(self.wall_2) == True:
            self.y = 10
        if self.player.colliderect(self.wall_3) == True:
            self.x = 570
        if self.player.colliderect(self.wall_4) == True:
            self.y = 540


    def check_buttons(self):
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_e:
                    self.state = 'interacting'
                    self.draw_key = False
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    self.state = 'up'
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    self.state = 'down'
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    self.state = 'left'
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    self.state = 'right'
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    exit()
            if event.type == pygame.KEYUP:
                self.state = 'idle'
            
    def move(self):
        if self.state == 'up':
            self.y -= self.velocity
        if self.state == 'down':
            self.y += self.velocity
        if self.state == 'left':
            self.x -= self.velocity
        if self.state == 'right':
            self.x += self.velocity
        if self.state == 'idle':
            pass
        if self.state == 'interacting':
            self.draw_key = True

    def play(self):
        while True:
            self.draw_objects()
            self.check_buttons()
            self.check_collisions()
            self.move()
            pygame.display.update()
            clock.tick(120)


game = mygame()
game.play()