-
Notifications
You must be signed in to change notification settings - Fork 1
/
game04.py
145 lines (123 loc) · 3.76 KB
/
game04.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
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
import pygame
from dataclasses import dataclass
from pygame.constants import KEYDOWN
pygame.init()
clock = pygame.time.Clock()
# Notre fenêtre de jeu aura une dimenssion de 400x600 pixels
win = pygame.display.set_mode((400, 600))
pygame.display.set_caption("{EPITECH.}-Reunion-FocusTech-JV")
font = pygame.font.SysFont(None, 24)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
@dataclass
class Player():
x: float
y: float
direction: str = ""
speed: float = 400
alive: bool = True
score: int = 0
def draw(self):
if self.alive:
rect = pygame.Rect(self.x, self.y, 30, 30)
pygame.draw.rect(win, WHITE, rect)
score_text = font.render(f'Score: {self.score}', True, WHITE)
win.blit(score_text, (0, 560))
def update(self):
if self.alive:
if self.direction == "left":
self.x -= self.speed * dt
elif self.direction == "right":
self.x += self.speed * dt
if self.x > 370:
self.x = 370
if self.x < 0:
self.x = 0
@dataclass()
class Bullet():
x: float
y: float
is_shot: bool = False
speed: int = 400
def shoot(self):
if player.alive:
self.x = player.x + 15
self.y = player.y
self.is_shot = True
def draw(self):
if self.is_shot:
rect = pygame.Rect(self.x, self.y, 5, 15)
pygame.draw.rect(win, WHITE, rect)
def update(self):
if self.y < 0:
self.is_shot = False
if self.is_shot:
self.y -= self.speed * dt
@dataclass
class Enemy():
x: float
y: float
color: tuple = RED
speed: float = 200
alive: bool = True
def draw(self):
if self.alive:
rect = pygame.Rect(self.x, self.y, 30, 30)
pygame.draw.rect(win, self.color, rect)
def update(self):
if self.alive:
if self.x > 430:
self.y += 30
self.speed = -self.speed
if self.x < -30:
self.y += 30
self.speed = -self.speed
if self.y > 630:
self.speed = 0
self.x += self.speed * dt
def collide_with_ship(self):
if self.alive:
enemy_rect = pygame.Rect(self.x, self.y, 30, 30)
ship_rect = pygame.Rect(player.x, player.y, 30, 30)
return enemy_rect.colliderect(ship_rect)
def collide_with_bullet(self):
if self.alive and bullet.is_shot:
enemy_rect = pygame.Rect(self.x, self.y, 30, 30)
bullet_rect = pygame.Rect(bullet.x, bullet.y, 5, 15)
return enemy_rect.colliderect(bullet_rect)
player = Player(x=200, y=550)
bullet = Bullet(x=200, y=550)
enemy = Enemy(x=0, y=0)
running = True
while running:
dt = clock.tick(60)/1000
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
key_press = pygame.key.get_pressed()
if key_press[pygame.K_LEFT]:
player.direction = "left"
elif key_press[pygame.K_RIGHT]:
player.direction = "right"
else:
player.direction = ""
if key_press[pygame.K_SPACE] and not bullet.is_shot:
bullet.shoot()
win.fill(BLACK)
player.update()
player.draw()
bullet.update()
bullet.draw()
enemy.update()
enemy.draw()
if enemy.collide_with_ship():
player.alive = False
if enemy.collide_with_bullet():
bullet.is_shot = False
# 4 Augmentez le score du joueur et la vitesse de l'ennemi
# N'oubliez pas de replacer l'ennemi à sa position initiale en (0, 0)
pygame.display.update()