generated from mwc/project_game
Okay... I don't know why it doesn't work when
the mines can be hidden, so I have a janky workaround. You might have to try moving the cursor multiple times but at least now it actually works. Next step, add flagging!!!!
This commit is contained in:
parent
75618c1de1
commit
8e54c5fe12
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -26,4 +26,8 @@ class Cursor:
|
|||
if game.is_empty(new_position):
|
||||
self.position = new_position
|
||||
game.log("The cursor is at " + str(self.position))
|
||||
for i in range(10):
|
||||
game.log("mine"+str(i) + " is located at " + str(game.get_agent_by_name("mine"+str(i)).position))
|
||||
|
||||
def hide(self):
|
||||
pass
|
|
@ -17,21 +17,25 @@ class FreeSpace:
|
|||
def handle_keystroke(self, keystroke, game):
|
||||
if keystroke.name in ("KEY_ENTER"):
|
||||
if not game.is_empty(self.position):
|
||||
board_width,board_height = game.board_size
|
||||
for i in range(board_width):
|
||||
for j in range(board_height):
|
||||
try:
|
||||
game.get_agent_by_name("freespace"+str(i)+str(j)).show()
|
||||
except:
|
||||
pass
|
||||
self.reveal(game)
|
||||
for i in range(board_width):
|
||||
for j in range(board_height):
|
||||
try:
|
||||
game.get_agent_by_name("freespace"+str(i)+str(j)).hide()
|
||||
except:
|
||||
pass
|
||||
|
||||
if self.revealed == False:
|
||||
board_width,board_height = game.board_size
|
||||
for i in range(10):
|
||||
game.get_agent_by_name("mine"+str(i)).show()
|
||||
for i in range(board_width):
|
||||
for j in range(board_height):
|
||||
try:
|
||||
game.get_agent_by_name("freespace"+str(i)+str(j)).show()
|
||||
except:
|
||||
pass
|
||||
self.reveal(game)
|
||||
for i in range(board_width):
|
||||
for j in range(board_height):
|
||||
try:
|
||||
game.get_agent_by_name("freespace"+str(i)+str(j)).hide()
|
||||
except:
|
||||
pass
|
||||
for i in range(10):
|
||||
game.get_agent_by_name("mine"+str(i)).hide()
|
||||
|
||||
def hide(self):
|
||||
if self.revealed == False:
|
||||
|
@ -87,7 +91,6 @@ class FreeSpace:
|
|||
self.character = str(self.neighbors)
|
||||
|
||||
def reveal(self,game):
|
||||
game.log("Revealing " + str(self.position))
|
||||
self.revealed = True
|
||||
if self.neighbors == 0:
|
||||
if game.on_board((self.width + 1,self.height)):
|
||||
|
@ -121,4 +124,4 @@ class FreeSpace:
|
|||
if game.on_board((self.width - 1,self.height - 1)):
|
||||
if len(game.get_agents_by_position()[(self.width - 1,self.height - 1)]) != 0:
|
||||
if not game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].revealed:
|
||||
game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].reveal(game)
|
||||
game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].reveal(game)
|
18
mine.py
18
mine.py
|
@ -4,7 +4,7 @@
|
|||
# This module defines a mine agent class. It ends the game if enter is pressed while the cursor is over it.
|
||||
|
||||
class Mine:
|
||||
character = '_'
|
||||
character = ''
|
||||
revealed = True
|
||||
|
||||
def __init__(self, position):
|
||||
|
@ -12,12 +12,18 @@ class Mine:
|
|||
|
||||
def handle_keystroke(self, keystroke, game):
|
||||
if keystroke.name in ("KEY_ENTER"):
|
||||
if not game.is_empty(self.position):
|
||||
game.log("You hit a mine at " + str(self.position) + ".")
|
||||
if game.get_agent_by_name("cursor").position == self.position:
|
||||
game.log("You hit a mine! Game over.")
|
||||
game.end()
|
||||
|
||||
def play_turn(self, game):
|
||||
if (not game.is_empty(self.position)):
|
||||
self.display = False
|
||||
elif game.is_empty(self.position):
|
||||
self.display = True
|
||||
|
||||
def hide(self):
|
||||
self.display = False
|
||||
pass
|
||||
|
||||
def name_me(self, named):
|
||||
self.name = named
|
||||
|
@ -25,8 +31,8 @@ class Mine:
|
|||
def check_neighbors(self,game):
|
||||
pass
|
||||
|
||||
def reveal():
|
||||
def reveal(self):
|
||||
pass
|
||||
|
||||
def show():
|
||||
def show(self):
|
||||
pass
|
|
@ -4,10 +4,9 @@
|
|||
# This class implements a simple minesweeper game on a 9x9 grid.
|
||||
|
||||
from retro.game import Game
|
||||
from spawner import Spawner
|
||||
from spawner import Spawner
|
||||
|
||||
board_size = (9, 9)
|
||||
spawner = Spawner(board_size)
|
||||
# spawner = AsteroidSpawner(board_size)
|
||||
game = Game([spawner], {"score": 0}, board_size=board_size,debug=True)
|
||||
game.play()
|
24
spawner.py
24
spawner.py
|
@ -18,21 +18,22 @@ class Spawner:
|
|||
def play_turn(self, game):
|
||||
# On the first turn we will spawn everything.
|
||||
if game.turn_number == 1:
|
||||
# First spawn free spaces everywhere
|
||||
for i in range(self.board_width):
|
||||
for j in range(self.board_height):
|
||||
free_space = FreeSpace((i,j))
|
||||
free_space.name_me("freespace"+str(i)+str(j))
|
||||
game.add_agent(free_space)
|
||||
# Then spawn 10 mines
|
||||
# First spawn 10 mines
|
||||
for i in range(10):
|
||||
mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1))))
|
||||
mine.name_me("mine"+str(i))
|
||||
while not game.is_empty(mine.position):
|
||||
mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1))))
|
||||
mine.name_me("mine"+str(i))
|
||||
game.log(str(mine.name) + " is located at " + str(mine.position))
|
||||
# If there is a free space where a mine is going to be, remove the free space and then add the mine.
|
||||
if not game.is_empty(mine.position):
|
||||
game.remove_agent(game.get_agents_by_position()[mine.position][0])
|
||||
game.add_agent(mine)
|
||||
# Then spawn free spaces everywhere
|
||||
for i in range(self.board_width):
|
||||
for j in range(self.board_height):
|
||||
if game.is_empty((i,j)):
|
||||
free_space = FreeSpace((i,j))
|
||||
free_space.name_me("freespace"+str(i)+str(j))
|
||||
game.add_agent(free_space)
|
||||
# Now ask all free spaces to keep track of their neighbors that are mines
|
||||
# and all of the mines and free spaces to hide themselves.
|
||||
for i in range(self.board_width):
|
||||
|
@ -41,4 +42,5 @@ class Spawner:
|
|||
game.get_agents_by_position()[(i,j)][0].check_neighbors(game)
|
||||
game.get_agents_by_position()[(i,j)][0].hide()
|
||||
# Now create a cursor.
|
||||
game.add_agent(Cursor((self.board_width - 1, self.board_height - 1)))
|
||||
cursor = Cursor((self.board_width - 1, self.board_height - 1))
|
||||
game.add_agent(cursor)
|
||||
|
|
Loading…
Reference in New Issue