Reputation: 262
I drew an 8x8 checkered board in the middle of the screen, there is suppose to be empty space around the board but at the end of each row and column it looks like it continues to draw to the edge of the screen. Is there anything to fix this or will I have to create another surface around the board?
Note: I think the problem is somewhere in the drawMainBoard function. I tried creating a line where each box starts and ends and the line stops where it should but the board continues to draw to the edge of the screen.
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()
Upvotes: 0
Views: 810
Reputation: 101052
The problem is within your drawBox
function. You pass in the parameters boxstart
and boxend
, which represents the top left and the bottom right point of the rect to draw.
But the pygame.draw.rect
function expects a Rect
like object or tuple, representing the x and y coordinate of the top left point of the rect and it's size. So when you pass in the values (450, 100), (500, 150)
, you don't draw a Rect
from (450, 100)
to (500, 150)
, but a rect starting at (450, 100)
with a length of 500 and a height of 150.
An easy fix is to calculate the correct size in the function:
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)
or simply use the Rect
class instead:
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
Upvotes: 1