diff --git a/__pycache__/ball.cpython-311.pyc b/__pycache__/ball.cpython-311.pyc new file mode 100644 index 0000000..fd8809c Binary files /dev/null and b/__pycache__/ball.cpython-311.pyc differ diff --git a/__pycache__/paddle.cpython-311.pyc b/__pycache__/paddle.cpython-311.pyc new file mode 100644 index 0000000..ed13cf7 Binary files /dev/null and b/__pycache__/paddle.cpython-311.pyc differ diff --git a/ball.py b/ball.py new file mode 100644 index 0000000..41b1f85 --- /dev/null +++ b/ball.py @@ -0,0 +1,20 @@ +# ball.py +class Ball: + character = "O" + + def __init__(self, position): + self.position = position + self.velocity = (1, 1) + + def play_turn(self, game): + x, y = self.position + vx, vy = self.velocity + new_x = x + vx + new_y = y + vy + board_x, board_y = game.board_size + if new_x < 0 or board_x <= new_x: + self.velocity = (-vx, vy) + elif new_y < 0 or board_y <= new_y: + self.velocity = (vx, -vy) + else: + self.position = (new_x, new_y) \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..171bb14 --- /dev/null +++ b/game.py @@ -0,0 +1,15 @@ +# game.py, version 2 +from retro.game import Game +from ball import Ball +from paddle import Paddle + +width = 40 +height = 40 + +ball = Ball((26, 20)) +paddle = Paddle((18, 38)) +agents = [ball, paddle] +state = {} +game = Game(agents, state, board_size=(width, height), debug=True) +game.play() + diff --git a/paddle.py b/paddle.py new file mode 100644 index 0000000..75d2e8b --- /dev/null +++ b/paddle.py @@ -0,0 +1,47 @@ +# 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) + check_collision() + + def check_collision: + x, y = self.position + print (retro.game.Game.get_agent_by_name(ball)) + + 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)