fixed some game logic

This commit is contained in:
CHIBOUB Chakib 2024-07-23 18:25:46 +02:00
parent 486accd4eb
commit 685173df7e
3 changed files with 42 additions and 20 deletions

View File

@ -12,13 +12,14 @@ class Game:
self.game_state = { self.game_state = {
'player1_name': player1.user.username, 'player1_name': player1.user.username,
'player2_name': player2.user.username, 'player2_name': player2.user.username,
'player1_position': 200, # middle of the game field 'player1_position': 150,
'player2_position': 200, 'player2_position': 150,
'ball_position': {'x': 400, 'y': 300}, # middle of the game field 'ball_position': {'x': 390, 'y': 190},
'ball_velocity': {'x': random.choice([-5, 5]), 'y': random.choice([-5, 5])}, 'ball_velocity': {'x': random.choice([-5, 5]), 'y': random.choice([-5, 5])},
'player1_score': 0, 'player1_score': 0,
'player2_score': 0 'player2_score': 0
} }
self.speed = 1
self.game_loop_task = None self.game_loop_task = None
async def start_game(self): async def start_game(self):
@ -37,28 +38,48 @@ class Game:
self.game_state['ball_position']['y'] += self.game_state['ball_velocity']['y'] self.game_state['ball_position']['y'] += self.game_state['ball_velocity']['y']
# Check for collisions with top and bottom walls # Check for collisions with top and bottom walls
if self.game_state['ball_position']['y'] <= 0 or self.game_state['ball_position']['y'] >= 600: if self.game_state['ball_position']['y'] <= 10 or self.game_state['ball_position']['y'] >= 390:
self.game_state['ball_velocity']['y'] *= -1 self.game_state['ball_velocity']['y'] *= -1
# Check for scoring # Check for scoring
if self.game_state['ball_position']['x'] <= 0: if self.game_state['ball_position']['x'] <= 10:
self.game_state['player2_score'] += 1 self.game_state['player2_score'] += 1
self.reset_ball() self.reset_ball()
elif self.game_state['ball_position']['x'] >= 800: elif self.game_state['ball_position']['x'] >= 790:
self.game_state['player1_score'] += 1 self.game_state['player1_score'] += 1
self.reset_ball() self.reset_ball()
# Check for collisions with paddles # Check for collisions with paddles
if self.game_state['ball_position']['x'] <= 20 and \ if self.game_state['ball_position']['x'] <= 20 and \
self.game_state['player1_position'] - 50 <= self.game_state['ball_position']['y'] <= self.game_state['player1_position'] + 50: self.game_state['player1_position'] <= self.game_state['ball_position']['y'] <= self.game_state['player1_position'] + 80:
self.update_ball_velocity()
self.game_state['ball_velocity']['x'] *= -1 self.game_state['ball_velocity']['x'] *= -1
elif self.game_state['ball_position']['x'] >= 780 and \ elif self.game_state['ball_position']['x'] >= 760 and \
self.game_state['player2_position'] - 50 <= self.game_state['ball_position']['y'] <= self.game_state['player2_position'] + 50: self.game_state['player2_position'] <= self.game_state['ball_position']['y'] <= self.game_state['player2_position'] + 80:
self.update_ball_velocity()
self.game_state['ball_velocity']['x'] *= -1 self.game_state['ball_velocity']['x'] *= -1
def reset_ball(self): def reset_ball(self):
self.game_state['ball_position'] = {'x': 400, 'y': 300} self.game_state['ball_position'] = {'x': 390, 'y': 190}
self.game_state['ball_velocity'] = {'x': random.choice([-5, 5]), 'y': random.choice([-5, 5])} self.game_state['ball_velocity'] = {'x': random.choice([-5, 5]), 'y': random.choice([-5, 5])}
self.speed = 1
def update_ball_velocity(self):
self.speed += 0.05
if self.speed > 2:
self.speed = 2
else:
print(f"Ball velocity: {self.speed}")
self.game_state['ball_velocity']['x'] *= self.speed
if self.game_state['ball_velocity']['x'] < -10:
self.game_state['ball_velocity']['x'] = -10
elif self.game_state['ball_velocity']['x'] > 10:
self.game_state['ball_velocity']['x'] = 10
self.game_state['ball_velocity']['y'] *= self.speed
if self.game_state['ball_velocity']['y'] < -10:
self.game_state['ball_velocity']['y'] = -10
elif self.game_state['ball_velocity']['y'] > 10:
self.game_state['ball_velocity']['y'] = 10
async def send_game_state(self): async def send_game_state(self):
message = json.dumps({ message = json.dumps({
@ -70,15 +91,15 @@ class Game:
async def handle_key_press(self, player, key): async def handle_key_press(self, player, key):
if player == self.player1: if player == self.player1:
if key == 'arrowup' and self.game_state['player1_position'] > 0: if key == 'arrowup' and self.game_state['player1_position'] >= 25:
self.game_state['player1_position'] -= 10 self.game_state['player1_position'] -= 25
elif key == 'arrowdown' and self.game_state['player1_position'] < 550: elif key == 'arrowdown' and self.game_state['player1_position'] <= 275:
self.game_state['player1_position'] += 10 self.game_state['player1_position'] += 25
elif player == self.player2: elif player == self.player2:
if key == 'arrowup' and self.game_state['player2_position'] > 0: if key == 'arrowup' and self.game_state['player2_position'] >= 25:
self.game_state['player2_position'] -= 10 self.game_state['player2_position'] -= 25
elif key == 'arrowdown' and self.game_state['player2_position'] < 550: elif key == 'arrowdown' and self.game_state['player2_position'] <= 275:
self.game_state['player2_position'] += 10 self.game_state['player2_position'] += 25
async def end_game(self): async def end_game(self):
if self.game_loop_task: if self.game_loop_task:

View File

@ -164,7 +164,7 @@ document.addEventListener('DOMContentLoaded', () => {
} }
function handleKeyDown(event) { function handleKeyDown(event) {
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') { if (event.key === 'ArrowUp' || event.key === 'ArrowDown' || event.key === '+') {
sendKeyPress(event.key.toLowerCase()); sendKeyPress(event.key.toLowerCase());
} }
} }

View File

@ -6,7 +6,8 @@ from django.conf import settings
from django.conf.urls.static import static from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), # Disable the admin page
# path('admin/', admin.site.urls),
path('api/', include('pong.game.urls')), path('api/', include('pong.game.urls')),
path('', include('pong.game.urls')), path('', include('pong.game.urls')),
] ]