我想用python在pygame中创建一个棋盘。只是带有for循环的棋盘。我试过几种方法来做到这一点,但我还没弄清楚到底是什么。这是我的代码:
import pygame
pygame.init()
#set color with rgb
white,black,red = (255,255,255),(0,0,0),(255,0,0)
#set display
gameDisplay = pygame.display.set_mode((800,600))
#caption
pygame.display.set_caption("ChessBoard")
#beginning of logic
gameExit = False
lead_x = 20
lead_y = 20
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
#For loop for chessboard
#draw a rectangle
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [lead_x,lead_y,20,20])
pygame.display.update()
#quit from pygame & python
pygame.quit()
quit()
现在我需要一个专家建议使用python代码。我只想在屏幕上显示一个棋盘。多数民众赞成。
答案 0 :(得分:3)
更有效的方法是在初始化时绘制一次电路板,然后只是表面处理:
exports.getMentionedUsers = function(str) {
return Promise.all(getUsernamesFromString(str).map(username => {
return db.ref('/users').orderByChild('username').equalTo(username).once('value').then(snapshot => {
return snapshot.val();
});
}));
}
然后在你的循环中首先绘制板面:
cellSize = 20
board = Surface((cellSize * 8, cellSize * 8))
board.fill((255, 255, 255))
for x in range(0, 8, 2):
for y in range(0, 8, 2):
pygame.draw.rect(board, (0,0,0), (x*size, y*size, size, size))
答案 1 :(得分:1)
可能的解决方案,也许不是最优雅的,但你可以在循环中创建方块
#Size of squares
size = 20
#board length, must be even
boardLength = 8
gameDisplay.fill(white)
cnt = 0
for i in range(1,boardLength+1):
for z in range(1,boardLength+1):
#check if current loop value is even
if cnt % 2 == 0:
pygame.draw.rect(gameDisplay, white,[size*z,size*i,size,size])
else:
pygame.draw.rect(gameDisplay, black, [size*z,size*i,size,size])
cnt +=1
#since theres an even number of squares go back one value
cnt-=1
#Add a nice boarder
pygame.draw.rect(gameDisplay,black,[size,size,boardLength*size,boardLength*size],1)
pygame.display.update()
答案 2 :(得分:0)
您可以使用itertools.cycle
循环嵌套for循环中的颜色,然后将next(colors)
传递给pygame.draw.rect
。我会创建一个背景曲面并在程序启动时将其绘制到它上面,然后在while循环中只显示背景冲浪,因为这比单独渲染rects更有效。
import itertools
import pygame as pg
pg.init()
BLACK = pg.Color('black')
WHITE = pg.Color('white')
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
colors = itertools.cycle((WHITE, BLACK))
tile_size = 20
width, height = 8*tile_size, 8*tile_size
background = pg.Surface((width, height))
for y in range(0, height, tile_size):
for x in range(0, width, tile_size):
rect = (x, y, tile_size, tile_size)
pg.draw.rect(background, next(colors), rect)
next(colors)
game_exit = False
while not game_exit:
for event in pg.event.get():
if event.type == pg.QUIT:
game_exit = True
screen.fill((60, 70, 90))
screen.blit(background, (100, 100))
pg.display.flip()
clock.tick(30)
pg.quit()
答案 3 :(得分:0)
这是我在代码中使用的一小段代码
import pygame; from pygame import *
import numpy as np
import itertools
class Chessboard:
...
def create(self):
for row in range(8):
for col in range(8):
self.checker = draw.rect(
board_surf, next(self.checker_color),
(checker_size * row, checker_size * col,
checker_size, checker_size))
self.append(self.checker)
next(self.checker_color)
return np.array(self.checker_coords).reshape(8,8,4)
chessboard = Chessboard()
checkers = chessboard.create()
现在您有了一个列表,可以在以后参考! https://imgur.com/a/N7MXAC5
[[[ 0 0 64 64]
[ 0 64 64 64]
[ 0 128 64 64]
[ 0 192 64 64]
[ 0 256 64 64]
[ 0 320 64 64]
[ 0 384 64 64]
[ 0 448 64 64]]
[[ 64 0 64 64]
[ 64 64 64 64]
[ 64 128 64 64]
[ 64 192 64 64]
[ 64 256 64 64]
[ 64 320 64 64]
[ 64 384 64 64]
[ 64 448 64 64]]
[[128 0 64 64]
[128 64 64 64]
[128 128 64 64]
[128 192 64 64]
[128 256 64 64]
[128 320 64 64]
[128 384 64 64]
[128 448 64 64]]
[[192 0 64 64]
[192 64 64 64]
[192 128 64 64]
[192 192 64 64]
[192 256 64 64]
[192 320 64 64]
[192 384 64 64]
[192 448 64 64]]
[[256 0 64 64]
[256 64 64 64]
[256 128 64 64]
[256 192 64 64]
[256 256 64 64]
[256 320 64 64]
[256 384 64 64]
[256 448 64 64]]
[[320 0 64 64]
[320 64 64 64]
[320 128 64 64]
[320 192 64 64]
[320 256 64 64]
[320 320 64 64]
[320 384 64 64]
[320 448 64 64]]
[[384 0 64 64]
[384 64 64 64]
[384 128 64 64]
[384 192 64 64]
[384 256 64 64]
[384 320 64 64]
[384 384 64 64]
[384 448 64 64]]
[[448 0 64 64]
[448 64 64 64]
[448 128 64 64]
[448 192 64 64]
[448 256 64 64]
[448 320 64 64]
[448 384 64 64]
[448 448 64 64]]]
答案 4 :(得分:0)
就用这个:
def pxl(q):
return q * tile_size
for y in range(8):
for x in range(8):
pygame.draw.rect(screen, color, [pxl(x), pxl(y), pxl(1), pxl(1)])(x, y, black if (x + y) % 2 == 0 else white)
答案 5 :(得分:0)
for x in range(width):
for y in range(height):
if y % 2 == 0:
if not x % 2 == 0:
pygame.draw.rect(w, (255, 255, 255), (x * scale, y * scale, scale, scale))
else:
if x % 2 == 0:
pygame.draw.rect(w, (255, 255, 255), (x * scale, y * scale, scale, scale))