Click here to Skip to main content
15,357,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the link where you can see the screen.
I am attaching the code below.
Please resolve this ASAP.

What I have tried:

import pygame,sys
import random
screen = pygame.display.set_mode( (800,600) )

rectangle = pygame.Rect(400, 300, 60, 40)

clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:

# Process Player Input
up = pygame.key.get_pressed() [pygame.K_UP]
down = pygame.key.het_pressed() [pygame.K_DOWN]
left = pygame.key.get_pressed() [pygame.K_LEFT]
right = pygame.key.get_pressed() [pygame.K_RIGHT]

#Updating game logic
if up:
    rectangle.y += -3
if down:
    rectangle.y += 3
if left:
    rectangle.x += -3
if right:
    rectangle.x += 3

if rectangle.x < 0:
    rectangle.x = 0
if rectangle.y < 0:
    rectangle.y = 0
if rectangle.x > screen.get_width() - rectangle.width:
    rectangle.x = screen.get_width() - rectangle.width
if rectangle.y > screen.get_height() - rectangle.height:
    rectangle.y = screen.get_height() - rectangle.height

screen.fill( (200,200,255) )
pygame.draw.rect(screen, (255,255,255), rectangle)

#Stimulate Some Delay
randDelay = random.randrange(randDelay)

Updated 19-May-22 4:05am

1 solution

Python execution starts at the top of the file, and proceeds down.
Indentation is significant: lines which are indented to the right are part of a different code block to the line above.

Your code starts with a loop, the only way out of which is to exit the program:
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
So no code below that will ever be executed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900