-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle.py
More file actions
41 lines (29 loc) · 1.11 KB
/
rectangle.py
File metadata and controls
41 lines (29 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
import pygame
screen_width, screen_height = 800, 600
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("My Pygame")
fill_color = (32, 52, 71)
rect_width, rect_height = 100, 200
rect_x = screen_width / 2 - rect_width / 2
rect_y = screen_height / 2 - rect_height / 2
rect_color = pygame.Color('lightyellow')
STEP = 10
while True:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and rect_y >= STEP:
rect_y -= STEP
if event.key == pygame.K_DOWN and rect_y <= screen_height - rect_height - STEP:
rect_y += STEP
if event.key == pygame.K_LEFT and rect_x >= STEP:
rect_x -= STEP
if event.key == pygame.K_RIGHT and rect_x <= screen_width - rect_width - STEP:
rect_x += STEP
screen.fill(fill_color)
pygame.draw.rect(screen, rect_color, (rect_x, rect_y, rect_width, rect_height))
pygame.display.update()