Repository navigation
Expand file tree
/
Copy pathgame005.py
More file actions
51 lines (44 loc) · 1.45 KB
/
Copy pathgame005.py
File metadata and controls
51 lines (44 loc) · 1.45 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
41
42
43
44
45
46
47
48
49
50
51
import pygame
pygame.init()
# Set up the display
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Moving Circle")
# Set up the circle
circle_radius = 50
circle_color = (0, 255, 0)
circle_x = screen_width // 2
circle_y = screen_height // 2
# Set up the movement
move_step = 50
# Draw the circle on the screen
def draw_circle(x, y):
pygame.draw.circle(screen, circle_color, (x, y), circle_radius)
# Update the screen
def update_screen():
screen.fill((0, 0, 0))
draw_circle(circle_x, circle_y)
pygame.display.update()
update_screen()
# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# Move the circle in response to arrow key presses
if event.key == pygame.K_LEFT:
if circle_x > 0 + circle_radius:
circle_x -= move_step
elif event.key == pygame.K_RIGHT:
if circle_x < screen_width - circle_radius:
circle_x += move_step
elif event.key == pygame.K_UP:
if circle_y > 0 + circle_radius:
circle_y -= move_step
elif event.key == pygame.K_DOWN:
if circle_y < screen_height - circle_radius:
circle_y += move_step
update_screen()