Compare commits
6 Commits
main
...
game-build
Author | SHA1 | Date | |
---|---|---|---|
88aa86228e | |||
0ad5a3e97c | |||
5ce89d29ee | |||
3b17cdb8b8 | |||
0c4684da30 | |||
faa55f0b47 |
8
pypong/__main__.py
Normal file
8
pypong/__main__.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
"""
|
||||||
|
Launch script
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pypong.game_files.game import main as game_main
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
game_main()
|
0
pypong/game_files/__init__.py
Normal file
0
pypong/game_files/__init__.py
Normal file
169
pypong/game_files/game.py
Normal file
169
pypong/game_files/game.py
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
import math
|
||||||
|
import pygame
|
||||||
|
import pypong
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
SCREEN_SIZE = (800, 600) # Size of the game window
|
||||||
|
GAME_FPS = 60
|
||||||
|
GAME_COLOR = (255, 255, 255) # Color of all the sprites on screen
|
||||||
|
PADDLE_SIZE = (20, 200)
|
||||||
|
PADDLE_SPEED = 10
|
||||||
|
BALL_SIZE = 30
|
||||||
|
BALL_SPEED = 7
|
||||||
|
|
||||||
|
|
||||||
|
class Paddle(pygame.sprite.Sprite):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.moving_down = False
|
||||||
|
self.moving_up = False
|
||||||
|
|
||||||
|
self.image = pygame.Surface(PADDLE_SIZE)
|
||||||
|
self.image.fill(GAME_COLOR)
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
if self.moving_down:
|
||||||
|
self.rect.y += PADDLE_SPEED
|
||||||
|
if self.moving_up:
|
||||||
|
self.rect.y -= PADDLE_SPEED
|
||||||
|
|
||||||
|
if self.rect.top < 0:
|
||||||
|
self.rect.top = 0
|
||||||
|
if self.rect.bottom > SCREEN_SIZE[1]:
|
||||||
|
self.rect.bottom = SCREEN_SIZE[1]
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
window = pygame.display.set_mode(SCREEN_SIZE)
|
||||||
|
surface = pygame.Surface(SCREEN_SIZE)
|
||||||
|
|
||||||
|
# Set title of window
|
||||||
|
pygame.display.set_caption('PyPong {0}'.format(pypong.__version__))
|
||||||
|
|
||||||
|
# Add an icon to the pygame window.
|
||||||
|
# icon = pygame.image.load('NO ICON YET, WIP').convert_alpha()
|
||||||
|
# pygame.display.set_icon(icon)
|
||||||
|
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
# Default game state when the game first starts.
|
||||||
|
running = True
|
||||||
|
|
||||||
|
# A group of all the sprites on screen. Used to update all sprites at once
|
||||||
|
all_sprites = pygame.sprite.Group()
|
||||||
|
|
||||||
|
# Functions used for drawing the game screen #
|
||||||
|
|
||||||
|
def draw():
|
||||||
|
"""
|
||||||
|
Draws the main pygame display.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Draws all the sprites on a black background
|
||||||
|
all_sprites.update()
|
||||||
|
surface.fill((0, 0, 0))
|
||||||
|
all_sprites.draw(surface)
|
||||||
|
window.blit(surface, surface.get_rect())
|
||||||
|
|
||||||
|
# Update the entire display
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def keyboard_movement(paddle):
|
||||||
|
for keyboard_event in pygame.event.get():
|
||||||
|
if keyboard_event.type == pygame.KEYDOWN:
|
||||||
|
if keyboard_event.key == pygame.K_DOWN:
|
||||||
|
paddle.moving_down = True
|
||||||
|
if keyboard_event.key == pygame.K_UP:
|
||||||
|
paddle.moving_up = True
|
||||||
|
if keyboard_event.type == pygame.KEYUP:
|
||||||
|
if keyboard_event.key == pygame.K_DOWN:
|
||||||
|
paddle.moving_down = False
|
||||||
|
if keyboard_event.key == pygame.K_UP:
|
||||||
|
paddle.moving_up = False
|
||||||
|
|
||||||
|
# Create the two paddles
|
||||||
|
host_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
|
||||||
|
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:
|
||||||
|
clock.tick(GAME_FPS)
|
||||||
|
draw()
|
||||||
|
|
||||||
|
if is_host:
|
||||||
|
keyboard_movement(host_player)
|
||||||
|
else:
|
||||||
|
keyboard_movement(client_player)
|
||||||
|
|
||||||
|
# Bounce ball off of paddles
|
||||||
|
if pygame.sprite.spritecollideany(ball, paddle_sprites):
|
||||||
|
ball.x_vel *= -1
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""
|
||||||
|
Calls the game() function to start the game.
|
||||||
|
"""
|
||||||
|
game()
|
||||||
|
|
||||||
|
pygame.quit()
|
Loading…
Reference in New Issue
Block a user