Reopening a game fullscreen is being broken in pygame - python

I am trying to make a 700x700 fullscreen game in pygame, but whenever I get out of the game and get back in, the screen breaks, showing what was opened before reopening the game. How do I fix it without changing the width and height of the screen?
Width, Height = 700, 700
win = pygame.display.set_mode((Width, Height), pygame.FULLSCREEN)

You will have to reset the fullscreen modifier whenever your window gets activated again. This might be a bug in a new version of pygame.
You can detect when the game gets maximized with pygame.ACTIVEEVENT. You can put something like this in your event loop:
if event.type == pygame.ACTIVEEVENT:
if event.gain == 1:
win = pygame.display.set_mode((Width, Height), pygame.FULLSCREEN)

Related

Python Can't load background images in pygame

i try to load backgroud.png using pygame.image.load(),but i get nothing.here is my code,please help me ,thanks.
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("./images/background.png")
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
while True:
pass
pygame.quit()
here is my sreen:it get nothing
With a game you're making all stuff that needs to be refreshed needs to be in the main game loop, your problem is, is you are drawing the image outside that game loop, meaning it gets drawn once then cleared and never drawn again.
To fix your code this is how you would write it:
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("./images/background.png")
while True:
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
pygame.quit()
But notice how the bg=pygame.image... is outside the loop, this is because if it was inside the loop it would create a new instance of that image every time the loop happens.
The main game loop works by looping through all your functions and other stuff and then doing again and again, and again.
A game loop is how fps works, basically it is the measurement of how many times per second that game loop happens.
Make sure whenever you are doing anything in the loop it actually has a place there, for example loading an image doesn't, but updating where the player is on the screen does.
If you want to have a look at a good game loop that can be applied to most game engines this website will help you. But don't look at the most complex one when using pygame as it isn't built for that. Fix Your Timestep!
But your original problem of the image not loading isn't the case, it was loading but you were drawing your image in the wrong way, if you want a basic tutorial on pygame watch these videos: Game Development in Python 3 With PyGame - 1 - Intro
A better system to ease development
import pygame
bg = None
def load_resources():
bg = pygame.image.load("./images/background.png")
# all other resources
def render():
screen.blit(bg, (0,0))
def update():
# all logic updates for example movement of entities.
### start of game
load_resources()
while True:
update()
render()
pygame.display.update()
pygame.quit()
I think you should not write pass inside while loop because of it the output window will stop responding. Also you should write the screen blit and display.update inside while loop and you should write the correct image extension in the path. You can also write the full path of the file like ==> pygame.image.load(r"C:\Users\Desktop\back_ground.jpg")
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("back_ground.jpg")
while True:
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

PyGame draws a blank window until quit

Here is my code:
import pygame
pygame.init()
screen = pygame.display.set_mode((200,200))
clock = pygame.time.Clock()
while True:
color = (100,100,200)
screen.fill(color)
pygame.display.flip()
clock.tick(60)
I think this should create a 200x200 window and fill it with the RGB color (100,100,200). Instead of this, it creates a blank 200x200 window. When I control-c to quit, the window flashes the correct color for one or two frames then closes. Why is this?
Thanks to siliconwolf on the PyGame Discord server, here is the answer:
If you don't process PyGame events with pygame.event.get(), then you must use pygame.event.pump() at the beginning of the loop or the window will freeze. However, pygame.event.get() is strongly preferred because you may have issues closing the window otherwise.

Opening a resizable window in Pygame in Fullscreen

I'm making a firework simulation in pygame, and I want to be able to run the program, and have it open up in full screen. Not the pygame.FULLSCREEN, I still want to be able to use pygame.QUIT.
I don't know if this is possible, but if anyone could help, please share your ideas!
Here's my code for the screen:
import pygame
screen = pygame.display.set_mode((0, 0), pygame.RESIZABLE)
You could use the width and height of the screen to setup the resolution, like in this post, by using the VideoInfo object provided by pygame:
import pygame
pygame.init()
video_infos = pygame.display.Info()
width, height = video_infos.current_w, video_infos.current_h
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
# [...]

Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.

The image is a playing card. We are using pygame 4.5 community edition and pycharm 2.6.9 because 2.7 does not support pygame (this is a school). Here is the code:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
pygame.display.update()
Why does the window disappear?
Try this:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
while True:
main_surface.blit(picture, (0,0))
pygame.display.update()
pygame.display.update() updates a frame. There are multiple frames per second depending on what what you draw onto the surface.
The problem is, after you update the screen with pygame.display.update(), you do nothing, and your program simply ends. pygame.display.update() does not block.
You need what is usually called a main loop. Here's a simple example with event handling:
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
# display.set_mode already returns the screen surface
screen = pygame.display.set_mode(picture.get_size())
# a simple flag to show if the application is running
# there are other ways to do this, of course
running = True
while running:
# it's important to get all events from the
# event queue; otherwise it may get stuck
for e in pygame.event.get():
# if there's a QUIT event (someone wants to close the window)
# then set the running flag to False so the while loop ends
if e.type == pygame.QUIT:
running = False
# draw stuff
screen.blit(picture, (0,0))
pygame.display.update()
This way, your application does not, only when someone closes the window.

How to click on an element and then use that to change another element

I'm still a noob to python and now im just getting started with my GUI's. Right now I have a bunch of elements on a screen, and I want to know how I can register a click on one element, and then use that to change both the element that was clicked on and another element. To put it into context, I'm making a light switch. I'll post the code I have so far as it isn't too chunky.
#Import pygame lib
import pygame
from pygame.locals import *
#Initialize the game
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
#Load images
switchOn = pygame.image.load("resources/img/switchOn.png")
switchOff = pygame.image.load("resources/img/switchOff.png")
bulbOn = pygame.image.load("resources/img/bulbOn.png")
bulbOff = pygame.image.load("resources/img/bulbOff.png")
#Loop
while 1:
#clear screen before drawing again
screen.fill(0)
#draw the screen elements
screen.blit(bulbOff, (50,50))
screen.blit(switchOff, (300,250))
#update the screen
pygame.display.flip()
#loop the events
for event in pygame.event.get():
#check if event is X button
if event.type==pygame.QUIT:
pygame.quit()
exit(0)
To register a click on, say, a button, you need to use the pygame.Rect.collidepoint (http://pygame.org/docs/ref/rect.html#pygame.Rect.collidepoint) and check if the current mouse position is within the rectangle of our button.
In pseudo code:
check for events
if event is mouseclick
if button.rect.collidepoint(button, mouse_pos)
# player clicked, so...
do something
Creating a clickable button is not much trouble, to be honest, but it might be a bit rough if you're new at Python and PyGame.

Categories

Resources