我正在開發一個 picross 版本,但在為方塊著色的功能上遇到了問題。給出一個包含 10 個串列的串列,這些串列代表網格的行,其中每個串列的長度為 10 個元素。它們可以有 0、1 或 2 的條目。0 應表示該方塊沒有被標記為正確或錯誤,1 是正確的空格,而 2 是不正確的空格。代碼正確地為第一行著色,但不影響其他 9 行。它還引入了我不滿意的游戲視窗閃爍。這是函式,我會在之后添加整個游戲代碼。
def colorSquares(player_guess):
draw_y = 101
draw_x = 101
for row in player_guess:
for position in row:
rect = pygame.Rect(draw_x, draw_y, 43,43)
if position == 0:
pygame.draw.rect(win, BLACK, rect)
elif position == 1:
pygame.draw.rect(win, BLUE, rect)
elif position == 2:
pygame.draw.rect(win, GRAY, rect)
draw_x = 45
draw_y = 45
from itertools import zip_longest
from random import randint
import pygame
import math
BLACK = (0,0,0)
WHITE = (255,255,255)
BLUE = (25, 193, 212)
GRAY = (160, 181, 159)
win_height = 600
win_width = 600
def main():
global win, clock, rows, columns, player_guess
rows = [[randint(0,1) for i in range(10)] for x in range(10)]
columns = list([x for x in y if x is not None] for y in zip_longest(*rows))
player_guess = [[0 for i in range(10)] for i in range(10)]
pygame.init()
win = pygame.display.set_mode((win_height,win_width))
clock = pygame.time.Clock()
win.fill(BLACK)
run = True
while run:
drawGrid()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
if x > 100 and y > 100 and x < 550 and y < 550:
row_pos = (x - 100)// 45
col_pos = (y - 100)// 45
if player_guess[col_pos][row_pos] == 1:
player_guess[col_pos][row_pos] = 0
else:
player_guess[col_pos][row_pos] = 1
print("left ",row_pos,col_pos)
print(player_guess[col_pos][row_pos])
if event.button == 3:
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
if x > 100 and y > 100 and x < 550 and y < 550:
row_pos = (x - 100)// 45
col_pos = (y - 100)// 45
if player_guess[col_pos][row_pos] == 2:
player_guess[col_pos][row_pos] = 0
else:
player_guess[col_pos][row_pos] = 2
print("left ",row_pos,col_pos)
print(player_guess[col_pos][row_pos])
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
colorSquares()
pygame.display.update()
def drawGrid():
blockSize = 45 #Set the size of the grid block
for x in range(100, 550, blockSize):
for y in range(100, 550, blockSize):
rect = pygame.Rect(x, y, blockSize, blockSize)
pygame.draw.rect(win, WHITE, rect, 1)
def colorSquares():
draw_y = 101
draw_x = 101
for row in player_guess:
for position in row:
rect = pygame.Rect(draw_x, draw_y, 43,43)
if position == 0:
pygame.draw.rect(win, BLACK, rect)
elif position == 1:
pygame.draw.rect(win, BLUE, rect)
elif position == 2:
pygame.draw.rect(win, GRAY, rect)
draw_x = 45
draw_y = 45
main()
我知道它還不是很完善,但我想在我專注于調整它之前讓它作業。也就是說,歡迎任何和所有建議。
uj5u.com熱心網友回復:
您需要draw_x在每一行“重新啟動” :
def colorSquares():
draw_y = 101
# draw_x = 101 # <--- DELETE
for row in player_guess:
draw_x = 101 # <--- INSERT
for position in row:
rect = pygame.Rect(draw_x, draw_y, 43,43)
if position == 0:
pygame.draw.rect(win, BLACK, rect)
elif position == 1:
pygame.draw.rect(win, BLUE, rect)
elif position == 2:
pygame.draw.rect(win, GRAY, rect)
draw_x = 45
draw_y = 45
您可以使用串列和使用來簡化代碼enumerate:
def colorSquares():
colors = [BLACK, BLUE, GRAY]
for y, row in enumerate(player_guess):
for x, position in enumerate(row):
rect = pygame.Rect(101 x * 45, 101 y * 45, 43,43)
if position < len(colors):
pygame.draw.rect(win, colors[position], rect)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327038.html
下一篇:如何列印嵌套字典的串列值
