created ball and bouncing off paddles
This commit is contained in:
parent
0ad5a3e97c
commit
88aa86228e
@ -1,11 +1,16 @@
|
|||||||
|
import math
|
||||||
import pygame
|
import pygame
|
||||||
import pypong
|
import pypong
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
SCREEN_SIZE = (800, 600) # Size of the game window
|
SCREEN_SIZE = (800, 600) # Size of the game window
|
||||||
GAME_FPS = 60
|
GAME_FPS = 60
|
||||||
GAME_COLOR = (255, 255, 255) # Color of all the sprites on screen
|
GAME_COLOR = (255, 255, 255) # Color of all the sprites on screen
|
||||||
PADDLE_SIZE = (20, 200)
|
PADDLE_SIZE = (20, 200)
|
||||||
PADDLE_SPEED = 10
|
PADDLE_SPEED = 10
|
||||||
|
BALL_SIZE = 30
|
||||||
|
BALL_SPEED = 7
|
||||||
|
|
||||||
|
|
||||||
class Paddle(pygame.sprite.Sprite):
|
class Paddle(pygame.sprite.Sprite):
|
||||||
@ -31,7 +36,39 @@ class Paddle(pygame.sprite.Sprite):
|
|||||||
self.rect.bottom = SCREEN_SIZE[1]
|
self.rect.bottom = SCREEN_SIZE[1]
|
||||||
|
|
||||||
|
|
||||||
def game(is_host=False):
|
class Ball(pygame.sprite.Sprite):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
# Booleans to randomize direction of the ball upon spawning
|
||||||
|
initial_x_vel_negative = bool(random.getrandbits(1))
|
||||||
|
initial_y_vel_negative = bool(random.getrandbits(1))
|
||||||
|
|
||||||
|
self.x_vel = BALL_SPEED
|
||||||
|
self.y_vel = BALL_SPEED
|
||||||
|
if initial_x_vel_negative: # Flip ball speed based on random bools
|
||||||
|
self.x_vel = -BALL_SPEED
|
||||||
|
if initial_y_vel_negative:
|
||||||
|
self.y_vel = -BALL_SPEED
|
||||||
|
|
||||||
|
self.image = pygame.Surface((BALL_SIZE, BALL_SIZE), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(self.image, GAME_COLOR, (BALL_SIZE / 2, BALL_SIZE / 2), math.floor(BALL_SIZE / 2.0))
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.rect.centerx += self.x_vel
|
||||||
|
self.rect.centery += self.y_vel
|
||||||
|
|
||||||
|
# Always bounce ball if it hits the top or bottom of the screen
|
||||||
|
if self.rect.top <= 0:
|
||||||
|
self.rect.top = 0
|
||||||
|
self.y_vel *= -1
|
||||||
|
if self.rect.bottom >= SCREEN_SIZE[1]:
|
||||||
|
self.rect.bottom = SCREEN_SIZE[1]
|
||||||
|
self.y_vel *= -1
|
||||||
|
|
||||||
|
|
||||||
|
def game(is_host=True):
|
||||||
"""
|
"""
|
||||||
Starts the game.
|
Starts the game.
|
||||||
"""
|
"""
|
||||||
@ -88,8 +125,26 @@ def game(is_host=False):
|
|||||||
# Create the two paddles
|
# Create the two paddles
|
||||||
host_player = Paddle()
|
host_player = Paddle()
|
||||||
client_player = Paddle()
|
client_player = Paddle()
|
||||||
|
host_player.rect.centery = SCREEN_SIZE[1] / 2
|
||||||
|
client_player.rect.centery = SCREEN_SIZE[1] / 2
|
||||||
client_player.rect.right = SCREEN_SIZE[0] # Move client paddle to other side
|
client_player.rect.right = SCREEN_SIZE[0] # Move client paddle to other side
|
||||||
all_sprites.add(host_player, client_player)
|
all_sprites.add(host_player, client_player)
|
||||||
|
paddle_sprites = pygame.sprite.Group()
|
||||||
|
paddle_sprites.add(host_player, client_player)
|
||||||
|
|
||||||
|
# Create the ball
|
||||||
|
ball = Ball()
|
||||||
|
ball.rect.centerx = SCREEN_SIZE[0] / 2
|
||||||
|
# The ball is moved up or down a random amount
|
||||||
|
ball.rect.centery = (SCREEN_SIZE[1] / 2) + random.randint(-25, 25)
|
||||||
|
all_sprites.add(ball)
|
||||||
|
|
||||||
|
# Create the scoreboard
|
||||||
|
# host_score = 0
|
||||||
|
# client_score = 0
|
||||||
|
|
||||||
|
draw()
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
clock.tick(GAME_FPS)
|
clock.tick(GAME_FPS)
|
||||||
@ -100,6 +155,10 @@ def game(is_host=False):
|
|||||||
else:
|
else:
|
||||||
keyboard_movement(client_player)
|
keyboard_movement(client_player)
|
||||||
|
|
||||||
|
# Bounce ball off of paddles
|
||||||
|
if pygame.sprite.spritecollideany(ball, paddle_sprites):
|
||||||
|
ball.x_vel *= -1
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user