added keyboard movement depending on if player is the host or not
This commit is contained in:
parent
5ce89d29ee
commit
0ad5a3e97c
@ -2,24 +2,36 @@ import pygame
|
|||||||
import pypong
|
import pypong
|
||||||
|
|
||||||
SCREEN_SIZE = (800, 600) # Size of the game window
|
SCREEN_SIZE = (800, 600) # Size of the game window
|
||||||
GAME_FPS = 30
|
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
|
||||||
|
|
||||||
|
|
||||||
class Paddle(pygame.sprite.Sprite):
|
class Paddle(pygame.sprite.Sprite):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
self.moving_down = False
|
||||||
|
self.moving_up = False
|
||||||
|
|
||||||
self.image = pygame.Surface(PADDLE_SIZE)
|
self.image = pygame.Surface(PADDLE_SIZE)
|
||||||
self.image.fill(GAME_COLOR)
|
self.image.fill(GAME_COLOR)
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
pass
|
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]
|
||||||
|
|
||||||
|
|
||||||
def game(isHost=True):
|
def game(is_host=False):
|
||||||
"""
|
"""
|
||||||
Starts the game.
|
Starts the game.
|
||||||
"""
|
"""
|
||||||
@ -60,16 +72,34 @@ def game(isHost=True):
|
|||||||
# Update the entire display
|
# Update the entire display
|
||||||
pygame.display.flip()
|
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
|
# Create the two paddles
|
||||||
host_player = Paddle()
|
host_player = Paddle()
|
||||||
client_player = Paddle()
|
client_player = Paddle()
|
||||||
client_player.rect.x = SCREEN_SIZE[0] - client_player.rect.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)
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
clock.tick(GAME_FPS)
|
clock.tick(GAME_FPS)
|
||||||
draw()
|
draw()
|
||||||
|
|
||||||
|
if is_host:
|
||||||
|
keyboard_movement(host_player)
|
||||||
|
else:
|
||||||
|
keyboard_movement(client_player)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user