-2
\$\begingroup\$

It was working before, then I added the enemy rectangle and it has stopped working.

import time, copy
import pygame
import random
import os
WINDOW_WIDTH = 560
WINDOW_HEIGHT = 560
WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
red =(255,0,0)
green =(0,255,50)
yellow =(255,255,0)
black = (0,0,0)
white = (255,255,255)
WINDOW.fill(black)
pygame.display.update()
a = 20
b = 20
L = False
R = False
U = False
D = False
##x = [20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,520]
##ex = x[random.randint(0,24)]
##ey = x[random.randint(0,24)]
player = pygame.Rect(a,b,20,20)
pygame.draw.rect(WINDOW, green, player)
##enemy = pygame.Rect(ex,ey,20,20)
##pygame.draw.rect(WINDOW, red, enemy)
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:                  
            if event.key == pygame.K_LEFT:        
                L = True
            elif event.key == pygame.K_RIGHT:    
                R = True
            elif event.key == pygame.K_UP:       
                U = True
            elif event.key == pygame.K_DOWN:    
                D = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                L = False
            elif event.key == pygame.K_RIGHT:     
                R = False
            elif event.key == pygame.K_UP:       
                U = False
            elif event.key == pygame.K_DOWN:     
                D = False
        if(D):
            if b < 540:
                pygame.draw.rect(WINDOW, black, player)
                b = b+20
                player = pygame.Rect(a,b,20,20)
                pygame.draw.rect(WINDOW, green, player)
        elif(U):
            if b != 0:
                pygame.draw.rect(WINDOW, black, player)
                b = b-20
                player = pygame.Rect(a,b,20,20)
                pygame.draw.rect(WINDOW, green, player)
        elif(R):
            if a < 540:
                pygame.draw.rect(WINDOW, black, player)
                a = a+20
                player = pygame.Rect(a,b,20,20)
                pygame.draw.rect(WINDOW, green, player)
        elif(L):
            if a != 0:
                pygame.draw.rect(WINDOW, black, player)
                a = a-20
                player = pygame.Rect(a,b,20,20)
                pygame.draw.rect(WINDOW, green, player)
##        time.sleep(0.2)
##        if ex < a:
##            pygame.draw.rect(WINDOW, black, enemy)
##            ex = ex+20
##            enemy = pygame.Rect(ex,ey,20,20)
##            pygame.draw.rect(WINDOW, red, enemy)
##        elif ex > a:
##            pygame.draw.rect(WINDOW, black, enemy)
##            ex = ex-20
##            enemy = pygame.Rect(ex,ey,20,20)
##            pygame.draw.rect(WINDOW, red, enemy)
##        elif ey > b:
##            pygame.draw.rect(WINDOW, black, enemy)
##            ey = ey-20
##            enemy = pygame.Rect(ex,ey,20,20)
##            pygame.draw.rect(WINDOW, red, enemy)
##        elif ey > b:
##            pygame.draw.rect(WINDOW, black, enemy)
##            ey = ey-20
##            enemy = pygame.Rect(ex,ey,20,20)
##            pygame.draw.rect(WINDOW, red, enemy)
##        pygame.display.update()
##        if ex == a and ey == b:
##            WINDOW.fill(white)
##            pygame.display.update()
##            time.sleep(2)
##            os._exit(1)
\$\endgroup\$
2
  • \$\begingroup\$ What did stop working? Your whole game or just the key detection? And all errors you get shouldbe included as well \$\endgroup\$
    – Zibelas
    Oct 23, 2021 at 12:08
  • \$\begingroup\$ How about sharing the code that does work (minus all the commented out cruft) followed by exactly what you added (the diff) that broke it? \$\endgroup\$ Oct 23, 2021 at 12:30

1 Answer 1

1
\$\begingroup\$

I've never used Pygame, but I'm pretty sure you're supposed to be updating the display in the main loop, not outside.

pygame.display.update() on line 28 should be inside the loop, so under the while True: part.

I noticed that you had an update further down, but you commented it out.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .