@chris My fruit offset does not work

This commit is contained in:
kdang
2026-01-09 09:49:15 -05:00
parent d6adfef5a1
commit 267fffa9b0
9 changed files with 69 additions and 54 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,11 +1,18 @@
from random import randint
class CatcherPiece: class CatcherPiece:
character = "-" character = "-"
color = "white_on_indigo" color = "white_on_indigo"
def __init__(self, position): def __init__(self, position):
self.position = position self.position = position
def play_turn(self, game):
fruit = game.get_agent_by_name("fruit")
if new_position == fruit.position:
game.state['Score'] += 1
class Catcher: class Catcher:
width = 7 width = 6
display = False display = False
pieces = [] pieces = []
name = "catcher" name = "catcher"

View File

@@ -1,71 +1,66 @@
from random import randint
SHAPE_DEFINITIONS = [
[(0,0)],
[(0, 0), (1, 0), (0, 1), (1, 1)],
]
class FruitPiece: class FruitPiece:
character = "@" character = "@"
color = "green_on_indigo" color = "green_on_indigo"
display = True
def __init__(self, position): def __init__(self, position):
self.position = position self.position = position
class Fruit: class Fruit:
width = 2 width = 2
height = 2 height = 1
display = False display = False
pieces = [] pieces = []
name = "fruit"
character = "@" character = "@"
color = "green_on_indigo" color = "green_on_indigo"
def __init__(self, position): def __init__(self, position, game, shape_offsets):
self.position = position self.position = position
self.blocks = {}
for shape in shape_offsets:
self.create_shape(game, offset)
def play_turn(self, game): def play_turn(self, game):
if not self.pieces: if game.turn_number % 3 == 0:
self.create_pieces(game)
if game.turn_number % 2 == 0:
x, y = self.position x, y = self.position
if y == 24: if y == 29:
game.remove_agent(self) game.remove_agent(self)
game.state['Score'] -= 3
else: else:
catcher = game.get_agent_by_name("Player") catcher = game.get_agent_by_name("catcher")
new_position = (x, y + 1) new_position = (x, y + 1)
if new_position == catcher.position: 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) game.remove_agent(self)
else: game.state['Score'] += 1
ship = game.get_agent_by_name("Player")
new_position = (x, y - 1)
if new_position == ship.position:
ship.explode()
game.end()
else: else:
self.position = new_position self.position = new_position
def create_shape(self, game, offset):
class FruitSpawner: x, y = self.position
display = False ox, oy = offset
fruit = Fruit((x + ox, y + oy))
def play_turn(self, game): self.fruits[offset] = fruit
if self.should_spawn_fruit(game.turn_number):
asteroid = Fruit((randint(0, WIDTH - 1), 0))
game.add_agent(fruit) game.add_agent(fruit)
def should_spawn_fruit(self, turn_number): def update_piece_positions(self):
return randint(0, 1000) < turn_number if game.turn_number % 3 == 0:
self.set_color()
x, y = self.position
if y == 29:
game.remove_agent(self)
game.state['Score'] -= 3
else:
catcher = game.get_agent_by_name("catcher")
new_position = (x, y + 1)
if new_position == catcher.position:
game.remove_agent(self)
game.state['Score'] += 1
else:
self.position = new_position

16
game.py
View File

@@ -1,20 +1,16 @@
from random import randint from random import randint
from retro.game import Game from retro.game import Game
from retro.graph import Graph
from catcher import Catcher from catcher import Catcher
from fruit import Fruit from manager import FruitManager
g = Graph() WIDTH = 27
g.get_or_create_edge(26, 10, 26, 22) HEIGHT = 30
agents = g.get_agents()
for agent in agents:
agent.color = "white_on_indigo"
agents = [ agents = [
Catcher((14, 24)), Catcher((11, 29)),
Fruit((14, 2)), FruitManager(),
] ]
state = {'Score': 0} state = {'Score': 0}
game = Game(agents, state, board_size=(35, 25), framerate=24, color="white_on_indigo") game = Game(agents, state, board_size=(WIDTH, HEIGHT), framerate=24, color="white_on_indigo")
game.play() game.play()

17
manager.py Normal file
View File

@@ -0,0 +1,17 @@
from fruit import Fruit, SHAPE_DEFINITIONS
from random import choice
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):
fruit = Fruit((14, 1), game, choice(SHAPE_DEFINITIONS))
game.add_agent(fruit)

2
nav.py
View File

@@ -2,7 +2,7 @@ from random import randint
from retro.game import Game from retro.game import Game
HEIGHT = 25 HEIGHT = 25
WIDTH = 2 WIDTH = 4
class Spaceship: class Spaceship:
"""A player-controlled agent which moves left and right, dodging asteroids. """A player-controlled agent which moves left and right, dodging asteroids.

6
poetry.lock generated
View File

@@ -50,14 +50,14 @@ ansicon = {version = "*", markers = "platform_system == \"Windows\""}
[[package]] [[package]]
name = "retro-games" name = "retro-games"
version = "1.1.1" version = "1.1.3"
description = "A simple framework for Terminal-based games" description = "A simple framework for Terminal-based games"
optional = false optional = false
python-versions = "<4.0,>=3.10" python-versions = "<4.0,>=3.10"
groups = ["main"] groups = ["main"]
files = [ files = [
{file = "retro_games-1.1.1-py3-none-any.whl", hash = "sha256:2b2eac8c2667c69f1dd90c083a0f58b948e6fdecb184720133b819fa78f8a57f"}, {file = "retro_games-1.1.3-py3-none-any.whl", hash = "sha256:4bdd27241b5cb3ee72e69a042d301ff58df2a2ade7e3c29400a538fa54e30148"},
{file = "retro_games-1.1.1.tar.gz", hash = "sha256:67ce475191f78d13148028de17b97de099226c4c581d5cf811cd9dfc7f5bb420"}, {file = "retro_games-1.1.3.tar.gz", hash = "sha256:4f91ff725e551820aa4e30c12c0264e2da41967ed34252122b7136bc2a8ed311"},
] ]
[package.dependencies] [package.dependencies]