generated from mwc/project_game
@chris
This commit is contained in:
BIN
__pycache__/catcher.cpython-311.pyc
Normal file
BIN
__pycache__/catcher.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/fruit.cpython-311.pyc
Normal file
BIN
__pycache__/fruit.cpython-311.pyc
Normal file
Binary file not shown.
48
catcher.py
Normal file
48
catcher.py
Normal file
@@ -0,0 +1,48 @@
|
||||
class CatcherPiece:
|
||||
character = "-"
|
||||
color = "white_on_indigo"
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
class Catcher:
|
||||
width = 7
|
||||
display = False
|
||||
pieces = []
|
||||
name = "Player"
|
||||
color = "white_on_indigo"
|
||||
|
||||
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 = CatcherPiece((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)
|
||||
|
||||
def can_move(self, position, game):
|
||||
on_board = game.on_board(position)
|
||||
empty = game.is_empty(position)
|
||||
70
fruit.py
Normal file
70
fruit.py
Normal file
@@ -0,0 +1,70 @@
|
||||
class FruitPiece:
|
||||
character = "@"
|
||||
color = "green_on_indigo"
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
class Fruit:
|
||||
width = 2
|
||||
height = 2
|
||||
display = False
|
||||
pieces = []
|
||||
character = "@"
|
||||
color = "green_on_indigo"
|
||||
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
def play_turn(self, game):
|
||||
if not self.pieces:
|
||||
self.create_pieces(game)
|
||||
if game.turn_number % 2 == 0:
|
||||
x, y = self.position
|
||||
if y == 24:
|
||||
game.remove_agent(self)
|
||||
else:
|
||||
catcher = game.get_agent_by_name('catcher')
|
||||
new_position = (x, y + 1)
|
||||
if new_position == catcher.position:
|
||||
catcher.explode()
|
||||
game.end()
|
||||
else:
|
||||
self.position = new_position
|
||||
|
||||
def create_pieces(self, game):
|
||||
x, y = self.position
|
||||
self.pieces = []
|
||||
for i in range(self.width):
|
||||
piece = FruitPiece((x + i, y))
|
||||
self.pieces.append(piece)
|
||||
game.add_agent(piece)
|
||||
for i in range(self.height):
|
||||
piece = FruitPiece((x, y + i))
|
||||
self.pieces.append(piece)
|
||||
game.add_agent(piece)
|
||||
|
||||
def update_piece_positions(self):
|
||||
if game.turn_number % 2 == 0:
|
||||
self.set_color()
|
||||
x, y = self.position
|
||||
if y == HEIGHT - 1:
|
||||
game.remove_agent(self)
|
||||
else:
|
||||
ship = game.get_agent_by_name('ship')
|
||||
new_position = (x, y + 1)
|
||||
if new_position == ship.position:
|
||||
ship.explode()
|
||||
game.end()
|
||||
else:
|
||||
self.position = new_position
|
||||
|
||||
class FruitSpawner:
|
||||
display = False
|
||||
|
||||
def play_turn(self, game):
|
||||
if self.should_spawn_fruit(game.turn_number):
|
||||
asteroid = Fruit((randint(0, WIDTH - 1), 0))
|
||||
game.add_agent(fruit)
|
||||
|
||||
def should_spawn_fruit(self, turn_number):
|
||||
return randint(0, 1000) < turn_number
|
||||
40
game.py
40
game.py
@@ -1,32 +1,20 @@
|
||||
from random import randint
|
||||
from retro.game import Game
|
||||
from retro.graph import Graph
|
||||
from catcher import Catcher
|
||||
from fruit import Fruit
|
||||
|
||||
class Agent:
|
||||
RIGHT = (1, 0)
|
||||
UP = (0, -1)
|
||||
LEFT = (-1, 0)
|
||||
DOWN = (0, 1)
|
||||
character = ">"
|
||||
direction = RIGHT
|
||||
position = (15, 0)
|
||||
name = "agent"
|
||||
color = "white_on_black"
|
||||
display = True
|
||||
g = Graph()
|
||||
g.get_or_create_edge(26, 10, 26, 22)
|
||||
agents = g.get_agents()
|
||||
for agent in agents:
|
||||
agent.color = "white_on_indigo"
|
||||
|
||||
def handle_keystroke(self, keystroke, game):
|
||||
x, y = self.position
|
||||
if keystroke.name == "M":
|
||||
self.direction = self.RIGHT
|
||||
self.character = '>'
|
||||
elif keystroke.name == "I":
|
||||
self.direction = self.LEFT
|
||||
self.character = '<'
|
||||
agents = [
|
||||
Catcher((14, 24)),
|
||||
Fruit((14, 2)),
|
||||
]
|
||||
|
||||
def can_move(self, position, game):
|
||||
on_board = game.on_board(position)
|
||||
empty = game.is_empty(position)
|
||||
|
||||
if __name__ == '__main__':
|
||||
player = Agent()
|
||||
game = Game([player], {'score': 0}, board_size:=(24, 20), framerate:=12)
|
||||
state = {'Score': 0}
|
||||
game = Game(agents, state, board_size=(35, 25), framerate=24, color="white_on_indigo")
|
||||
game.play()
|
||||
Reference in New Issue
Block a user