В настоящее время я использую тексты и тексты в Pygame, и пока моя программа работает нормально, и она сталкивается с краем экрана. Проблема в том, что я не знаю, как изменить размер шрифта после нажатия клавиши, чтобы она правильно сталкивалась со стеной. Все остальное работает нормально, только столкновение после изменения размера шрифта не работает должным образом. Кто-нибудь может мне помочь с этим?
import pygame # Import the pygame and sys module.
import sys
pygame.init() # Initialize pygame.
screen = pygame.display.set_mode((800,600),0) # Set the display and display caption.
pygame.display.set_caption("Pygame Text With Rects")
width = screen.get_width() # Declare the variables for the width height and text.
height = screen.get_height()
x = int(width / 2)
y = int(height / 2)
dx = 0
dy = 0
speed = 10
oldx = x
oldy = y
WHITE = (255,255,255) # Declare the variables for various colours and the colour of the text.
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
colour = BLACK
fontSize = 30
screen.fill(WHITE) # Fill the screen white and update the display.
pygame.display.update()
fontTitle = pygame.font.SysFont("comicsansms", fontSize) # Declare the fontTitle and textTitle variables that will render the text with the given font.
textTitle = fontTitle.render("Go Huskies", True, colour)
textRect = textTitle.get_rect(center=(x, y)) # Create a rectangle around the font rendered above.
main = True
while main:
for event in pygame.event.get():
if event.type ==pygame.QUIT: # Input handling for moving the text and changing the colour depending on the direction the text travels.
main = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP:
dx = 0
dy = -speed
colour = RED
fontTitle = pygame.font.SysFont("arial", fontSize) # Declare the fontTitle and textTitle variables that will render the text with the given font.
elif event.key == pygame.K_DOWN:
dx = 0
dy = speed
colour = BLUE
fontTitle = pygame.font.SysFont("comicsansms", fontSize) # Declare the fontTitle and textTitle variables that will render the text with the given font.
elif event.key == pygame.K_LEFT:
dx = -speed
dy = 0
colour = GREEN
fontTitle = pygame.font.SysFont("sourceserifpro", fontSize) # Declare the fontTitle and textTitle variables that will render the text with the given font.
elif event.key == pygame.K_RIGHT:
dx = speed
dy = 0
colour = YELLOW
fontTitle = pygame.font.SysFont("mvboli", fontSize) # Declare the fontTitle and textTitle variables that will render the text with the given font.
elif event.key == pygame.K_c: # If the c key is pressed move the text rectangle and therefore text to the centre of the screen.
textRect.x = screen.get_width() / 2
textRect.y = screen.get_height() / 2
colour = BLACK
fontTitle = pygame.font.SysFont("arial", fontSize) # Declare the fontTitle and textTitle variables that will render the text with the given font.
textRect = textTitle.get_rect(center=(x, y)) # Create a rectangle around the font rendered above.
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
dx = 0
dy = 0
elif event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
dx = 0
dy = 0
screen.fill(WHITE)
textTitle = fontTitle.render("Go Huskies", True, colour) # Render the text again with the new colour that would have changed after a key press.
oldx = textRect.x # Set the oldx and oldy variables to the x and y position of the text rectangle.
oldy = textRect.y
textRect.move_ip(dx, dy) # Move the rectangle.
if (textRect.top <= 0) or (textRect.bottom >= height): # Collision for the rectangle using the .top, .bottom, .left and .right.
dy = 0
textRect.y = int(oldy)
if (textRect.left <= 0) or (textRect.right >= width):
dx = 0
textRect.x = int(oldx)
screen.blit(textTitle, textRect) # Blit the text to the screen and update the display.
pygame.display.update()
pygame.quit() # Quit pygame.
sys.exit()