Python/PyGame Select 列表中的随机布尔值
Python/PyGame Select a Random Boolean From List
Python版本:2.7.8
Objective:使用 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()
问题:我将每个单独的正方形作为其自己的对象,如 class Square() 中所示。在 Square class 的绘图函数中,它 returns 一个 None 的布尔值(变量 isMine),当我稍后在 DrawBoard class 中调用它时,它将对象附加到列表 'board'。要为游戏分配地雷,我想随机 select 创建的 Square 对象之一,并将布尔值从 None 更改为 True。也许这不是分配地雷的最佳方式,但我正在努力做到最好。感谢任何帮助。
只是生成一个介于0
和len(list)
之间的随机值。然后您可以使用以下语法访问该值:list[random_val]
如果要更改值:list[random_val] = True
from collections import OrderedDict
from random import sample
class Square():
def __init__(self):
self.val = 0
self.count = 0
def draw(self):
BLACK = (0, 0, 0)
x = 100 + self.val * 60
y = 0 + 60 * self.count
pygame.draw.rect(DRAWSURF, BLACK, (x, y, 60, 60), 5)
class DrawBoard():
def __init__(self, grid, mines): # size of grid and how many mines to set
self.grid_size = grid
# create x,y coordinates and set all mains to False initially
self.board = OrderedDict(((i,j),False) for i in range(grid) for j in range(grid))
self.mines = mines
# pick random coords to place mines
samp = sample(list(self.board),self.mines)
for k in samp:
self.board[k]=True
def draw(self):
item = Square()
for i, j in self.board:
item.val, item.count = i,j
item.draw()
game = DrawBoard(5,5) # create board
game.draw() # draw
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
clock.tick(30)
# game logic goes here
pygame.display.update()
这应该可以帮助您入门,您需要让用户知道他们何时靠近矿井并处理 how/when 他们获胜:
import pygame, sys
from pygame.locals import *
pygame.init()
width, height = 40, 40
clock = pygame.time.Clock()
DRAWSURF = pygame.display.set_mode((width, height))
pygame.display.set_caption("Matt's Minesweeper")
background = pygame.Surface(DRAWSURF.get_size())
DRAWSURF.blit(background, (0, 0))
pygame.display.flip()
size = [255, 255]
screen = pygame.display.set_mode(size)
margin = 5
from collections import OrderedDict
from random import sample
class DrawBoard():
def __init__(self, grid, mines):
self.grid_size = grid
self.board = OrderedDict(((i, j), False) for i in range(grid) for j in range(grid))
self.mines = mines
samp = sample(list(self.board), self.mines)
for k in samp:
self.board[k] = True
def draw(self):
for i, j in self.board:
x = 100 + i * 60
y = 0 + 60 * j
pygame.draw.rect(DRAWSURF,(0,0,0), (x, y, 60, 60), margin)
game = DrawBoard(5, 5)
game.draw()
def game_over():
font = pygame.font.SysFont(None, 50)
text = font.render('Game over!', True, (255, 0, 0), (255, 255, 255))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery
screen.blit(text, text_rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
user_won = False
user_lost = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# get mouse pos
pos = pygame.mouse.get_pos()
# change to grid coordinates
column = pos[0] // (width + margin)
row = pos[1] // (height + margin)
print(game.board[row, column])
if game.board[row, column]:
user_lost = True
else:
game.board[row, column] = "picked"
if user_lost:
game_over()
screen.fill((0, 0, 0))
for row, column in game.board:
color = (255, 255, 255)
if game.board[row, column] == "picked":
color = (0, 255, 0)
pygame.draw.rect(screen,
color,
[(margin + width) * column + margin,
(margin + height) * row + margin,
width,
height])
pygame.display.flip()
clock.tick(30)
pygame.display.update()
如果您想要 "tutorial" 检查程序的实现,您可以在 ActiveState 的 Python Cookbook 上查看此 MineSweep recipe。代码的版本 2 引入了方法 __push
和 __build_mines
来处理单个地雷的创建。
Python版本:2.7.8
Objective:使用 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()
问题:我将每个单独的正方形作为其自己的对象,如 class Square() 中所示。在 Square class 的绘图函数中,它 returns 一个 None 的布尔值(变量 isMine),当我稍后在 DrawBoard class 中调用它时,它将对象附加到列表 'board'。要为游戏分配地雷,我想随机 select 创建的 Square 对象之一,并将布尔值从 None 更改为 True。也许这不是分配地雷的最佳方式,但我正在努力做到最好。感谢任何帮助。
只是生成一个介于0
和len(list)
之间的随机值。然后您可以使用以下语法访问该值:list[random_val]
如果要更改值:list[random_val] = True
from collections import OrderedDict
from random import sample
class Square():
def __init__(self):
self.val = 0
self.count = 0
def draw(self):
BLACK = (0, 0, 0)
x = 100 + self.val * 60
y = 0 + 60 * self.count
pygame.draw.rect(DRAWSURF, BLACK, (x, y, 60, 60), 5)
class DrawBoard():
def __init__(self, grid, mines): # size of grid and how many mines to set
self.grid_size = grid
# create x,y coordinates and set all mains to False initially
self.board = OrderedDict(((i,j),False) for i in range(grid) for j in range(grid))
self.mines = mines
# pick random coords to place mines
samp = sample(list(self.board),self.mines)
for k in samp:
self.board[k]=True
def draw(self):
item = Square()
for i, j in self.board:
item.val, item.count = i,j
item.draw()
game = DrawBoard(5,5) # create board
game.draw() # draw
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
clock.tick(30)
# game logic goes here
pygame.display.update()
这应该可以帮助您入门,您需要让用户知道他们何时靠近矿井并处理 how/when 他们获胜:
import pygame, sys
from pygame.locals import *
pygame.init()
width, height = 40, 40
clock = pygame.time.Clock()
DRAWSURF = pygame.display.set_mode((width, height))
pygame.display.set_caption("Matt's Minesweeper")
background = pygame.Surface(DRAWSURF.get_size())
DRAWSURF.blit(background, (0, 0))
pygame.display.flip()
size = [255, 255]
screen = pygame.display.set_mode(size)
margin = 5
from collections import OrderedDict
from random import sample
class DrawBoard():
def __init__(self, grid, mines):
self.grid_size = grid
self.board = OrderedDict(((i, j), False) for i in range(grid) for j in range(grid))
self.mines = mines
samp = sample(list(self.board), self.mines)
for k in samp:
self.board[k] = True
def draw(self):
for i, j in self.board:
x = 100 + i * 60
y = 0 + 60 * j
pygame.draw.rect(DRAWSURF,(0,0,0), (x, y, 60, 60), margin)
game = DrawBoard(5, 5)
game.draw()
def game_over():
font = pygame.font.SysFont(None, 50)
text = font.render('Game over!', True, (255, 0, 0), (255, 255, 255))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery
screen.blit(text, text_rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
user_won = False
user_lost = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# get mouse pos
pos = pygame.mouse.get_pos()
# change to grid coordinates
column = pos[0] // (width + margin)
row = pos[1] // (height + margin)
print(game.board[row, column])
if game.board[row, column]:
user_lost = True
else:
game.board[row, column] = "picked"
if user_lost:
game_over()
screen.fill((0, 0, 0))
for row, column in game.board:
color = (255, 255, 255)
if game.board[row, column] == "picked":
color = (0, 255, 0)
pygame.draw.rect(screen,
color,
[(margin + width) * column + margin,
(margin + height) * row + margin,
width,
height])
pygame.display.flip()
clock.tick(30)
pygame.display.update()
如果您想要 "tutorial" 检查程序的实现,您可以在 ActiveState 的 Python Cookbook 上查看此 MineSweep recipe。代码的版本 2 引入了方法 __push
和 __build_mines
来处理单个地雷的创建。