forked from KeithGalli/Basic-Python-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetris
More file actions
101 lines (80 loc) · 2.33 KB
/
Tetris
File metadata and controls
101 lines (80 loc) · 2.33 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
import pygame
import random
# Initialisation de Pygame
pygame.init()
# Couleurs
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (255, 165, 0)]
# Taille de l'écran
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 500
BLOCK_SIZE = 20
# Initialisation de l'écran
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Tetris")
# Classes
class Block:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, BLOCK_SIZE, BLOCK_SIZE))
def move(self, dx, dy):
self.x += dx
self.y += dy
class Shape:
def __init__(self, x, y):
self.x = x
self.y = y
self.color = random.choice(COLORS)
self.blocks = []
def draw(self):
for block in self.blocks:
block.draw()
def move(self, dx, dy):
for block in self.blocks:
block.move(dx, dy)
def rotate(self):
pass # À implémenter
def create_shape():
return Shape(SCREEN_WIDTH // 2, 0)
def draw_grid():
for x in range(0, SCREEN_WIDTH, BLOCK_SIZE):
pygame.draw.line(screen, GRAY, (x, 0), (x, SCREEN_HEIGHT))
for y in range(0, SCREEN_HEIGHT, BLOCK_SIZE):
pygame.draw.line(screen, GRAY, (0, y), (SCREEN_WIDTH, y))
# Initialisation du jeu
current_shape = create_shape()
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.5
# Boucle principale
running = True
while running:
screen.fill(BLACK)
# Gestion des événements
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Mouvement de la forme
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
current_shape.move(-BLOCK_SIZE, 0)
if keys[pygame.K_RIGHT]:
current_shape.move(BLOCK_SIZE, 0)
if keys[pygame.K_DOWN]:
current_shape.move(0, BLOCK_SIZE)
# Gestion de la chute de la forme
fall_time += clock.get_rawtime()
if fall_time // 1000 >= fall_speed:
current_shape.move(0, BLOCK_SIZE)
fall_time = 0
# Dessin de la grille et de la forme
draw_grid()
current_shape.draw()
pygame.display.flip()
clock.tick(30)
pygame.quit()