我正在用Python编写一个程序,它将作为一种刮刮票类型的游戏 . 我完成了所有工作,除非用户“擦掉”机票上的方格时打印出数字 . 这是我的代码:
import pygame
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((300,450))
pygame.display.set_caption("test grid")
background = pygame.Surface(screen.get_size())
background.fill((209, 95, 238))
clock = pygame.time.Clock()
keepGoing = True
pos = [0, 0]
x = 40
for line in range(5):
y = 200
for row in range(4):
pygame.draw.rect(background, (238, 201, 0), (x, y, 40, 40), 0)
y += 45
x += 45
board = [[1, 1, 3, 15, 11],
[1, 14, 13, 15, 9],
[2, 6, 7, 15, 5],
[8, 10, 4, 12, 7]]
myfont = pygame.font.SysFont("arial", 50)
while keepGoing:
clock.tick(30)
y = 200
x = 40
x1 = 40
x2 = 80
y1 = 200
y2 = 240
num = 0
label = myfont.render(str(num),1,(255,255,255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
print pygame.mouse.get_pos()
for row in range(4):
for line in range(5):
if pos[0] >= x1 and pos[0] <= x2 and pos[1] >= y1 and pos[1] <= y2:
pygame.draw.rect(background, (0,0,0), (x,y,40,40), 0)
num = board[row][line]
label = myfont.render(str(num), 1, (255,255,255))
x1 += 45
else:
x1 += 45
x2 += 45
x += 45
x = 40
y += 45
y1 += 45
y2 += 45
x1 = 40
x2 = 80
screen.blit(label,(200,200))
screen.blit(background, (0,0))
pygame.display.flip()
pygame.quit()
由于某种原因,我无法弄清楚,不管我做什么,我的字体都不会打印出来 . 我尝试只在循环外声明它,我尝试只在循环中声明它,我尝试了两个,并且由于某种原因没有任何工作,我无法弄清楚为什么我的文本拒绝在屏幕上打印 . 请帮忙?
我的老师说这与我的x1和x2变量有关,但我检查过,他们正在做他们应该做的事情 .
(对于任何想知道的人,我将x添加到x1的原因是即使if中的语句为真也是因为这样它不会继续重复并且一次又一次地打印出数字直到for循环结束 . 不是最大的解决方案,但我唯一能想到的 . )