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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import pygame
# Initialize Pygame
pygame.init()
# Set the dimensions of the window
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
# Set the color of the circle
color = (255, 255, 153) # Light yellow
# Set the center position of the circle
center = [320, 240] # Center of the window
radius = 16
# Set the circle's velocity
dx = 0
dy = 0
# Set the speed of the circle's movement
speed = 10
fps = 30
clock = pygame.time.Clock()
# Checks collision with walls
def check_collision(circle_center_x, circle_center_y, dx, dy):
# edges of the circle
upper_circle_point = circle_center_y + radius
lower_circle_point = circle_center_y - radius
right_circle_point = circle_center_x + radius
left_circle_point = circle_center_x - radius
return upper_circle_point + dy > height or lower_circle_point + dy < 0 or right_circle_point + dx > width or left_circle_point + dx < 0
# Main game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Move the circle based on the pressed key
if event.key == pygame.K_w:
dy = -speed
dx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_s:
dy = speed
dx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_a:
dx = -speed
dy = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_d:
dx = speed
dy = 0 # Necssarry to move only horizontal or vertical
# Update the circle's position
if not check_collision(center[0], center[1], dx, dy):
center[0] += dx
center[1] += dy
# Draw the filled circle on the screen
screen.fill((0, 0, 0)) # Clear the screen
pygame.draw.circle(screen, color, center, radius)
# Update the screen
pygame.display.flip()
clock.tick(fps)
# Quit Pygame
pygame.quit()
|