Python Pygame:绘制一个棋盘格,继续在方框的末尾绘制 我在屏幕中间画了一个8x8的棋盘,假设板周围是空的,但是在每行和列的末端,它看起来像一直延伸到屏幕的边缘。有什么办法解决这个问题吗?或者我必须在电路板周围创建另一个曲面吗
注意:我认为问题出在drawMainBoard功能的某个地方。我试着在每一个框的开始和结束处创建一条线,线停在它应该停的地方,但电路板继续拉到屏幕边缘Python Pygame:绘制一个棋盘格,继续在方框的末尾绘制 我在屏幕中间画了一个8x8的棋盘,假设板周围是空的,但是在每行和列的末端,它看起来像一直延伸到屏幕的边缘。有什么办法解决这个问题吗?或者我必须在电路板周围创建另一个曲面吗,python,pygame,Python,Pygame,注意:我认为问题出在drawMainBoard功能的某个地方。我试着在每一个框的开始和结束处创建一条线,线停在它应该停的地方,但电路板继续拉到屏幕边缘 import pygame._view import pygame import sys from pygame.locals import* import time import random FPS=30 fpsClock=pygame.time.Clock() displayWidth=600 displayHeight=600 Xm
import pygame._view
import pygame
import sys
from pygame.locals import*
import time
import random
FPS=30
fpsClock=pygame.time.Clock()
displayWidth=600
displayHeight=600
Xmargin=(displayWidth/12)*2
Ymargin=(displayHeight/12)*2
boardRows=8
boardColumns=8
boxx=(displayWidth-(Xmargin*2))/8
boxy=(displayHeight-(Ymargin*2))/8
DISPLAYSURF=pygame.display.set_mode((displayWidth,displayHeight),0,32)
colorRed= (255,0,0)
colorBlack= (0,0,0)
colorWhite= (255,255,255)
colorRed2= (150,0,0)
colorBlack2=(10,10,10)
pygame.init()
myFont=pygame.font.SysFont("ariel",15)
def mainscreen():
DISPLAYSURF.fill((255,255,255))
drawMainboard()
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)
def drawBox(boxstart,boxend,boxcolor):
pygame.draw.rect(DISPLAYSURF,boxcolor,(boxstart,boxend),0)
def drawMainboard():
firstColor=colorBlack
boxxstart=Xmargin
boxystart=Ymargin
boxxend=Xmargin+boxx
boxyend=Ymargin+boxy
lettercount=0
boxstart=(boxxstart,boxystart)
boxend=(boxxend,boxyend)
for columns in range(0,boardColumns):
if firstColor==colorRed:
firstColor=colorBlack
else:
firstColor=colorRed
for rows in range(0,boardRows):
drawBox(boxstart,boxend,firstColor)
label=myFont.render(str(lettercount),20,(0,0,255))
DISPLAYSURF.blit(label,(boxxstart,boxystart))
pygame.draw.line(DISPLAYSURF,colorWhite,(boxxend,boxyend),(boxxstart,boxystart),1)
lettercount+=1
if firstColor==colorRed:
firstColor=colorBlack
else:
firstColor=colorRed
boxxstart+=boxx
boxxend+=boxx
boxstart=(boxxstart,boxystart)
boxend=((boxxend),(boxyend))
boxxstart=Xmargin
boxxend=Xmargin+boxx
boxystart+=boxy
boxyend+=boxy
boxstart=(boxxstart,boxystart)
boxend=(boxxend,boxyend)
while True:
mainscreen()
问题出在您的
drawBox
功能中。传入参数boxstart
和boxend
,表示要绘制的矩形的左上角和右下角点
但是pygame.draw.rect
函数需要一个类似rect
的对象或元组,表示rect左上点的x和y坐标及其大小。因此,当您传入值(450100)、(500150)
时,您不会从(450100)
到(500150)
绘制矩形,而是从(450100)
开始绘制长度为500、高度为150的矩形
一个简单的解决方法是计算函数中的正确大小:
def drawBox(boxstart,boxend,boxcolor):
sx, sy = boxstart
ex, ey = boxend
r = Rect(sx, sy, ex-sx, ey-sy)
pygame.draw.rect(DISPLAYSURF,boxcolor,r,0)
或者只需使用Rect
类:
from itertools import cycle
...
def drawMainboard():
# the current rect we're going to draw
current = Rect((Xmargin, Ymargin), (boxx, boxy))
# the colors we use
colors = cycle((pygame.color.Color('Black'), pygame.color.Color('Red')))
lettercount = 0
for _ in range(0, boardRows):
# switch colors on each row, so the last rect on a row
# has the same color as the starting rect on the next row
# only need on an even number of columns
next(colors)
for _ in range(0, boardColumns):
# switch color and draw rect
pygame.draw.rect(DISPLAYSURF, next(colors), current, 0)
# draw label. We can simply use 'current' as position
label=myFont.render(str(lettercount),20,(0,0,255))
DISPLAYSURF.blit(label, current)
# draw the line. We can simply use current.topleft, current.bottomright
pygame.draw.line(DISPLAYSURF,colorWhite, current.topleft, current.bottomright, 1)
lettercount += 1
# move the current rect to the next column
current.move_ip((boxx, 0))
# for the next row, start at 'Xmargin' and move the rect down one row
current.topleft = Xmargin, current.top + boxy
我添加了一个更简单的drawMainboard
功能。我还没有学习所有不同模块的所有功能。只有在我使用它们的时候,我才真正了解它们,所以你真的在帮助我,非常感谢:)