Reputation:
Trying to add text into a rectangle and move the rectangle around the screen using the arrow keys. I want to make it so the text doesn't go off the edge. So far I had it working without putting it into a Rect but I want to make the Rect function work. Right now the text just bounces back and I don't know how big to make the original rectangle that corresponds with the font size.
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((800,600),0)
pygame.display.set_caption("Basic Pygame Text With Rects")
width = screen.get_width()
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)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
colour = BLACK
textRect = pygame.Rect(x, y, 30, 10)
screen.fill(WHITE)
pygame.display.update()
# first set up your font (typeface and size)
# I have created two different ones here that can be used later in the program
fontTitle = pygame.font.SysFont("arial",10)
main = True
while main:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
main = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP:
dx = 0
dy = -speed
colour = RED
elif event.key == pygame.K_DOWN:
dx = 0
dy = speed
colour = BLUE
elif event.key == pygame.K_LEFT: # note: this section of code
dx = -speed # doesn't have to change from
dy = 0
colour = GREEN
elif event.key == pygame.K_RIGHT:
dx = speed
dy = 0
colour = YELLOW
elif event.key == pygame.K_c:
x = screen.get_width() / 2
y = screen.get_height() / 2
colour = BLACK
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)
oldx = x
oldy = y
textRect.move_ip(dx,dy)
if (textRect.y <= 0) or (textRect.y >= height):
dy = 0
textRect.y = int(oldy)
if (textRect.x <= 20) or (textRect.x >= width - 20):
dx = 0
textRect.x = int(oldx)
# render the text into an image of the text, colour is red
# create a rect with it's centre placed at centre of screen
# blit the image to memory, it will display upon next update
textTitle = fontTitle.render("Go Huskies", True, colour)
drawText = textTitle.get_rect(center=(textRect.x, textRect.y))
screen.blit(textTitle, textRect)
pygame.display.update()
pygame.quit()
sys.exit()
Upvotes: 1
Views: 1762
Reputation: 142744
You could generate text at start to get its size
fontTitle = pygame.font.SysFont("arial", 10)
textTitle = fontTitle.render("Go Huskies", True, colour)
rectTitle = textTitle.get_rect(center=screen.get_rect().center)
and later use only this rectTitle
to move it and check collision with borders - and don't create new textTitle.get_rect()
screen.blit(textTitle, rectTitle)
Rect
has .top
, .bottom
, .left
, .right
, .centerx
, .centery
which you can use to check collision
oldx, oldy = rectTitle.center
rectTitle.move_ip(dx, dy)
if (rectTitle.top <= 0) or (rectTitle.bottom >= height):
dy = 0
rectTitle.centery = oldy
if (rectTitle.left <= 0) or (rectTitle.right >= width):
dx = 0
rectTitle.centerx = oldx
import pygame
import sys
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
# --- classes --- (CamelCaseNames)
# empty
# --- functions --- (lower_case_names)
# empty
# --- main --- (lower_case_names)
pygame.init()
screen = pygame.display.set_mode((800,600),0)
screen_rect = screen.get_rect()
pygame.display.set_caption("Basic Pygame Text With Rects")
x, y = screen_rect.center
width, height = screen_rect.size
dx = 0
dy = 0
speed = 10
oldx = x
oldy = y
colour = BLACK
fontTitle = pygame.font.SysFont("arial", 10)
textTitle = fontTitle.render("Go Huskies", True, colour)
rectTitle = textTitle.get_rect(center=screen_rect.center)
# - mainloop -
clock = pygame.time.Clock()
main = True
while main:
# - events -
for event in pygame.event.get():
if event.type ==pygame.QUIT:
main = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP:
dx = 0
dy = -speed
colour = RED
elif event.key == pygame.K_DOWN:
dx = 0
dy = speed
colour = BLUE
elif event.key == pygame.K_LEFT: # note: this section of code
dx = -speed # doesn't have to change from
dy = 0
colour = GREEN
elif event.key == pygame.K_RIGHT:
dx = speed
dy = 0
colour = YELLOW
elif event.key == pygame.K_c:
x, y = screen_rect.center
colour = BLACK
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
# - moves/updates -
oldx, oldy = rectTitle.center
rectTitle.move_ip(dx,dy)
if (rectTitle.top <= 0) or (rectTitle.bottom >= height):
dy = 0
rectTitle.centery = oldy
if (rectTitle.left <= 0) or (rectTitle.right >= width):
dx = 0
rectTitle.centerx = oldx
textTitle = fontTitle.render("Go Huskies", True, colour)
# - draws -
screen.fill(WHITE)
screen.blit(textTitle, rectTitle)
pygame.display.update()
clock.tick(25) # slow down to 25 FPS (frames per seconds)
# - end -
pygame.quit()
sys.exit()
If you want to move text with background rectangle then you can create Surface()
, fill it with some color and then blit text on it
at start:
rectRect = pygame.surface.Surface
fontTitle = pygame.font.SysFont("arial", 10)
textTitle = fontTitle.render("Go Huskies", True, colour)
rectTitle = textTitle.get_rect(center=screen_rect.center)
item_surface = pygame.surface.Surface(rectTitle.size)
item_surface.fill(RED)
item_surface.blit(textTitle, (0,0))
textTitle = item_surface
in loop:
textTitle = fontTitle.render("Go Huskies", True, colour)
item_surface = pygame.surface.Surface(rectTitle.size)
item_surface.fill(RED)
item_surface.blit(textTitle, (0,0))
textTitle = item_surface
Full code:
import pygame
import sys
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
# --- classes --- (CamelCaseNames)
# empty
# --- functions --- (lower_case_names)
# empty
# --- main --- (lower_case_names)
pygame.init()
screen = pygame.display.set_mode((800,600),0)
screen_rect = screen.get_rect()
pygame.display.set_caption("Basic Pygame Text With Rects")
x, y = screen_rect.center
width, height = screen_rect.size
dx = 0
dy = 0
speed = 10
oldx = x
oldy = y
colour = BLACK
rectRect = pygame.surface.Surface
fontTitle = pygame.font.SysFont("arial", 10)
textTitle = fontTitle.render("Go Huskies", True, colour)
rectTitle = textTitle.get_rect(center=screen_rect.center)
item_surface = pygame.surface.Surface(rectTitle.size)
item_surface.fill(RED)
item_surface.blit(textTitle, (0,0))
textTitle = item_surface
# - mainloop -
clock = pygame.time.Clock()
main = True
while main:
# - events -
for event in pygame.event.get():
if event.type ==pygame.QUIT:
main = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP:
dx = 0
dy = -speed
colour = RED
elif event.key == pygame.K_DOWN:
dx = 0
dy = speed
colour = BLUE
elif event.key == pygame.K_LEFT: # note: this section of code
dx = -speed # doesn't have to change from
dy = 0
colour = GREEN
elif event.key == pygame.K_RIGHT:
dx = speed
dy = 0
colour = YELLOW
elif event.key == pygame.K_c:
x, y = screen_rect.center
colour = BLACK
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
# - moves -
oldx, oldy = rectTitle.center
rectTitle.move_ip(dx,dy)
if (rectTitle.top <= 0) or (rectTitle.bottom >= height):
dy = 0
rectTitle.centery = oldy
if (rectTitle.left <= 0) or (rectTitle.right >= width):
dx = 0
rectTitle.centerx = oldx
textTitle = fontTitle.render("Go Huskies", True, colour)
item_surface = pygame.surface.Surface(rectTitle.size)
item_surface.fill(RED)
item_surface.blit(textTitle, (0,0))
textTitle = item_surface
# - draws -
screen.fill(WHITE)
screen.blit(textTitle, rectTitle)
pygame.display.update()
clock.tick(25) # slow down to 25 FPS (frames per seconds)
# - end -
pygame.quit()
sys.exit()
Upvotes: 1