generated from mwc/project_game
Add features--multiple fruits
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
fruit_catcher/__pycache__
|
fruit_catcher/__pycache__
|
||||||
|
result.json
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from random import randint
|
|||||||
class CatcherPiece:
|
class CatcherPiece:
|
||||||
character = "-"
|
character = "-"
|
||||||
color = "white_on_indigo"
|
color = "white_on_indigo"
|
||||||
|
z = 1
|
||||||
def __init__(self, position):
|
def __init__(self, position):
|
||||||
self.position = position
|
self.position = position
|
||||||
|
|
||||||
@@ -24,12 +25,12 @@ class Catcher:
|
|||||||
x, y = self.position
|
x, y = self.position
|
||||||
width, height = game.board_size
|
width, height = game.board_size
|
||||||
if keystroke.name == "KEY_LEFT":
|
if keystroke.name == "KEY_LEFT":
|
||||||
if 0 < x:
|
new_x = max(0, x - 3)
|
||||||
self.position = (x-1, y)
|
self.position = (new_x, y)
|
||||||
self.update_piece_positions()
|
self.update_piece_positions()
|
||||||
if keystroke.name == "KEY_RIGHT":
|
if keystroke.name == "KEY_RIGHT":
|
||||||
if x + self.width < width:
|
new_x = min(width - self.width, x + 3)
|
||||||
self.position = (x+1, y)
|
self.position = (new_x, y)
|
||||||
self.update_piece_positions()
|
self.update_piece_positions()
|
||||||
|
|
||||||
def create_pieces(self, game):
|
def create_pieces(self, game):
|
||||||
|
|||||||
@@ -1,49 +1,72 @@
|
|||||||
from random import randint
|
from random import randint, choice
|
||||||
|
|
||||||
SHAPE_DEFINITIONS = [
|
FRUIT_TYPES = [
|
||||||
[(0,0)],
|
{'shape': [(0, 0)], 'color': 'green_on_indigo'},
|
||||||
[(0, 0), (1, 0), (0, 1), (1, 1)],
|
{'shape': [(0, 0), (1, 0)], 'color': 'yellow_on_indigo'},
|
||||||
|
{'shape': [(0, 0), (1, 0), (2, 0)], 'color': 'red_on_indigo'},
|
||||||
|
{'shape': [(0, 0), (0, 1)], 'color': 'cyan_on_indigo'},
|
||||||
|
{'shape': [(0, 0), (1, 0), (0, 1), (1, 1)], 'color': 'magenta_on_indigo'},
|
||||||
|
{'shape': [(0, 0), (1, 1), (2, 0)], 'color': 'white_on_indigo'},
|
||||||
]
|
]
|
||||||
|
|
||||||
class FruitPiece:
|
class FruitPiece:
|
||||||
character = "@"
|
character = "@"
|
||||||
color = "green_on_indigo"
|
|
||||||
display = True
|
display = True
|
||||||
def __init__(self, position):
|
|
||||||
|
def __init__(self, position, color, z):
|
||||||
self.position = position
|
self.position = position
|
||||||
|
self.color = color
|
||||||
|
self.z = z
|
||||||
|
|
||||||
class Fruit:
|
class Fruit:
|
||||||
width = 2
|
|
||||||
height = 1
|
|
||||||
display = False
|
display = False
|
||||||
pieces = []
|
|
||||||
name = "fruit"
|
|
||||||
character = "@"
|
character = "@"
|
||||||
color = "green_on_indigo"
|
|
||||||
|
|
||||||
def __init__(self, position, game, shape_offsets):
|
def __init__(self, position, game, shape_offsets, color, dx, speed, start_z):
|
||||||
self.position = position
|
self.position = position
|
||||||
self.pieces = {}
|
self.pieces = {}
|
||||||
for offset in shape_offsets:
|
self.color = color
|
||||||
self.create_shape(game, offset)
|
self.dx = dx
|
||||||
|
self.speed = speed
|
||||||
|
self.alive = True
|
||||||
|
for i, offset in enumerate(shape_offsets):
|
||||||
|
self.create_shape(game, offset, start_z + i)
|
||||||
|
|
||||||
def play_turn(self, game):
|
def play_turn(self, game):
|
||||||
if game.turn_number % 3 == 0:
|
if game.turn_number % self.speed == 0:
|
||||||
|
self.move(game)
|
||||||
|
|
||||||
|
def move(self, game):
|
||||||
x, y = self.position
|
x, y = self.position
|
||||||
if y >= 29:
|
width, height = game.board_size
|
||||||
|
offsets = list(self.pieces.keys())
|
||||||
|
max_ox = max(ox for ox, oy in offsets)
|
||||||
|
min_ox = min(ox for ox, oy in offsets)
|
||||||
|
max_oy = max(oy for ox, oy in offsets)
|
||||||
|
|
||||||
|
new_x = x + self.dx
|
||||||
|
if new_x + min_ox < 0 or new_x + max_ox >= width:
|
||||||
|
self.dx = -self.dx
|
||||||
|
new_x = x + self.dx
|
||||||
|
|
||||||
|
new_y = y + 1
|
||||||
|
if new_y + max_oy >= height:
|
||||||
for piece in self.pieces.values():
|
for piece in self.pieces.values():
|
||||||
game.remove_agent(piece)
|
game.remove_agent(piece)
|
||||||
game.remove_agent(self)
|
game.remove_agent(self)
|
||||||
|
self.alive = False
|
||||||
|
game.state['Lives'] -= 1
|
||||||
|
if game.state['Lives'] <= 0:
|
||||||
game.end()
|
game.end()
|
||||||
else:
|
else:
|
||||||
self.position = (x, y + 1)
|
self.position = (new_x, new_y)
|
||||||
self.update_piece_positions()
|
self.update_piece_positions()
|
||||||
self.check_catcher_collision(game)
|
self.check_catcher_collision(game)
|
||||||
|
|
||||||
def create_shape(self, game, offset):
|
def create_shape(self, game, offset, z):
|
||||||
x, y = self.position
|
x, y = self.position
|
||||||
ox, oy = offset
|
ox, oy = offset
|
||||||
piece = FruitPiece((x + ox, y + oy))
|
piece = FruitPiece((x + ox, y + oy), self.color, z)
|
||||||
self.pieces[offset] = piece
|
self.pieces[offset] = piece
|
||||||
game.add_agent(piece)
|
game.add_agent(piece)
|
||||||
|
|
||||||
@@ -62,4 +85,5 @@ class Fruit:
|
|||||||
for p in self.pieces.values():
|
for p in self.pieces.values():
|
||||||
game.remove_agent(p)
|
game.remove_agent(p)
|
||||||
game.remove_agent(self)
|
game.remove_agent(self)
|
||||||
|
self.alive = False
|
||||||
return
|
return
|
||||||
@@ -12,6 +12,6 @@ def play():
|
|||||||
Catcher((11, 29)),
|
Catcher((11, 29)),
|
||||||
FruitManager(),
|
FruitManager(),
|
||||||
]
|
]
|
||||||
state = {'Score': 0}
|
state = {'Score': 0, 'Lives': 5}
|
||||||
game = Game(agents, state, board_size=(WIDTH, HEIGHT), framerate=24, color="white_on_indigo", dump_state="result.json")
|
game = Game(agents, state, board_size=(WIDTH, HEIGHT), framerate=24, color="white_on_indigo", dump_state="result.json")
|
||||||
game.play()
|
game.play()
|
||||||
|
|||||||
@@ -1,18 +1,31 @@
|
|||||||
from .fruit import Fruit, SHAPE_DEFINITIONS
|
from .fruit import Fruit, FRUIT_TYPES
|
||||||
from random import choice, randint
|
from random import choice, randint
|
||||||
from retro.errors import AgentNotFoundByName
|
|
||||||
|
|
||||||
class FruitManager:
|
class FruitManager:
|
||||||
|
|
||||||
display = False
|
display = False
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.active_fruits = []
|
||||||
|
self.last_spawn_turn = 0
|
||||||
|
self.next_z = 0
|
||||||
|
|
||||||
def play_turn(self, game):
|
def play_turn(self, game):
|
||||||
try:
|
self.active_fruits = [f for f in self.active_fruits if f.alive]
|
||||||
game.get_agent_by_name("fruit")
|
spawn_interval = max(12, 72 - game.turn_number // 10)
|
||||||
except AgentNotFoundByName:
|
if game.turn_number - self.last_spawn_turn >= spawn_interval:
|
||||||
self.create_piece(game)
|
self.create_piece(game)
|
||||||
|
self.last_spawn_turn = game.turn_number
|
||||||
|
|
||||||
def create_piece(self, game):
|
def create_piece(self, game):
|
||||||
x = randint(0, 25)
|
width, _ = game.board_size
|
||||||
fruit = Fruit((x, 1), game, choice(SHAPE_DEFINITIONS))
|
fruit_type = choice(FRUIT_TYPES)
|
||||||
|
shape = fruit_type['shape']
|
||||||
|
max_ox = max(ox for ox, oy in shape)
|
||||||
|
x = randint(0, width - 1 - max_ox)
|
||||||
|
dx = choice([-1, 0, 0, 1])
|
||||||
|
speed = randint(2, 5)
|
||||||
|
fruit = Fruit((x, 1), game, shape, fruit_type['color'], dx, speed, self.next_z)
|
||||||
|
self.next_z += len(shape)
|
||||||
game.add_agent(fruit)
|
game.add_agent(fruit)
|
||||||
|
self.active_fruits.append(fruit)
|
||||||
|
|||||||
Reference in New Issue
Block a user