-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchat.py
More file actions
103 lines (85 loc) · 2.61 KB
/
Copy pathchat.py
File metadata and controls
103 lines (85 loc) · 2.61 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
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
import pygame
import random
# Set up Pygame
pygame.init()
# Set up window
WINDOW_SIZE = (400, 400)
window = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Circle Collector')
# Set up colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
colors = [RED, GREEN, BLUE]
# Set up font
font = pygame.font.Font(None, 36)
# Set up grid
GRID_SIZE = 8
grid = []
for i in range(GRID_SIZE):
grid.append([])
for j in range(GRID_SIZE):
grid[i].append(0)
# Place circles randomly on the grid
print('place circles on grid')
num_circles = 5
for i in range(num_circles):
circle_placed = False
while not circle_placed:
x = random.randint(0, GRID_SIZE - 1)
y = random.randint(0, GRID_SIZE - 1)
if grid[x][y] == 0:
grid[x][y] = random.randint(1, 3)
circle_placed = True
# Set up player
player_x = 0
player_y = 0
score = 0
# Set time limit
time_limit = 10
start_time = pygame.time.get_ticks()
print('main game loop')
# Main game loop
running = True
while running:
# Calculate time remaining
time_remaining = time_limit - (pygame.time.get_ticks() - start_time) / 1000
if time_remaining < 0:
time_remaining = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Check if player has moved
if event.key == pygame.K_LEFT and player_x > 0:
player_x -= 1
elif event.key == pygame.K_RIGHT and player_x < GRID_SIZE - 1:
player_x += 1
elif event.key == pygame.K_UP and player_y > 0:
player_y -= 1
elif event.key == pygame.K_DOWN and player_y < GRID_SIZE - 1:
player_y += 1
# Check if player has picked up a circle
if grid[player_x][player_y] != 0:
value = grid[player_x][player_y]
score += value
grid[player_x][player_y] = 0
# Check if time is up
if pygame.time.get_ticks() - start_time > time_limit * 1000:
running = False
# Draw grid and score
window.fill(BLACK)
for i in range(GRID_SIZE):
for j in range(GRID_SIZE):
if i == player_x and j == player_y:
pygame.draw.circle(window, WHITE, (i * 50 + 25, j * 50 + 25), 25)
elif grid[i][j] != 0:
value = grid[i][j]
pygame.draw.circle(window, colors[value-1], (i * 50 + 25, j * 50 + 25), 25)
score_text = font.render(f'Score: {score}', True, WHITE)
window.blit(score_text, (0, 0))
time_text = font.render(f'Time: {time_remaining:.1f}', True, WHITE)
window.blit(time_text, (200, 0))
pygame.display.flip()