Sunday, 21 April 2019

Pygame: Resizing a window

Pygame: Resizing a window

We already know how to create a game window using pygame. For creating a window, we use pygame.display. We set window size by setting the display mode in pygame. Now what if we want to make the window full screen? or we want to hide it, or resize it,
There are some meethods in pygame.display module

While setting the display mode, if we provide a third parameter, pygame.FULLSCREEN, we get a full screen window. For UNIX X11 driver there is one more function pygame.display.toggle_fullscreen() which works as switch between fullscreen mode and normal mode.


if you want to hide the window leaving only icon behind to get the access next time to the running window, use pygame.display.iconify() method.

A working example of all these functions, is in the following gist.



import pygame
from pygame.locals import *
winWidth=600
winHeight=480
color_CYAN=(0,255,255)
pygame.init()
caption="Empty Game Window"
pygame.display.set_caption(caption)
Game_Window=pygame.display.set_mode((winWidth,winHeight),pygame.FULLSCREEN)
Game_Window.fill(color_CYAN)
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
exit()
if event.type==KEYDOWN:
if event.key == K_ESCAPE:
print(pygame.display.toggle_fullscreen())
if pygame.display.toggle_fullscreen():
Game_Window=pygame.display.set_mode((winWidth,winHeight))
pygame.display.iconify()
pygame.display.update()
You might get confused that above script just gives an empty window that can be resizable but it works just the same as window with contents check out following gist
import pygame
import time
from pygame.locals import *
winWidth = 600
winHeight = 480
color_CYAN = (0, 255, 255)
color_BLACK = (0, 0, 0)
bkg_texture_str = "gameBkg.png"
shipAnimFrames_str = ["ripple0.png", "ripple1.png", "ripple2.png", "ripple3.png", "ripple4.png"]
clock = pygame.time.Clock()
class Ship(pygame.sprite.Sprite):
index = 0
def __init__(self, texture, animFrames, x=0, y=0, width=1, height=1):
super(Ship, self).__init__()
self.image = texture
self.animFrames = animFrames
self.currentFrame = self.animFrames[0]
self.x = x
self.y = y
self.rect = self.image.get_rect()
self.width = self.rect.width
self.height = self.rect.height
def update(self):
if self.isAnimating:
self.animate()
def draw(self, surface):
surface.blit(self.currentFrame, (self.x-self.width, self.y-self.height*0.2))
surface.blit(self.image, ((self.x), (self.y)))
def startAnimation(self):
self.isAnimating = True
def animate(self):
self.index += 1
if self.index >= len(self.animFrames):
self.index = 0
self.currentFrame = self.animFrames[self.index]
time.sleep(0.01)
def stopAnimation(self):
self.isAnimating = False
if __name__ == "__main__":
pygame.init()
caption = "Sprite Animation Test"
pygame.display.set_caption(caption)
Game_Window = pygame.display.set_mode((winWidth, winHeight),pygame.FULLSCREEN)
Game_Window.fill(color_CYAN)
ship = pygame.image.load("ship.png").convert_alpha()
pygame.display.set_icon(ship)
bush = pygame.transform.scale((pygame.image.load("bush.png")), (50, 50))
bushes = []
shipAnimFrames = []
i = 0
while i < 5:
bushes.append(bush.copy())
i += 1
i = 0
while i < len(shipAnimFrames_str):
shipAnimFrames.append((pygame.image.load(shipAnimFrames_str[i])))
i += 1
bkg_texture = pygame.transform.scale((pygame.image.load(bkg_texture_str)), (winWidth, winHeight))
ShipSprite = Ship(ship, shipAnimFrames, winWidth*0.45, winHeight*0.75)
ShipSprite.startAnimation()
y = winHeight*0.7
while True:#the game loop
if y > 0:
y -= 1
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type==KEYDOWN:
if event.key == K_ESCAPE:
print(pygame.display.toggle_fullscreen())
if pygame.display.toggle_fullscreen():
Game_Window=pygame.display.set_mode((winWidth,winHeight))
pygame.display.iconify()
ShipSprite.update()
Game_Window.fill(color_CYAN)
Game_Window.blit(bkg_texture, (0, 0))
i = 0
while i < len(bushes):
if i % 2 == 0:
Game_Window.blit(bushes[i], (winWidth*0.03, 50*i))
else:
Game_Window.blit(bushes[i], (winWidth*0.83, 50*i))
i += 1
ShipSprite.draw(Game_Window)
clock.tick(60)
pygame.display.flip()

No comments:

Post a Comment

Your comments and suggestions are valuable and are always welcome and will be helpful to me to create a good quality content. Please leave your thoughts