I am new to Python programming and I recently started working with the PyGame module. Here is a simple piece of code to initialize a display screen. My question is : Currently, the maximize button is disabled and I cannot resize the screen. How do I enable it to switch between full screen and back?
Thanks
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300))
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
To become fullscreen at native resolution, do
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
To make the window resizeable, add the pygame.RESIZABLE parameter when seting the mode. You can set the mode of the screen surface multiple times but you might have to do pygame.display.quit() followed by pygame.display.init()
You should also check the pygame documentation here http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode
The method you are looking for is pygame.display.toggle_fullscreen
Or, as the guide recommends in most situations, calling pygame.display.set_mode() with the FULLSCREEN tag.
In your case this would look like
DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)
(Please use the pygame.FULLSCREEN instead of just FULLSCREEN because upon testing with my own system FULLSCREEN just maximized the window without fitting the resolution while pygame.FULLSCREEN fit my resolution as well as maximizing.)
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()
pygame.init()
info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h
These are the dimensions of your screen/monitor. You can use these or reduce them to exclude borders and title bar:
window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()
This will let you toggle from maximize to the initial size
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
#Below line will let you toggle from maximize to the initial size
DISPLAYSURF = pygame.display.set_mode((400, 300), RESIZABLE)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
You'd have to add the full screen parameter onto the display declaration like this:
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300), FULLSCREEN)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
Just use pygame.RESIZABLE as an easier option.
Related
My pygame window closes without any errors right after I run the code
Here's my code:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
If anyone can help, thank you in advance :)
See Why is my PyGame application not running at all? Your application will exit right after the window is created. You have to implement an application loop. You must implement an application loop to prevent the window from closing immediately:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill((0, 0, 0))
# draw scene
# [...]
pygame.display.flip()
pygame.quit()
I am trying to write my first game using Python3 & Pygame in the Pycharm Community IDE. I am trying to make a simple Pong game :-D lol I am a beginner so please help me if you can.
When I run my code, I am getting the following error:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/home/TechSlugz/PycharmProjects/Pong-Project/Pong1.py", line 7, in <module>
clock = pygame.time.clock()
AttributeError: module 'pygame.time' has no attribute 'clock'
Process finished with exit code 1
The code is just the basic set up for a game window, check it out
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.setmode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)
Can you notice anything that I am doing wrong to get this issue? None of my IDEs seem to recognise time as a pygame module but I am pretty damn sure it is...
The clock is a class, so you should use C instead of c.
clock = pygame.time.Clock()
and you have another problem, setmode is not an available function. You meant to write set_mode
The code that works fine for me:
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.Clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)
This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
pygame.error: video system not initialized
(5 answers)
Closed 1 year ago.
I'm trying to do one of the projects in the Python Crash Course book: Alien Invasion Chapter 12. I just started and for some reason the error: pygame.error: video system not initialized keeps popping up. I'm pretty sure I followed the directions appropriately so I don't know what I have possibly done wrong...?
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen.fill(ai_settings.bg_color)
pygame.quit()
sys.exit()
pygame.display.flip()
run_game()
The error occurs because pygame.event.get is called before you initialize the display with pygame.display.set_mode.
It's very important in Python to indent your code correctly. Your program should most likely look similar to this version:
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
# This block should be in the `run_game` function.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Fill the screen in the while loop not in the event
# loop. It makes no sense to fill the screen only when
# the user quits.
screen.fill(ai_settings.bg_color)
pygame.display.flip()
run_game()
When I drag the mouse over the image, the image traces a path with the mouse instead of moving. See the screenshot for an example of this behavior:
Here is the code:
import pygame
import sys
from pygame.locals import *
pygame.display.init()
clock=pygame.time.Clock()
canvas=pygame.display.set_mode((500,500))
pygame.display.set_caption("hy")
color=(255,0,0)
canvas.fill(color)
pygame.mouse.set_visible(1)
pygame.mouse.set_cursor(*pygame.cursors.diamond)
img=pygame.image.load("maryo.png")
while True:
x,y=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
canvas.blit(img,(x,y))
clock.tick(60)
pygame.display.update()
If you donĀ“t want a tracing path, you need to clear the screen before you blit the img-surface onto a new position in your main game loop.
This can be done by calling the fill() method of the main screen object canvas:
while True:
x,y=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
canvas.fill(color) #clear the screen
canvas.blit(img,(x,y)) #blit img onto screen
clock.tick(60)
pygame.display.update()
When i run the code it play only the .wav file
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)
pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()
soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
pygame.mixer.music.stop()
someone know what i should do for the background.ogg to be played too?
Remove this line:
pygame.mixer.music.stop()
Basically, you're loading and playing the background noise, and then immediately stopping it. I also suggest you create a main loop, like so:
import pygame
from pygame.locals import *
# This makes sure that you're not importing the module.
if __name__ == "__main__":
# Create surface
size = width, height = (500, 400)
# You don't need the extra arguments
window = pygame.display.set_mode(size)
# Do sound stuff
pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()
soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
# This is your main loop.
running = True
while running:
# Check if escape was pressed
Most of the time people just check if Escape was pressed in their main loop (if it was pressed, they set running = False and exit the program).