0

I'm making a simple game, for now, all it is is a rectangle that moves ten pixels, turns the entire screen red for 5 seconds, and turns the screen back to normal (at least that's what I want it to do). for now it isn't taking any key input! The module itself works but stops when I put it in my code.

import time

import pygame
#---------------------------------------------------------
pygame.init()
win = pygame.display.set_mode((500, 500))
x = 200
y = 200
width = 20
height = 20
vel = 10
turn=True
run=True

while run:
 
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
   break
 if turn:
  print('turn')
  win.fill((0,255,0))
  while True:
   print("while loop entered")
   keys = pygame.key.get_pressed()
   if keys[pygame.K_LEFT] and x>0:
    print('left')
    x -= vel
    break
   if keys[pygame.K_RIGHT] and x<500-width:
    x += vel
    break
   if keys[pygame.K_UP] and y>0:
    y -= vel
    break
   if keys[pygame.K_DOWN] and y<500-height:
    y += vel
    break
   pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
   pygame.display.update()
 else:
  print('no turn')
  win.fill((0,255,0))
 pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
 
 pygame.display.update()
 time.sleep(5)
 if turn:
  turn=False
 else:
  turn=True
 print('Turn changed')
pygame.quit()
#-------------------------------------------------------------------

0 Answers0