简体   繁体   English

文本不会进入屏幕

[英]the text won't blit into the screen

I am trying to blit the score in the left corner of the screen.我正在尝试将屏幕左角的分数设为 blit。 I have currently been trying for 2 hours and nothing seems to be working.我目前已经尝试了 2 个小时,但似乎没有任何效果。

font = pygame.font.SysFont("arial", 72)     
text = "Score:" + str(enemies_clicked)
label = font.render(text, 1, red)
screen.blit(label, (20, 10))

There are no error messages.没有错误消息。 I tried different fonts and everything seems to work, except the text.我尝试了不同的字体,除了文本之外,一切似乎都有效。 The font variable is declared between the pygame.init() and the while loop. font 变量在pygame.init()和 while 循环之间声明。 The other 3 lines are 2 indents deep in the while loop.其他 3 行是 while 循环中的 2 个缩进。

Complete code:完整代码:

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()

You blit it in wrong place - you have to do it after screen.fill(black) which clears buffer.你在错误的地方 blit 它 - 你必须在screen.fill(black)清除缓冲区之后做它。 And before pygame.display.update() which sends buffer on screen.pygame.display.update()在屏幕上发送缓冲区之前。

if you blit before screen.fill(black) then fill() removes it and you can't see it on screen.如果您在screen.fill(black)之前 blit 然后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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM