我正在尝试在屏幕的左上角打分。我目前已经尝试了2个小时,但似乎没有任何反应。
font = pygame.font.SysFont("arial", 72)
text = "Score:" + str(enemies_clicked)
label = font.render(text, 1, red)
screen.blit(label, (20, 10))
没有错误消息。我尝试了不同的字体,除了文本,其他所有内容似乎都可以正常工作。
字体变量在pygame.init()
和while循环之间声明。其他3行在while循环的深处是2个缩进。
完整代码:
import pygame
import sys
import time
from random import randint as random
pygame.init()
width = 600
height = 400
block_size = 40
enemy_size = block_size
enemy_pos = [int(random(1,14)*block_size), int(random(1,8)*block_size)]
enemies_clicked = 0
font = pygame.font.SysFont("comicsansms", 72)
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
green = (0,255,0)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("click game")
def clicked_on_enemy(enemy_pos, enemy_size, click_pos):
e_x = enemy_pos[0]
e_y = enemy_pos[1]
c_e = click_pos[0]
c_y = click_pos[1]
x_col = False
y_col = False
for i in range(enemy_size):
if e_x + i == c_e:
x_col = True
for i in range(enemy_size):
if e_y + i == c_y:
y_col = True
if x_col and y_col:
return True
else:
return False
#main loop#
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
click_pos = pygame.mouse.get_pos()
if clicked_on_enemy(enemy_pos, enemy_size, click_pos):
enemy_pos = [int(random(1,14) * block_size), int(random(1,8) * block_size)]
enemies_clicked += 1
text = "Score:" + str(enemies_clicked)
label = font.render(text, 1, red)
screen.blit(label, (20, 10))
screen.fill(black)
pygame.draw.rect(screen, green, (enemy_pos[0],enemy_pos[1], enemy_size, enemy_size))
pygame.display.update()
答案 0 :(得分:1)
您将其涂抹在错误的位置-必须在screen.fill(black)
之后清除缓冲区。在pygame.display.update()
之前,它会在屏幕上发送缓冲区。
如果您在screen.fill(black)
之前流血,那么fill()
会将其删除,并且您将无法在屏幕上看到它。
text = "Score:" + str(enemies_clicked)
label = font.render(text, 1, red)
screen.fill(black)
pygame.draw.rect(screen, green, (enemy_pos[0],enemy_pos[1], enemy_size, enemy_size))
screen.blit(label, (20, 10))
pygame.display.update()