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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
from enum import Enum
import pygame
# Initialize Pygame
pygame.init()
# Set the dimensions of the window
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
# Sprite sheet for pacman
sprite_sheet = pygame.image.load('../assets/pacman_left_sprite.png').convert_alpha();
sheet_width, sheet_height = sprite_sheet.get_size()
sprite_width, sprite_height = 32, 32
rows = sheet_height // sprite_height
columns = sheet_width // sprite_width
# Set the center position of the circle
center = [320, 240] # Center of the window
radius = 0
# Set the circle's velocity
dx = 0
dy = 0
# Set the speed of the pacman movement
speed = 5
fps = 60
clock = pygame.time.Clock()
sprites = []
class DIRECTION(Enum):
UP = 1
DOWN = 2
RIGHT = 3
LEFT = 4
def get_sprites():
for row in range(rows):
for col in range(columns):
x = col * sprite_width
y = row * sprite_height
# Create a new surface for the current sprite and blit it from the sprite sheet onto this new surface
new_sprite_surface = pygame.Surface((sprite_width, sprite_height), pygame.SRCALPHA)
new_sprite_surface.blit(sprite_sheet, (0, 0), (x, y, x + sprite_width, y +sprite_height))
# Add this new surface to our list of sprites
sprites.append(new_sprite_surface)
# Checks collision with walls
def check_collision(pos_x, pos_y, dx, dy):
# edges of the circle
# upper_circle_point = circle_center_y
# lower_circle_point = circle_center_y
# right_circle_point = circle_center_x
# left_circle_point = circle_center_x
print(pos_x, pos_y)
return pos_y + sprite_height + dy > height or pos_y + dy < 0 or pos_x + sprite_width + dx > width or pos_x + dx < 0
get_sprites()
counter = 0
def draw_player(pos, direction):
if direction == DIRECTION.UP:
screen.blit(pygame.transform.rotate(sprites[counter // 5], 270), pos)
elif direction == DIRECTION.DOWN:
screen.blit(pygame.transform.rotate(sprites[counter // 5], 90), pos)
elif direction == DIRECTION.RIGHT:
screen.blit(sprites[counter // 5], pos)
elif direction == DIRECTION.LEFT:
screen.blit(pygame.transform.flip(sprites[counter // 5], True, False), pos)
sprite_direction = DIRECTION.LEFT
# Main game loop
running = True
while running:
# Handle events
clock.tick(fps)
if counter < 19:
counter += 1
else:
counter = 0
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:
sprite_direction = DIRECTION.UP
dy = -speed
dx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_s:
sprite_direction = DIRECTION.DOWN
dy = speed
dx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_a:
sprite_direction = DIRECTION.RIGHT
dx = -speed
dy = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_d:
sprite_direction = DIRECTION.LEFT
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
screen.fill((0, 0, 0)) # Clear the screen
draw_player(center, sprite_direction)
# Update the screen
pygame.display.flip()
# Quit Pygame
pygame.quit()
|