python棋盘放麦粒求和,使用python在pygame中制作8 * 8棋盘

I want to make a chessboard in pygame with python. Just only the chessboard with for loops. I tried in several ways to do this but i didn't figured out what exactly it will be. Here is my code:

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()

Now i need an expert suggestion what it will be with python code. I just wanna show a chessboard in my screen. Thats it.

解决方案

Possible solution, maybe not the most elegant, but you can create the squares in a loop

#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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值