我正在做一个清扫器游戏,这是整个代码:https://codeshare.io/5zxyxw
我正在尝试在第82行(从整个代码)上进行其他代码来制作这项工作:
g=class playGame(object):
"""Make the game play"""
def __init__(self, play_game):
def play_game(self, play_game):
self.play_game = play_game
grid_size = int(input("Choose the Width of the board: "))
num = int(input("Choose the number of mines: "))
self.mines = mines
mines = place_mines(grid, mines)
grid1 = grid(grid_size, self.mines)
while not gameOver:
print(grid)
print("Make your move:")
x = int(input("x: "))
y = int(input("y: "))
grid.makeMove(x, y)
gameOver = grid.hitMine(x, y)
if grid.isWinner() and gameOver == False:
gameOver = True
winner = True
print(grid)
if winner:
print("Congratulations. You Win!")
else:
print("You hit a mine. Game Over!")
g.play_game()
我遇到的错误是我的 class 被标记为无效的语法,因此我发现了以下内容:创建类实例class
时,我发现了这一点:,但据我所知,play_game()
是完全向右的。即使我添加了一个文档字符串,它仍然抱怨。
所以我找到了这个名字:名称"游戏"不是定义的,但是,但是,我真的要服用 self.play_game=play_game
班级外???
okey现在看起来像这样:
class playGame(object):
def __init__(self, play_game):
def play_game(self, play_game):
#self.play_game = play_game
grid_size = int(input("Choose the Width of the board: "))
num = int(input("Choose the number of mines: "))
self.mines = mines
mines = place_mines(grid, mines)
grid1 = grid(grid_size, self.mines)
while not gameOver:
print(grid)
print("Make your move:")
x = int(input("x: "))
y = int(input("y: "))
grid.makeMove(x, y)
gameOver = grid.hitMine(x, y)
if grid.isWinner() and gameOver == False:
gameOver = True
winner = True
print(grid)
if winner:
print("Congratulations. You Win!")
else:
print("You hit a mine. Game Over!")
self.play_game = play_game
play_game()
然后我得到了:
self.play_game = play_game
NameError: name 'play_game' is not defined
注意!我选择在g = class playGame()
g
因此,无效的语法消失了。(即使我不同意这是答案,但是)
-
所以我不知道在这里做什么。我想让它起作用。如果应该正确运行,则应:
问我网格的大小。
问我网格中有多少矿。
玩。
结束。
您肯定在基础知识上挣扎。第二次尝试很好,但是:
- 您必须删除嵌套函数 PLAY_GAME 来自 __ INIT __ INT __ INT __ 函数。
- 更改对象初始化代码。
class PlayGame(object): # class names should start with big letter
def play_game(self):
grid_size = int(input("Choose the Width of the board: "))
num = int(input("Choose the number of mines: "))
self.mines = mines
mines = place_mines(grid, mines)
grid1 = grid(grid_size, self.mines)
while not gameOver:
print(grid)
print("Make your move:")
x = int(input("x: "))
y = int(input("y: "))
grid.makeMove(x, y)
gameOver = grid.hitMine(x, y)
if grid.isWinner() and gameOver == False:
gameOver = True
winner = True
print(grid)
if winner:
print("Congratulations. You Win!")
else:
print("You hit a mine. Game Over!")
game = PlayGame() # this line would call your __init__ function
game.play_game()
您需要实例化类的对象。使用更新的代码,定义了类后,下一步该怎么办?显然,不能只是让它坐在那里,您必须将对象(变量)实例化。因此,您已经定义了类playGame
# Instantiate it to whichever variable/object here
myInstance = PlayGame()
这将实例化游戏类并将其分配给对象myInstance
。def __init__
中定义的所有内容也将执行。
# Then execute any function(s) outside of `__init__`
# With your code, since everything is inside of `__init__`, you don't need anything else
# With Dawid's code, you need another line like so:
myInstance.play_game()