Is there any posibility to render differently styled bullet points in Python3 with pygame? I can use the following character ' - ', but I'd rather have round bulletpoints for example, like the ones stackoverflow uses:
This type of bulletpoint
As a horribly ugly workaround I am currently thinking of drawing a circle where the bulletpoint would be, but before I put the effort in, I wanted to ask if it is inherently impossible with pygame or if I am missing something.
Minimal working code if you want to test your solutions:
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 900))
pygame.display.get_surface().fill((200, 200, 200))
# Fonts
pygame.font.init()
my_font = pygame.font.SysFont('Arial', 30)
text_surface = my_font.render('Some Text', False, (0, 0, 0))
text_surface2 = my_font.render('- A bulletpoint text', False, (0, 0, 0))
# Loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(text_surface, (10, 0))
screen.blit(text_surface2, (10, 50))
pygame.display.flip()
You have an option to download font that has different types of bulletpoints and use it. An easier but limiting alternative is to directly use the unicode for bulletpoint, which is u'\u2022', but this would limit the types of bulletpoints you can use.
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 900))
pygame.display.get_surface().fill((200, 200, 200))
# Fonts
pygame.font.init()
my_font = pygame.font.SysFont('Arial', 30)
text_surface = my_font.render('Some Text', False, (0, 0, 0))
text_surface2 = my_font.render(u'\u2022 BulletPoint' , False, (0, 0, 0))
# Loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(text_surface, (10, 0))
screen.blit(text_surface2, (10, 50))
pygame.display.flip()
Also, note that you have to set anti-aliasing to True, otherwise it doesn't make a circle.
my_font.render(u'\u2022 BulletPoint' , True, (0, 0, 0))
Related
I want to rotate the screen of my pygame window by 90 degrees and am unable to find any function to do so. I tried p.transform.rotate() but i guess that is used for Images only. Any help would be deeply appreciated
Of course you can use pygame.transform.rotate(). pygame.display.set_mode() just generates a pygame.Surface which is associated to the display.
However, pygame.transform.rotate() returns a new but rotated pygame.Surface object. Therefore you must blit the surface back on the dispaly:
window.blit(pygame.transform.rotate(window, 90), (0, 0))
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
font = pygame.font.SysFont(None, 100)
clock = pygame.time.Clock()
text = font.render("Display", True, (255, 255, 0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
window.blit(text, text.get_rect(center = window.get_rect().center))
window.blit(pygame.transform.rotate(window, 90), (0, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
exit()
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
I tried to make a clone of a game but the rectangle (which is the basic shape that I will need)does not appear. What did I do wrong or its just pygame going crazy?
code:
# importing modules
import pygame
import sys
import random
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.display.quit()
pygame.quit()
exit()
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# cube
if running == True:
pygame.draw.rect(screen, blue, (395, 0, 10, 10)),
pygame.draw.rect(screen, blue, (395, 10, 10, 10)),
pygame.draw.rect(screen, blue, (395, 20, 10, 10)),
clock.tick(60)
and also how am I going to make it empty and 3d. I know I am asking for a lot but I believe someone can explain
You have to draw the scene in the application loop:
# importing modules
import pygame
import sys
import random
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.draw.rect(screen, blue, (395, 0, 10, 10))
pygame.draw.rect(screen, blue, (395, 10, 10, 10))
pygame.draw.rect(screen, blue, (395, 20, 10, 10))
pygame.display.update()
clock.tick(60)
pygame.display.quit()
pygame.quit()
exit()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
I'm having trouble with the framerate in my game. I've set it to 60 but it only goes to ~25fps. This was not an issue before displaying the background (was fine with only win.fill(WHITE)). Here is enough of the code to reproduce:
import os, pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50, 50)
pygame.init()
bg = pygame.image.load('images/bg.jpg')
FPS = pygame.time.Clock()
fps = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
winW = 1227
winH = 700
win = pygame.display.set_mode((winW, winH))
win.fill(WHITE)
pygame.display.set_icon(win)
def redraw_window():
#win.fill(WHITE)
win.blit(bg, (0, 0))
win.blit(text_to_screen('FPS: {}'.format(FPS.get_fps()), BLUE), (25, 50))
pygame.display.update()
def text_to_screen(txt, col):
font = pygame.font.SysFont('Comic Sans MS', 25, True)
text = font.render(str(txt), True, col)
return text
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_window()
FPS.tick(fps)
pygame.quit()
Ensure that the background Surface has the same format as the display Surface. Use convert() to create a Surface that has the same pixel format. That should improve the performance, when the background is blit to the display, because the formats are compatible and blit do not have to do an implicit transformation.
bg = pygame.image.load('images/bg.jpg').convert()
Furthermore, it is sufficient to create the font once, rather than every time when a text is drawn. Move font = pygame.font.SysFont('Comic Sans MS', 25, True) to the begin of the application (somewhere after pygame.init() and before the main application loop)
Instead use screen.blit(pygame.image.load(picture.png))
Just image = pygame.image.load(picture.png) then screen.blit(image)
( if you keep loading your pictures continuously it will get lag )
I’m the first time to ask on stack overflow! I’m a 12-year-old boy who living in Hong Kong, so if my English was wrong, please tell me and please don’t keep in mind. Apart from that, I’m a newer of Python. I don’t know that the meaning of the code. Can everyone making a # to tell me the meaning? Thank you!!
I am doing a project that use Python. Also, for the display, I use Pygame too. But there have some problem with the displays.
Here is my testing code:
# install pygame
import pygame
from pygame import *
pygame.init()
# play music
mixer.init()
mixer.music.load("game_music.mp3")
mixer.music.play()
# colour
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
grass_green = (112, 173, 71)
white = (255, 255, 255)
black = (0, 0, 0)
# screen settings
window = pygame.display.set_mode((640, 600))
window.fill(grass_green)
pygame.display.set_caption("What's Your Number?")
# fonts settings
default_font = pygame.font.get_default_font()
font_a = pygame.font.Font(default_font, 57)
font_b = pygame.font.Font(default_font, 30)
font_c = pygame.font.Font(default_font, 18)
# text
title = font_a.render("What's Your Number?", 1, white)
enter_to_start = font_b.render("Press the Enter to start", 1, white)
random_mode = font_a.render("Random Mode", 1, black)
your_mode = font_a.render("Your Mode", 1, black)
des_random_1 = font_c.render("The PC choose a random number for you to", 1, black)
des_random_2 = font_c.render("guess! Can you guess it correct?", 1, black)
des_your = font_c.render("Pick a number and let you friends to guess!", 1, black)
random_control = font_b.render("Press the up arrow", 1, black)
your_control = font_b.render("Press the down arrow", 1, black)
# image
up_arrow = pygame.image.load("up_arrow_new.png")
down_arrow = pygame.image.load("down_arrow_new.png")
def blit_img(img, x, y):
window.blit(img, (x, y))
# game
game = False
temp = True
while not game:
if temp:
window.blit(title, (10, 150))
window.blit(enter_to_start, (160, 350))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_KP_ENTER:
temp = False
window.fill(grass_green)
pygame.draw.rect(window, red, [50, 50, 540, 225])
pygame.draw.rect(window, blue, [50, 325, 540, 225])
window.blit(random_mode, (55, 65))
window.blit(des_random_1, (55, 150))
window.blit(des_random_2, (55, 200))
window.blit(random_control, (55, 240))
blit_img(up_arrow, 450, 150)
window.blit(your_mode, (55, 335))
window.blit(des_your, (55, 425))
window.blit(your_control, (55, 515))
blit_img(down_arrow, 450, 425)
pygame.display.flip()
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
if temp == False:
temp = True
window.fill(grass_green)
pygame.display.flip()
if event.key == pygame.K_ESCAPE:
game = True
if event.type == pygame.QUIT:
game = True
I want to make it just like a book or a PowerPoint to have pages. In this code, my problem is that the text which displayed cannot erase and blit the new text on it. Can someone help me? Thank you very much!!
I'd fill the window every frame and then render and blit the current text. The texts can be stored in a list and the current text can be accessed with an index variable that you increment in the event loop.
import pygame
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
window = pygame.display.set_mode((640, 600))
clock = pygame.time.Clock() # A clock to limit the frame rate.
font = pygame.font.Font(None, 57) # Use the default font.
texts = ['Hello', "what's", 'up?']
text_index = 0
done = False
while not done:
# Handle events.
for event in pygame.event.get():
# Allow the user to quit by clicking on the 'X' button.
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key in (pygame.K_KP_ENTER, pygame.K_RETURN):
# Make sure that we don't get an IndexError.
if text_index < len(texts)-1:
# Increment the index.
text_index += 1
# Insert additional game logic here.
# Finally draw everything.
window.fill(white) # Use fill to clear the window.
what_I_say = font.render(texts[text_index], True, black)
window.blit(what_I_say, (10, 150))
pygame.display.flip()
clock.tick(30) # Limit the frame rate to 30 FPS.
If you really only need to update the window when the user wants to switch to the next page (an event occurs), you could also use pygame.event.wait instead of pygame.event.get.
The way to remove previous stuff in pygame is to draw the background and everything you want to keep over it. In this case, you want to remove the Hello, so you will have to stop pygame from drawing it, and then draw the background over it
It should look like this:
import pygame
pygame.init()
#colour settings
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
#making a screen(display)
window = pygame.display.set_mode((640, 600))
window.fill(green)
# fonts settings
default_font = pygame.font.get_default_font()
font = pygame.font.Font(default_font, 57)
temp = True
#the things I want to display
while True:
if temp:
what_I_say = font.render("Hello", 1, white)
window.blit(what_I_say, (10, 150))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
temp = False
window.fill(green)
pygame.draw.rect(window, red, [50, 50, 540, 500])
The boolean temp marks whether to draw Hello. On pressing return (my keyboard doesn't have keypad enter), temp is set to false (so it will stop drawing Hello), the window is refilled with green (to cover the previous hello), and then a red rectangle is drawn.
I want to draw a polygon on screen, like so:
coords = (653,333),(680,444),(680,444),(653,445)
pygame.draw.polygon(screen, (0, 0, 255), coords, 0)
however this does not draw to the screen. Any help appreciated.
ANSWER: coords should be a list of tuples, so for my example the correct answer would be:
coords = [(653,333), (680,444), (680,444), (653,445)]
pygame.draw.polygon(screen, (0, 0, 255), coords, 0)
The code that you gave seems to be correct. I've tested it and it displays a triangle. As suggested by skrx in the comment your screen size is too small and you are not able to see it.
import pygame
pygame.init()
BLUE = (0, 0, 255)
size = [1920, 1080]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
while not done:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
temp = (653, 333), (680, 444), (680, 444), (653, 445)
pygame.draw.polygon(screen, BLUE, temp, 0)
pygame.display.flip()
pygame.quit()