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.