Rotate pygame screen - python

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()

Related

How to laterally invert images in pygame?

I'm trying to use a sprite sheet, however it only contains images of the character facing right, I want to laterally invert these images when making the character move left. I can't figure out how to do so, if anyone knows how please feel free to let me know!
You can flip an image with pygame.transform.flip:
flipped_image = pygame.transform.flip(image, True, False)
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 150))
clock = pygame.time.Clock()
image = pygame.image.load('bird.png').convert_alpha()
flipped_image = pygame.transform.flip(image, True, False)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
window.blit(image, image.get_rect(center = (75, 75)))
window.blit(flipped_image, flipped_image.get_rect(center = (225, 75)))
pygame.display.flip()
clock.tick(60)
pygame.quit()
exit()

Why my rectrangle does not appere(python 3.8 windoes 7) [duplicate]

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

Display differently styled bulletpoints with pygame

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))

Remove black border in pygame fullscreen

I am creating a game using pygame 1.9.6. But, as you can see by running this simple example, some black borders appear around the window, even with the FULLSCREEN flag.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((900, 500), FULLSCREEN)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill((255, 255, 255))
pygame.display.flip()
I tried to add the flag NOFRAME in the screen initialisation, but it didn't work.
I wonder if it is possible to remove the border, for example by increasing the screen size to just fit in the current screen. But I also want to keep the 900x500 resolution.
Can pygame resize the entire screen? Do I have to blit() everything on a Surface, then rescale it and draw it on the actual screen?
Thanks #Glenn Mackintosh!
The best way to remove the black border is actually to add the SCALED flag in the screen declaration like this:
screen = pygame.display.set_mode((900, 500), FULLSCREEN|SCALED)
But to use it, the pygame version 2.0.0 or more is required.
The only way to remove the black bars is to set the pygame.display.set_mode((width, height)) to the users resolution. Use pygame.display.Info() to get the users' display resolution. Furthermore, you can have a preferred resolution such as 900 x 500 to get a "differential number" to get a retro/pixelated look.
resolution = pygame.display.Info()
width = resolution.current_w
height = resolution.current_h
dx = width // 900
dy = height // 500
This dx and dy can then be used to scale everything to a bigger size.
I would try this code:
import pygame
from pygame.locals import *
pygame.init() # You forgot the initialize the pygame window!
resolution = pygame.display.Info() # Get the users resolution
width = resolution.current_w
height = resolution.current_h
screen = pygame.display.set_mode((width, height), FULLSCREEN, 0, 32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill((255, 255, 255))
mouse_pos = pygame.mouse.get_pos()
pygame.draw.circle(screen, (0, 0, 255), mouse_pos, 20)
pygame.draw.circle(screen, (0, 0, 200), mouse_pos, 18)
pygame.draw.circle(screen, (0, 0, 100), mouse_pos, 15)
pygame.draw.circle(screen, (0, 0, 50), mouse_pos, 10)
pygame.display.flip()

Python- Pygame won't display images or captions

My code runs with no errors, but the background image does not display and neither does the caption at the top of the window. I have checked that my script is importing the correct pygame module, and tried different images. I have Python 3.6.2 on a Mac.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Users/MyMacbook/Desktop/pac-man/background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()
Yeah it appears that #skrx is correct, there's something blocking by not clearing the event buffer. This code works for me:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Path/To/Background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()

Categories