Skip to content

Commit

Permalink
my way plus ball reset
Browse files Browse the repository at this point in the history
  • Loading branch information
PauLuke committed Feb 8, 2024
1 parent 57b1037 commit 6964330
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 24 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pygame
from random import choice


def ball_animation():
Expand All @@ -11,12 +12,29 @@ def ball_animation():
if ball.top <= 0 or ball.bottom >= screen_height:
ball_speed_y *= -1
if ball.left <= 0 or ball.right >= screen_width:
ball_speed_x *= -1
ball.center = (screen_width / 2, screen_height / 2)
ball_speed_x *= choice((1, -1))
ball_speed_y *= choice((1, -1))

# Adding collision with the players
if ball.colliderect(player_1) or ball.colliderect(player_2):
ball_speed_x *= -1


def players_animation():
keys = pygame.key.get_pressed()

if keys[pygame.K_d] and player_1.top >= 0:
player_1.y -= 10
if keys[pygame.K_a] and player_1.bottom <= screen_height:
player_1.y += 10

if keys[pygame.K_LEFT] and player_2.top >= 0:
player_2.y -= 10
if keys[pygame.K_RIGHT] and player_2.bottom <= screen_height:
player_2.y += 10


# Screen size
screen_width = 1000
screen_height = 600
Expand All @@ -38,37 +56,30 @@ def ball_animation():
player_1 = pygame.Rect(10, screen_height / 2 - 45, 10, 90)
player_2 = pygame.Rect(screen_width - 20, screen_height / 2 - 45, 10, 90)

# Main loop
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

ball_animation()
keys = pygame.key.get_pressed()
players_animation()

# Set background color
screen.fill(bg_color)

# Central dashed line
# Drawing the dashed central line
for i in range(0, 40, 3):
pygame.draw.rect(screen, light_grey, (screen_width / 2 - 1.5, i * 15, 3, 15))

# Drawing the ball
pygame.draw.rect(screen, light_grey, ball)

# Player 1
# Drawing player 1
pygame.draw.rect(screen, light_grey, player_1)
if keys[pygame.K_d] and player_1.top >= 0:
player_1.y -= 20
if keys[pygame.K_a] and player_1.bottom <= screen_height:
player_1.y += 20

# Player 2
# Drawing player 2
pygame.draw.rect(screen, light_grey, player_2)
if keys[pygame.K_RIGHT] and player_2.top >= 0:
player_2.y -= 20
if keys[pygame.K_LEFT] and player_2.bottom < screen_height:
player_2.y += 20

pygame.display.flip()
clock.tick(60)
Expand Down

0 comments on commit 6964330

Please sign in to comment.