我在屏幕中间画了一块8x8格子板,假设板周围是空的空间,但在每一行和一列的末尾看起来它继续画到屏幕的边缘。有什么可以解决这个问题,还是我必须在电路板周围创建另一个表面?
注意:我认为问题出在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
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()
答案 0 :(得分:1)
问题出在您的drawBox
功能范围内。您传递参数boxstart
和boxend
,它们代表要绘制的矩形的左上角和右下角。
但是pygame.draw.rect
函数需要一个Rect
类似的对象或元组,表示矩形左上角的x和y坐标,它的大小。因此,当您传递值(450, 100), (500, 150)
时,您不会从Rect
到(450, 100)
绘制(500, 150)
,而是从(450, 100)
开始绘制一个长度的矩形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