generated from mwc/project_game
I decided to have the ball check for collision with the paddle instead of having the paddle check for collision with the ball. Either way could work, but it seemed easier to have the ball do the checking since the ball will need to respond with a change in velocity anyway.
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
# paddle.py
|
|
class PaddlePiece:
|
|
character = "X"
|
|
def __init__(self, position):
|
|
self.position = position
|
|
|
|
class Paddle:
|
|
width = 5
|
|
display = False
|
|
pieces = []
|
|
|
|
def __init__(self, position):
|
|
self.position = position
|
|
|
|
def play_turn(self, game):
|
|
if not self.pieces:
|
|
self.create_pieces(game)
|
|
|
|
def handle_keystroke(self, keystroke, game):
|
|
x, y = self.position
|
|
width, height = game.board_size
|
|
if keystroke.name == "KEY_LEFT":
|
|
if 0 < x:
|
|
self.position = (x-1, y)
|
|
self.update_piece_positions()
|
|
if keystroke.name == "KEY_RIGHT":
|
|
if x + self.width < width:
|
|
self.position = (x+1, y)
|
|
self.update_piece_positions()
|
|
|
|
def create_pieces(self, game):
|
|
x, y = self.position
|
|
self.pieces = []
|
|
for i in range(self.width):
|
|
piece = PaddlePiece((x + i, y))
|
|
self.pieces.append(piece)
|
|
game.add_agent(piece)
|
|
|
|
def update_piece_positions(self):
|
|
x, y = self.position
|
|
for i, piece in enumerate(self.pieces):
|
|
piece.position = (x + i, y)
|