【发布时间】:2015-02-21 03:21:24
【问题描述】:
Python 版本:2.7.8
目标:使用 PyGame 库在 python 上制作扫雷游戏(至少尝试)。
代码:
import pygame, random, sys
from pygame.locals import *
pygame.init()
width, height = 400, 400
clock = pygame.time.Clock()
DRAWSURF = pygame.display.set_mode((width, height))
pygame.display.set_caption("Matt's Minesweeper")
background = pygame.Surface(DRAWSURF.get_size())
background.fill((255, 255, 255))
DRAWSURF.blit(background, (0, 0))
pygame.display.flip()
board = []
class Square():
isMine = None
val = 0
count = 0
def draw(self):
BLACK = (0, 0, 0)
val = self.val
count = self.count
x = 100 + val * 60
y = 0 + 60 * count
pygame.draw.rect(DRAWSURF, BLACK, (x, y, 60, 60), 5)
return self.isMine
class DrawBoard():
def draw(self, grid):
item = Square()
for i in range(0, grid):
item.val = i
select = item.draw()
board.append(select)
for j in range(grid):
item.count = j
select_2 = item.draw()
board.append(select_2)
class MineSet():
temp = Square()
def mineSet(self, mines):
temp = self.temp
for i in range(0, mines):
test = random.choice(board)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
clock.tick(30)
# Insert drawings here
game = DrawBoard()
game.draw(5)
print board
pygame.display.update()
问题:我将每个单独的正方形作为自己的对象,如类 Square() 中所示。在 Square 类的绘图函数中,它返回一个布尔值 None(变量 isMine),当我稍后在 DrawBoard 类中调用它时,它会将对象附加到列表“板”中。要为游戏分配地雷,我想随机选择一个创建的 Square 对象并将布尔值从 None 更改为 True。也许这不是分配地雷的最佳方式,但我正在努力做到最好。任何帮助表示赞赏。
【问题讨论】:
-
所以你有一个布尔值列表并想随机选择一个?
-
可能更容易创建一个字典映射位置作为地雷是否爆炸的关键作为布尔值
-
你也永远不会在 MineSet 中使用
temp或test -
我有一个布尔值列表,我需要随机选择一定数量的(地雷参数)并将其替换为不同的值
-
select = item.draw()应该做什么?该方法不返回任何内容(默认情况下为 None),因此您将 select 设置为 None
标签: python boolean pygame minesweeper