correction des collisions avec la balle (fait pas ia)
This commit is contained in:
+47
-14
@@ -36,8 +36,8 @@ class Ball(Entity):
|
||||
self.screen.blit(self.image, (self.pos.x, self.pos.y))
|
||||
|
||||
def speed_up(self):
|
||||
self.velocity.x = self.velocity.x + .75 if (self.velocity.x > 0) else self.velocity.x - .75
|
||||
self.velocity.y = self.velocity.y + .75 if (self.velocity.y > 0) else self.velocity.y - .75
|
||||
self.velocity.x = self.velocity.x + .5 if (self.velocity.x > 0) else self.velocity.x - .5
|
||||
self.velocity.y = self.velocity.y + .5 if (self.velocity.y > 0) else self.velocity.y - .5
|
||||
|
||||
def move(self, deltatime: float):
|
||||
if (self.velocity.x == 0 and self.velocity.y == 0): # si la balle est a l'arret lui donner un mouvement aléatoire
|
||||
@@ -55,23 +55,56 @@ class Ball(Entity):
|
||||
self.velocity.x *= scale
|
||||
self.velocity.y *= scale
|
||||
|
||||
if (self.pos.x < 0 or (self.pos.x + round(self.size / 2)) > self.screen.get_size()[0]): self.velocity.x = -self.velocity.x # Collision avec le mur
|
||||
# Réinitialise le flag de collision pour cette frame
|
||||
self.collide = False
|
||||
|
||||
# collision avec le joueur
|
||||
if ((self.pos.y + self.size / 2) >= self.player.pos.y - self.player.size[1] and self.pos.y <= self.player.pos.y):
|
||||
if (self.pos.x + self.size >= self.player.pos.x and self.pos.x - self.size <= self.player.pos.x + self.player.size[0]):
|
||||
# Collision avec les murs gauche/droite
|
||||
if (self.pos.x < 0 or (self.pos.x + self.size) > self.screen.get_size()[0]):
|
||||
self.velocity.x = -self.velocity.x
|
||||
|
||||
# Récupère le centre et le rayon de la balle
|
||||
ball_center_x = self.pos.x + self.size / 2
|
||||
ball_center_y = self.pos.y + self.size / 2
|
||||
ball_radius = self.size / 2
|
||||
|
||||
# Collision avec le joueur (raquette du bas)
|
||||
player_left = self.player.pos.x
|
||||
player_right = self.player.pos.x + self.player.size[0]
|
||||
player_top = self.player.pos.y - self.player.size[1]
|
||||
player_bottom = self.player.pos.y
|
||||
|
||||
# Vérifie si la balle entre en collision avec la raquette du joueur
|
||||
if (ball_center_y + ball_radius >= player_top and
|
||||
ball_center_y - ball_radius <= player_bottom and
|
||||
ball_center_x + ball_radius >= player_left and
|
||||
ball_center_x - ball_radius <= player_right):
|
||||
|
||||
# Vérifie que la balle vient de dessous (évite les faux positifs)
|
||||
if self.velocity.y > 0:
|
||||
self.velocity.y = -self.velocity.y
|
||||
if not self.collide: self.speed_up()
|
||||
if not self.collide:
|
||||
self.speed_up()
|
||||
self.collide = True
|
||||
|
||||
# collision avec le bot
|
||||
elif (self.pos.y - round(self.size / 2) <= self.bot.pos.y and self.pos.y >= self.bot.pos.y + self.bot.size[1]):
|
||||
if (self.pos.x + self.size >= self.bot.pos.x and self.pos.x - self.size <= self.bot.pos.x + self.bot.size[0]):
|
||||
self.velocity.y = -self.velocity.y
|
||||
if not self.collide: self.speed_up()
|
||||
self.collide = True
|
||||
# Collision avec le bot (raquette du haut)
|
||||
elif (ball_center_y > 0): # Évite les collisions négatives
|
||||
bot_left = self.bot.pos.x
|
||||
bot_right = self.bot.pos.x + self.bot.size[0]
|
||||
bot_top = self.bot.pos.y
|
||||
bot_bottom = self.bot.pos.y + self.bot.size[1]
|
||||
|
||||
else: self.collide = False
|
||||
# Vérifie si la balle entre en collision avec la raquette du bot
|
||||
if (ball_center_y + ball_radius >= bot_top and
|
||||
ball_center_y - ball_radius <= bot_bottom and
|
||||
ball_center_x + ball_radius >= bot_left and
|
||||
ball_center_x - ball_radius <= bot_right):
|
||||
|
||||
# Vérifie que la balle vient de dessus (évite les faux positifs)
|
||||
if self.velocity.y < 0:
|
||||
self.velocity.y = -self.velocity.y
|
||||
if not self.collide:
|
||||
self.speed_up()
|
||||
self.collide = True
|
||||
|
||||
self.pos.x += self.velocity.x * deltatime * 100
|
||||
self.pos.y += self.velocity.y * deltatime * 100
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"""
|
||||
TODO
|
||||
- orriger collision
|
||||
- faire un menu style arcade
|
||||
- rendre le jeu plus arcade
|
||||
- ajouter des sons style arcade
|
||||
|
||||
Reference in New Issue
Block a user