Pygame blit 不显示任何内容



我目前正在为大学制作一款游戏。现在我需要在屏幕上显示文本和计时器。在代码的底部,您可以看到我使用了.blit函数来显示计时器。但是什么也没发生。也没有错误信息显示。它什么也不做。我也尝试过用列表中的元素。如果有人能帮我,那就太好了。提前谢谢你!

import pygame
from pygame.locals import *
import random
pygame.init()
# Color-Codes
ORANGE = (255, 140, 0)
RED = (255, 0, 0)
DARKBLUE = (0, 0, 139)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
DARKGREEN = (0, 98, 7)
LIMEGREEN = (50, 205, 50)
DARKGREY = (70, 70, 70)
BLUE = (0, 0, 255)
LIGHTBLUE = (173, 216, 230)
WOODY = (139,69,19)
LIST_OF_ALL_COLOR_NAMES = [ ORANGE, RED, DARKBLUE, WHITE, BLACK, YELLOW, GREEN, DARKGREEN, LIMEGREEN,
DARKGREY, BLUE, LIGHTBLUE ]

# Konstanten
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
PANEL_SIZE = 160
SQUARE_SIZE = 80
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
move_list = []
screen = pygame.display.set_mode((1200, 800))  # Fensterbreite und länge in pixeln, nur der Zeichenbereich
font = pygame.font.SysFont("Consolas", 30)  # Importieren der Schrift
textRight = font.render("RECHTS", False, WHITE)
pygame.time.set_timer(pygame.USEREVENT, 1500)
def pxl (number_of_squares):
"number of squares -> number of pixels"
return SQUARE_SIZE * number_of_squares
SIDEPANEL = pygame.Rect(pxl(13), pxl(0), pxl(2), pxl(10))
counter, text = 15, "15".rjust(1080)
# functions
def drawGrid():
for x in range(0, SCREEN_WIDTH, SQUARE_SIZE):
for y in range(0, SCREEN_HEIGHT, SQUARE_SIZE):
rect = pygame.Rect(x, y, SQUARE_SIZE, SQUARE_SIZE)
pygame.draw.rect(screen, BLACK, rect, 1)
def drawRect():
pygame.draw.rect(screen, WHITE, SIDEPANEL,)
def write_move():
for event in pygame.event.get():
if event.type == KEYDOWN:
j = 0
i = 0
if event.key == K_UP:
screen.blit(move_list[i], pxl(13.5), pxl(0.5+j))
j += 0.5
i += 1
elif event.key == K_DOWN:
move_list.append(2)
elif event.key == K_RIGHT:
move_list.append(3)
elif event.key == K_LEFT:
move_list.append(4)
def showTimer():
screen.blit(font.render(text, True, BLACK), (1080,40))
# Classes
class Player(object):
def __init__(self):
self.rect = pygame.Rect(pxl(2.1), pxl(2.1), pxl(0.8), pxl(0.8))
def move(self, dx, dy):
# Move each axis separately. Note that this checks for collisions both times.
if dx != 0:
self.move_single_axis(dx, 0)
if dy != 0:
self.move_single_axis(0, dy)
def move_single_axis(self, dx, dy):
# Move the rect
self.rect.x += dx
self.rect.y += dy
# If you collide with a wall, move out based on velocity
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0:  # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
if dx < 0:  # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
if dy > 0:  # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0:  # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
class Wall(object):
def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], pxl(1), pxl(1))
# Variables
pygame.display.set_caption("PyGame for University")  # Titel des Spiels
clock = pygame.time.Clock()  # Schnelligkeit des Spiels festlegen, damit es überall gleich schnell läuft
ticks = 0
player = Player()
walls = []
level = [
"WWWWWWWWWWWWW",
"W           W",
"W         WWW",
"W   WWWW    W",
"W   W       W",
"W WWW  WWWW W",
"W   W     W W",
"W   W     W W",
"W           W",
"WWWWWWWWWWWWW", 
]
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
if col == "E":
end_rect = pygame.Rect(x, y, pxl(1), pxl(1))
x += pxl(1)
y += pxl(1)
x = 0
## Screen Logic
# Screen-Settings

# game main loop
running = True
while running:
j = 0
i = 0
#renderText = font.render(move_list[i], True, BLACK)
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
print("Spiel wird beendet!")
elif event.type == KEYDOWN:
if event.key == K_UP:
screen.blit(font.render("RECHTS", False, WHITE), [pxl(2), pxl(2)])
move_list.append(1)
elif event.key == K_DOWN:
move_list.append(2)
write_move()
elif event.key == K_RIGHT:
move_list.append(3)
write_move()
elif event.key == K_LEFT:
move_list.append(4)
write_move()
elif event.key == K_0:
print(move_list)
if event.type == pygame.USEREVENT:
counter -= 1
text = str(counter).rjust(pxl(1080)) if counter > 0 else "boom!"
# game logic ########################################################################

# draw frame ########################################################################
screen.fill(WOODY)     # draw background
drawRect()
drawGrid()
for wall in walls:
pygame.draw.rect(screen, BLACK, wall.rect)
pygame.draw.rect(screen, BLUE, player.rect)

screen.blit(font.render(text, True, BLACK), (1080,40))
####################################################################################
pygame.display.flip()  # update window
clock.tick(60)

str.rjust(width[, fillchar]):

返回长度为width的字符串中右对齐的字符串。填充使用指定的fillchar完成(默认是一个ASCII空格)。如果width小于或等于len(s),则返回原始字符串。

1080字符太多了。改变参数(例如4):

text = str(counter).rjust(pxl(1080)) if counter > 0 else "boom!"

text = str(counter).rjust(4) if counter > 0 else "boom!"

最新更新