generated from mwc/project_game
at Chris: how could i get the ball too collide
with the paddle?
This commit is contained in:
BIN
__pycache__/ball.cpython-311.pyc
Normal file
BIN
__pycache__/ball.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/paddle.cpython-311.pyc
Normal file
BIN
__pycache__/paddle.cpython-311.pyc
Normal file
Binary file not shown.
20
ball.py
Normal file
20
ball.py
Normal file
@@ -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)
|
||||||
15
game.py
Normal file
15
game.py
Normal file
@@ -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()
|
||||||
|
|
||||||
47
paddle.py
Normal file
47
paddle.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user