bot now hit ball to send it in a random direction and fixed walls when ball is going straight

This commit is contained in:
2024-10-10 02:33:04 +02:00
parent 536e3969b1
commit 37e4f0b83c
2 changed files with 27 additions and 12 deletions

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/06 03:24:10 by tomoron #+# #+# #
# Updated: 2024/10/09 09:09:54 by tomoron ### ########.fr #
# Updated: 2024/10/10 01:25:42 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -20,14 +20,15 @@ class Ball:
self.obstacles = []
def setStartVel(self, inv):
self.speed = GameSettings.startSpeed
self.vel[0] = self.speed * (random.randint(-50, 50) / 100)
self.vel[1] = self.speed - abs(self.vel[0])
if(inv == 2):
self.vel[1] = -self.vel[1]
# self.speed = GameSettings.startSpeed
# self.vel[0] = self.speed * (random.randint(-50, 50) / 100)
# self.vel[1] = self.speed - abs(self.vel[0])
# if(inv == 2):
# self.vel[1] = -self.vel[1]
self.vel = [0, -3]
def default(self):
self.pos = [0, 0]
self.pos = [1, 5]
self.up = False
self.vel = [0, 0]
self.speed = GameSettings.startSpeed
@ -69,7 +70,7 @@ class Ball:
return(None)
wallSide = (GameSettings.wallWidth / 2) + GameSettings.ballRadius
if(pos1[1] < 0):
if(self.vel[1] > 0):
wallSide *= -1
hitPos = (wallSide - offset) / slope
relPos = wpos - hitPos
@ -118,7 +119,7 @@ class Ball:
def wallColisionDistanceY(self, wall):
wallSide = (GameSettings.wallWidth / 2) + GameSettings.ballRadius
if(self.pos[1] < 0):
if(self.vel[1] > 0):
wallSide *= -1
relPos = wall["pos"]["x"] - self.pos[0]
if(abs(relPos) < (GameSettings.wallLength / 2) + GameSettings.ballRadius):
@ -131,7 +132,6 @@ class Ball:
return(jumper["pos"]["z"])
return(None)
def colisionTimeY(self, checkObstacles):
wallsTime = self.getTimeUntilWallColision("back","front", self.pos[1], self.vel[1])
if(not checkObstacles):

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 03:54:20 by tomoron #+# #+# #
# Updated: 2024/10/09 09:05:42 by tomoron ### ########.fr #
# Updated: 2024/10/10 02:24:08 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -14,6 +14,7 @@ from .Ball import Ball
from .Player import Player
from .DummySocket import DummySocket
from .GameSettings import GameSettings
import random
import asyncio
import time
@ -23,6 +24,7 @@ class Bot(Player):
self.game = game
self.ready = True
self.pos = {"pos":0, "up": False}
self.lastCalculated = {"pos":0, "up":False}
self.objective = {"pos":0, "up": False}
self.skin = 0
asyncio.create_task(self.updateLoop())
@ -38,6 +40,18 @@ class Bot(Player):
res.speed = self.game.ball.speed
return(res)
def genRandomBallDirection(self, pos, up, center):
if(self.lastCalculated["pos"] == pos and self.lastCalculated["up"] == up):
return(self.objective)
self.lastCalculated = {"pos":pos, "up" : up}
if(not center):
offset = random.randint(-100, 100) / 100
if(offset == 0):
offset = 0.1
pos += offset * (GameSettings.playerLength / 2)
return({"pos":pos, "up":up})
async def getExpectedPos(self):
tempBall = self.createTempBall()
hit = 0
@ -47,8 +61,9 @@ class Bot(Player):
hit = await tempBall.update(sleepTime, self, self.game.p2, True)
i += 1
if(i == 50):
self.objective = self.genRandomBallDirection(0, 0, True)
return
self.objective = {"pos":tempBall.pos[0], "up":tempBall.up}
self.objective = self.genRandomBallDirection(tempBall.pos[0], tempBall.up, False)
async def updateLoop(self):
while not self.game.end: