generated from mwc/project_game
Fixing game to working state
This commit is contained in:
0
fruit_catcher/__init__.py
Normal file
0
fruit_catcher/__init__.py
Normal file
50
fruit_catcher/catcher.py
Normal file
50
fruit_catcher/catcher.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from random import randint
|
||||
|
||||
class CatcherPiece:
|
||||
character = "-"
|
||||
color = "white_on_indigo"
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
class Catcher:
|
||||
width = 6
|
||||
display = False
|
||||
pieces = []
|
||||
name = "catcher"
|
||||
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)
|
||||
65
fruit_catcher/fruit.py
Normal file
65
fruit_catcher/fruit.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from random import randint
|
||||
|
||||
SHAPE_DEFINITIONS = [
|
||||
[(0,0)],
|
||||
[(0, 0), (1, 0), (0, 1), (1, 1)],
|
||||
]
|
||||
|
||||
class FruitPiece:
|
||||
character = "@"
|
||||
color = "green_on_indigo"
|
||||
display = True
|
||||
def __init__(self, position):
|
||||
self.position = position
|
||||
|
||||
class Fruit:
|
||||
width = 2
|
||||
height = 1
|
||||
display = False
|
||||
pieces = []
|
||||
name = "fruit"
|
||||
character = "@"
|
||||
color = "green_on_indigo"
|
||||
|
||||
def __init__(self, position, game, shape_offsets):
|
||||
self.position = position
|
||||
self.pieces = {}
|
||||
for offset in shape_offsets:
|
||||
self.create_shape(game, offset)
|
||||
|
||||
def play_turn(self, game):
|
||||
if game.turn_number % 3 == 0:
|
||||
x, y = self.position
|
||||
if y >= 29:
|
||||
for piece in self.pieces.values():
|
||||
game.remove_agent(piece)
|
||||
game.remove_agent(self)
|
||||
game.end()
|
||||
else:
|
||||
self.position = (x, y + 1)
|
||||
self.update_piece_positions()
|
||||
self.check_catcher_collision(game)
|
||||
|
||||
def create_shape(self, game, offset):
|
||||
x, y = self.position
|
||||
ox, oy = offset
|
||||
piece = FruitPiece((x + ox, y + oy))
|
||||
self.pieces[offset] = piece
|
||||
game.add_agent(piece)
|
||||
|
||||
def update_piece_positions(self):
|
||||
x, y = self.position
|
||||
for offset, piece in self.pieces.items():
|
||||
ox, oy = offset
|
||||
piece.position = (x + ox, y + oy)
|
||||
|
||||
def check_catcher_collision(self, game):
|
||||
catcher = game.get_agent_by_name("catcher")
|
||||
catcher_positions = {p.position for p in catcher.pieces}
|
||||
for piece in self.pieces.values():
|
||||
if piece.position in catcher_positions:
|
||||
game.state['Score'] += 1
|
||||
for p in self.pieces.values():
|
||||
game.remove_agent(p)
|
||||
game.remove_agent(self)
|
||||
return
|
||||
17
fruit_catcher/game.py
Normal file
17
fruit_catcher/game.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from random import randint
|
||||
from retro.game import Game
|
||||
from .catcher import Catcher
|
||||
from .manager import FruitManager
|
||||
import json
|
||||
|
||||
WIDTH = 27
|
||||
HEIGHT = 30
|
||||
|
||||
def play():
|
||||
agents = [
|
||||
Catcher((11, 29)),
|
||||
FruitManager(),
|
||||
]
|
||||
state = {'Score': 0}
|
||||
game = Game(agents, state, board_size=(WIDTH, HEIGHT), framerate=24, color="white_on_indigo", dump_state="result.json")
|
||||
game.play()
|
||||
18
fruit_catcher/manager.py
Normal file
18
fruit_catcher/manager.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from .fruit import Fruit, SHAPE_DEFINITIONS
|
||||
from random import choice, randint
|
||||
from retro.errors import AgentNotFoundByName
|
||||
|
||||
class FruitManager:
|
||||
|
||||
display = False
|
||||
|
||||
def play_turn(self, game):
|
||||
try:
|
||||
game.get_agent_by_name("fruit")
|
||||
except AgentNotFoundByName:
|
||||
self.create_piece(game)
|
||||
|
||||
def create_piece(self, game):
|
||||
x = randint(0, 25)
|
||||
fruit = Fruit((x, 1), game, choice(SHAPE_DEFINITIONS))
|
||||
game.add_agent(fruit)
|
||||
Reference in New Issue
Block a user