Related
I want to display a number inside a circle.
The number must be centered (x,y).
How to do it?
import pygame, sys
pygame.init()
screen= pygame.display.set_mode((0, 0))
black= (0,0,0)
white= (255,255,255)
red= (255,0,0)
number= "8"
bigfont = pygame.font.SysFont("arial", 50)
text = bigfont.render(number, True, white)
def draw():
pygame.draw.circle(screen, red, (300,300), 200)
screen.blit(text, (300,300))
pygame.display.update()
Get the bounding rectangle of the text and set the center of the rectangle by the center of the text. Use the rectangle to blit the text:
text_rect = text.get_rect()
text_rect.center = (300, 300)
screen.blit(text, text_rect)
The same with one line of code:
screen.blit(text, text.get_rect(center = (300, 300)))
Use this code in a function:
def draw_circle(x, y, text):
pygame.draw.circle(screen, red, (x, y), 200)
screen.blit(text, text.get_rect(center = (x, y)))
See pygame.Surface.blit:
[...] The dest argument can either be a pair of coordinates representing the position of the upper left corner of the blit or a Rect, where the upper left corner of the rectangle will be used as the position for the blit.
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
font = pygame.font.SysFont(None, 100)
clock = pygame.time.Clock()
text = font.render("Text", True, (255, 255, 0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window_center = window.get_rect().center
window.fill(0)
pygame.draw.circle(window, (255, 0, 0), window_center, 100)
window.blit(text, text.get_rect(center = window_center))
pygame.display.flip()
clock.tick(60)
pygame.quit()
exit()
To centre PyGame text around point x_pos, y_pos:
Get the dimensions of the text object: width, height = text.get_rect()[2:4]
When you render (screen.blit()) the image, shift the coordinates back by half the width and half the height: screen.blit(text, (x_pos - int(width / 2), y_pos - int(height / 2)))
This question already has an answer here:
Why is my pygame application loop not working properly?
(1 answer)
Closed 2 years ago.
I cant blit anything to my pygame screen using studio code on my mac. Is this a known issue or is there a way to fix it I am overlooking? I'm not getting any errors it's just not doing anything. I am kinda new to pygame so anything could work. here's my code:
pygame.display.set_caption('The space Simulator')
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
screen = pygame.display.set_mode([1000, 1000])
background = pygame.Surface((1000,1000))
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = background.get_rect().centerx
running=True
while running:
screen.blit(image, (textpos))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
You're just not flushing / updating the drawing primitives to the screen. Really you just need a pygame.display.update() or pygame.display.flip() after all your blits are done.
I guess you removed parts of your code to keep it simple for the question, but I put them back in for a working answer.
I also re-arranged the code, and removed the creation of the background Surface just to get the centre co-ordinate. This operation can be performed on the existing screen Surface.
import pygame
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
pygame.init()
pygame.display.set_caption('The space Simulator')
screen = pygame.display.set_mode([1000, 1000])
#image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
image = pygame.image.load('background.png' ).convert()
image = pygame.transform.smoothscale( image, (1000,1000) )
#background = pygame.Surface((1000,1000))
myfont = pygame.font.SysFont( None, 24 )
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = screen.get_rect().centerx
running=True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(image, (0,0))
screen.blit(text1, (textpos))
pygame.display.flip()
pygame.quit()
I'm having issues integrating working buttons into this code. The buttons have been defined and show within pygame but I don't know where to go next. I've attempted to add mouse and click functions but I really don't know where they would be best located or how to use their functions
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
BACKGROUND = (200, 230, 234)
WHITE = (255, 255, 255)
HOVER_COLOUR = (50, 70, 90)
# Text Variables
FONT = pygame.font.SysFont ("Times New Norman", 60)
TEXT = FONT.render ("", True, WHITE)
background_images = pygame.image.load("background.jpg").convert()
screen.blit(background_images, [0,0])
screen.blit(TEXT, (150, 50))
# Text & Rectangles construction
text1 = FONT.render("PlAY", True, WHITE)
text2 = FONT.render("CONTROLS", True, WHITE)
text3 = FONT.render("DIFFICULTY", True, WHITE)
text4 = FONT.render("SCOREBOARD", True, WHITE)
rect1 = pygame.Rect(250,200,300,80)
rect2 = pygame.Rect(250,300,300,80)
rect3 = pygame.Rect(250,400,300,80)
rect4 = pygame.Rect(250,500,300,80)
# The button construction arry. Text and Rectangle
buttons = [
[text1, rect1, BACKGROUND],
[text2, rect2, BACKGROUND],
[text3, rect3, BACKGROUND],
[text4, rect4, BACKGROUND],
]
def game_intro():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEMOTION:
for button in buttons:
# Uses collisionpoint to detect mouse position collisions
if button[1].collidepoint(event.pos):
# Set the button's colour to the hover colour.
button[2] = HOVER_COLOUR
else:
# resets the colour to normal.
button[2] = BACKGROUND
# Draws the buttons with their current colours (normal & collisions)
for text, rect, colour in buttons:
pygame.draw.rect(screen, colour, rect)
screen.blit(text, rect)
pygame.display.flip()
clock.tick(15)
#Run Game
game_intro()
scene_change()
pygame.quit()
When integrating this:
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
The program kicks out an error about the mouse position function
Add ids to your buttons:
buttons = [
[text1, rect1, BACKGROUND, 1],
[text2, rect2, BACKGROUND, 2],
[text3, rect3, BACKGROUND, 3],
[text4, rect4, BACKGROUND, 4]
]
Add a function which handles a button event:
def on_button(button):
print(button[3])
Call the function when the button is pressed:
def game_intro():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEMOTION:
for button in buttons:
# Uses collisionpoint to detect mouse position collisions
if button[1].collidepoint(event.pos):
# Set the button's colour to the hover colour.
button[2] = HOVER_COLOUR
else:
# resets the colour to normal.
button[2] = BACKGROUND
elif event.type == pygame.MOUSEBUTTONDOWN:
for button in buttons:
# Uses collisionpoint to detect mouse position collisions
if button[1].collidepoint(event.pos):
on_button(button)
# Draws the buttons with their current colours (normal & collisions)
for text, rect, colour, button_id in buttons:
pygame.draw.rect(screen, colour, rect)
screen.blit(text, rect)
pygame.display.flip()
clock.tick(15)
I've been watching a bunch of tutorials and new to pygame and coding in general. None of the tutorials I watched helped me with this. What i'm aiming for is for the buttons to change the button to a different colour when the user hovers over the button. Below is my entire code for the main menu so far.
import pygame
import time
import random
pygame.init()
size = (800, 600)
screen = pygame.display.set_mode(size)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.display.set_caption("Basketball Shootout")
clock = pygame.time.Clock()
#GAME INTRO
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
#Font's and text
font = pygame.font.SysFont ("Times New Norman", 60)
text = font.render ("", True, WHITE)
#Background Image
background_images = pygame.image.load("greybackground.jpg").convert()
screen.blit(background_images, [0,0])
screen.blit(text, (150, 50))
#Background Music
pygame.mixer.music.load('game.ogg')
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()
pygame.display.update()
clock.tick(15)
#BUTTONS
screen.blit(text, (150, 50))
pygame.draw.rect(screen,(0,0,0),(300,300,205,80));
pygame.draw.rect(screen,(0,0,0),(300,400,205,80));
pygame.draw.rect(screen,(0,0,0),(300,500,205,80));
font = pygame.font.SysFont ("Times New Norman, Arial", 30)
text = font.render ("START", True, WHITE)
screen.blit(text, (340, 320))
font = pygame.font.SysFont ("Times New Norman, Arial", 30)
text = font.render ("OPTIONS", True, WHITE)
screen.blit(text, (340, 420))
font = pygame.font.SysFont ("Times New Norman, Arial", 30)
text = font.render ("ABOUT", True, WHITE)
screen.blit(text, (340, 520))
pygame.display.flip();
#Quit Pygame
game_intro()
pygame.quit
I recommend storing the button data (text surface, rect, color) in a list of lists. When the mouse moves, pygame.MOUSEMOTION events get added to the event queue. In the event loop check if the mouse moved and then iterate over your buttons and set the color of colliding buttons to the hover color, reset the others to black. Blit the rects and text surfaces in a for loop as well.
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
HOVER_COLOR = (50, 70, 90)
# Don't define new font objects in your while loop (that's inefficient).
FONT = pygame.font.SysFont ("Times New Norman", 60)
# If the text surfaces and button rects don't change,
# you can define them once outside of the while loop.
text1 = FONT.render("START", True, WHITE)
text2 = FONT.render("OPTIONS", True, WHITE)
text3 = FONT.render("ABOUT", True, WHITE)
rect1 = pygame.Rect(300,300,205,80)
rect2 = pygame.Rect(300,400,205,80)
rect3 = pygame.Rect(300,500,205,80)
# The buttons consist of a text surface, a rect and a color.
buttons = [
[text1, rect1, BLACK],
[text2, rect2, BLACK],
[text3, rect3, BLACK],
]
def game_intro():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEMOTION:
for button in buttons:
# button[1] is the rect. Use its collidepoint method with
# the `event.pos` (mouse position) to detect collisions.
if button[1].collidepoint(event.pos):
# Set the button's color to the hover color.
button[2] = HOVER_COLOR
else:
# Otherwise reset the color to black.
button[2] = BLACK
screen.fill((20, 50, 70))
# Draw the buttons with their current colors at their rects.
# You can unpack the button lists directly in the head of the loop.
for text, rect, color in buttons:
pygame.draw.rect(screen, color, rect)
screen.blit(text, rect)
pygame.display.flip()
clock.tick(15)
game_intro()
pygame.quit()
I'd actually recommend defining a Button class, but I'm not sure if you're already familiar with classes/object-oriented programming.
The buttons also need callback functions to actually do something. Take a look at my answer here: https://stackoverflow.com/a/47664205/6220679
I would recommend creating a function for this:
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
gameDisplay.blit(active, (x, y))
else:
gameDisplay.blit(inactive, (x, y))
and then in you game intro call you function like this:
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
# For example
button(100, 350, 195, 80, startBtn, startBtn_Hover)
This is what each parameter means:
x: x-coordinate of button
y: y-coordinate of button
w: width of button
h: height of button
active: picture when mouse is hovered over button
inactive: picture when button is idle
I am new to python and pygame and I'm trying to move a image over drawed rectangles with changing colors. Whenever I run this code the image moves but it creates a track of the image.
I know that I need to blit the background of the image in the game loop but how can I blit the background if the background is not an image?
Or do I need to draw my rectangles in a different way?
Full code:
import pygame
pygame.init()
screen = pygame.display.set_mode((600,200))
pygame.draw.rect(screen, (175,171,171), [0, 0, 600, 200])
pygame.draw.rect(screen, (255,192,0), [200, 0, 200, 200])
clock = pygame.time.Clock()
# load your own image here (preferably not wider than 30px)
truck = pygame.image.load('your_image.png').convert_alpha()
class Truck:
def __init__(self, image, x, y, speed):
self.speed = speed
self.image = image
self.pos = image.get_rect().move(x, y)
def move(self):
self.pos = self.pos.move(self.speed, 0)
def game_loop():
newTruck = Truck(truck, 0, 50, 1)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
newTruck.move()
screen.blit(newTruck.image, newTruck.pos)
clock.tick(60)
pygame.display.update()
game_loop()
You have to somehow redraw all the objects (rectangles) in your background at every update.
The simplest way to do this is just to call all the drawing code again, before updating your foreground object. Another way, if the background does not change after created, is to blit these background objects into a separate Surface object, and blit that one to the screen on every update.
More sophisticated ways would be to save the background under your foreground objects are prior to drawing them, and then, on the next redraw, first redraw the background and then save the background again and draw the foreground objects on the new position. It is easier to do one of the former methods.
Your code could be written like this:
import pygame
pygame.init()
SIZE = (600,200)
screen = pygame.display.set_mode(SIZE)
bg_image = None
def draw_background(screen):
global bg_image
if not bg_image:
bg_image = pygame.Surface((SIZE))
pygame.draw.rect(bg_image, (175,171,171), [0, 0, 600, 200])
pygame.draw.rect(bg_image, (255,192,0), [200, 0, 200, 200])
...
# Draw whatever you want inside this if body
screen.blit(bg_image, (0, 0))
...
class Truck:
...
def game_loop():
newTruck = Truck(truck, 0, 50, 1)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
newTruck.move()
draw_background()
screen.blit(newTruck.image, newTruck.pos)
clock.tick(60)
pygame.display.update()
game_loop()
You can just move the two lines that draw your rects into the main while loop:
def game_loop():
newTruck = Truck(truck, 0, 50, 1)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
newTruck.move()
# Draw the background items first, then the foreground images.
# If the rects don't cover the whole screen, you can use
# `screen.fill(some_color)` to clear it.
pygame.draw.rect(screen, (175,171,171), [0, 0, 600, 200])
pygame.draw.rect(screen, (255,192,0), [200, 0, 200, 200])
screen.blit(newTruck.image, newTruck.pos)
clock.tick(60)
pygame.display.update()
If the background should be static, you could also draw the rects onto a background surface once and then blit this surf onto the screen in the main loop as jsbueno suggests.