-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.python
More file actions
182 lines (151 loc) · 5.42 KB
/
tempCodeRunnerFile.python
File metadata and controls
182 lines (151 loc) · 5.42 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import pygame
import random
# Initialisation de Pygame
pygame.init()
# Dimensions de l'écran
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Cubes2048.io")
# Couleurs
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Clock pour contrôler le FPS
clock = pygame.time.Clock()
FPS = 60
# Classe Cube
class Cube:
def __init__(self, x, y, value):
self.x = x
self.y = y
self.value = value
self.width = 50
self.height = 50
self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
self.vel_y = 2 # Vitesse verticale
def move(self):
self.y += self.vel_y
def draw(self, surface):
pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))
font = pygame.font.Font(None, 36)
text = font.render(str(self.value), True, BLACK)
surface.blit(text, (self.x + self.width // 4, self.y + self.height // 4))
# Classe Serpent Ennemi
class EnemySnake:
def __init__(self, x, y, size, value):
self.x = x
self.y = y
self.size = size
self.value = value
self.width = self.size # Définit la largeur basée sur la taille
self.height = self.size # Définit la hauteur basée sur la taille
self.color = (255, random.randint(50, 200), random.randint(50, 200))
self.vel_x = random.choice([-2, 2])
self.vel_y = random.choice([-2, 2])
def move(self):
self.x += self.vel_x
self.y += self.vel_y
# Rebonds sur les murs
if self.x <= 0 or self.x + self.width >= WIDTH:
self.vel_x *= -1
if self.y <= 0 or self.y + self.height >= HEIGHT:
self.vel_y *= -1
def draw(self, surface):
pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))
font = pygame.font.Font(None, 24)
text = font.render(str(self.value), True, BLACK)
surface.blit(text, (self.x + self.width // 4, self.y + self.height // 4))
# Fonctions utilitaires
def check_collision(obj1, obj2):
return (
obj1.x < obj2.x + obj2.width and
obj1.x + obj1.width > obj2.x and
obj1.y < obj2.y + obj2.height and
obj1.y + obj1.height > obj2.y
)
# Initialisation des listes de cubes et ennemis
cubes = []
enemies = []
# Joueur (un grand cube contrôlé par le joueur)
player = Cube(WIDTH // 2, HEIGHT - 60, 8)
# Fonction pour ajouter un nouveau cube
def spawn_cube():
x = random.randint(0, WIDTH - 50)
value = random.choice([2, 4])
cubes.append(Cube(x, 0, value))
# Fonction pour ajouter un ennemi
def spawn_enemy():
x = random.randint(0, WIDTH - 50)
y = random.randint(0, HEIGHT - 50)
size = random.randint(30, 50)
value = random.choice([2, 4, 8, 16])
enemies.append(EnemySnake(x, y, size, value))
# Variables pour les timers
last_cube_spawn_time = pygame.time.get_ticks()
cube_spawn_interval = 3000 # 3 secondes
last_enemy_spawn_time = pygame.time.get_ticks()
enemy_spawn_interval = 5000 # 5 secondes
# Boucle principale du jeu
running = True
while running:
screen.fill(WHITE)
# Gérer les événements
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Contrôle du joueur
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x > 0:
player.x -= 5
if keys[pygame.K_RIGHT] and player.x + player.width < WIDTH:
player.x += 5
# Spawner de nouveaux cubes
if pygame.time.get_ticks() - last_cube_spawn_time > cube_spawn_interval:
spawn_cube()
last_cube_spawn_time = pygame.time.get_ticks()
# Spawner de nouveaux ennemis
if pygame.time.get_ticks() - last_enemy_spawn_time > enemy_spawn_interval:
spawn_enemy()
last_enemy_spawn_time = pygame.time.get_ticks()
# Déplacement et dessin des cubes
for cube in cubes[:]:
cube.move()
cube.draw(screen)
# Fusionner les cubes s'ils se touchent
if check_collision(player, cube):
if player.value == cube.value:
player.value *= 2
cubes.remove(cube)
elif player.value > cube.value:
# Absorber un cube de valeur inférieure
player.value += cube.value
cubes.remove(cube)
else:
# Collision fatale avec un cube plus fort
print("Game Over! Touched a cube stronger than you.")
running = False
elif cube.y > HEIGHT:
cubes.remove(cube) # Supprimer les cubes hors de l'écran
# Déplacement et dessin des ennemis
for enemy in enemies[:]:
enemy.move()
enemy.draw(screen)
# Collision entre joueur et ennemi
if check_collision(player, enemy):
if player.value > enemy.value: # Absorption
player.value += enemy.value
enemies.remove(enemy)
else: # Le joueur perd
print("Game Over! You were eaten by an enemy snake!")
running = False
# Dessiner le joueur
player.draw(screen)
# Vérification de défaite (cubes atteignant le haut de l'écran)
if any(cube.y <= 0 for cube in cubes):
print("Game Over! Cubes reached the top!")
running = False
# Mettre à jour l'écran
pygame.display.flip()
clock.tick(FPS)
pygame.quit()