-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalGame.py
60 lines (47 loc) · 1.95 KB
/
normalGame.py
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
import pygame
from Src.snake_enviroment import SnakeEnv
def play_manual_snake():
# Initialize the environment
env = SnakeEnv(grid_size=20)
# Initialize game state
done = False
score = 0
clock = pygame.time.Clock()
current_action = 1
while not done:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
# Handle direction changes
if event.key == pygame.K_UP and current_action != 1:
current_action = 0
elif event.key == pygame.K_DOWN and current_action != 0:
current_action = 1
elif event.key == pygame.K_LEFT and current_action != 3:
current_action = 2
elif event.key == pygame.K_RIGHT and current_action != 2:
current_action = 3
# Handle speed controls
elif event.key == pygame.K_SPACE:
env.speed_controller.toggle_pause()
elif event.key == pygame.K_UP and pygame.key.get_mods() & pygame.KMOD_CTRL:
env.speed_controller.increase_speed()
elif event.key == pygame.K_DOWN and pygame.key.get_mods() & pygame.KMOD_CTRL:
env.speed_controller.decrease_speed()
# Take action if not paused
if not env.speed_controller.paused:
state, reward, done, info = env.step(current_action)
if reward > 0:
score += 1
# Render the game
env.render()
# Control game speed
if not env.speed_controller.paused:
clock.tick(10 * env.speed_controller.speed)
print(f"Game Over! Final Score: {score}")
pygame.quit()
if __name__ == "__main__":
play_manual_snake()