SoFunction
Updated on 2024-11-16

Teach you to set window titles and icons with Python pygame

Window Title

.set_caption(title, icontitle=None)
'''
- title sets the content of the window's title
- icontitle sets the sub-title after graphing.
† icontitle is optional, some systems do not have it, and it is usually not set.
'''

在这里插入图片描述

.get_caption()
'''
- return the title of the current settings window and the content of the sub-title
- The return structure is (title, icontitle).
- This function works with the game interaction logic to modify the title according to the game plot.
'''

Setting Icons

.set_icon(surface)
'''
- Setting up icon effects for windows
- An icon is a Surface object
'''

Games with icons

I changed the icon to my CSDN avatar Format: (128px*128px png format)

在这里插入图片描述

Imported images are set as icons.

在这里插入图片描述

import pygame,sys

()
icon = ("img/")
.set_icon(icon)     #Setup Icons
v = ()
size = width,height = 600,400
speed = [1,1]
BLACK = 0, 0, 0
s = .set_mode(size,)
.set_caption("Hi, funny.")
ball = ("img/")
ballrect = ball.get_rect()
fps = 200
fclock = ()
while True:
    for event in ():
        if  == :
            ()
        elif  == :
            if  == pygame.K_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
            elif  == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif  == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif  == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
            elif  == pygame.K_ESCAPE:     # Get ESC Exit when pressed
                ()
        elif  == :
            size = width,height = ,
            s = .set_mode(size,)
    ballrect = (speed)
    if  < 0 or  > width:
        speed[0] = - speed[0]
    if  < 0 or  > height:
        speed[1] = - speed[1]
    .get_caption()
    (BLACK)
    (ball, ballrect)
    ()
    (fps)

on-screen control

.get_active()
'''
- Returns True when the window is displayed in the system (screen-drawn/non-iconified), otherwise returns False
.get_active()
- This function can be used to determine if the game window is minimized.
- Further, it can be used to pause the game, change the response mode, and so on.
'''

refresh (computer window)

()
# - redraw the entire window
()
#• Redraw only the areas of the window that have changed,compare.flip()Faster execution

judgment form

If the form is minimized, the ball stops moving. Just add this condition before the ball movement code

在这里插入图片描述

to this article about teaching you to use Python pygame set the window title and icon of the article is introduced to this, more related pygame set the window title and icon content, please search for my previous posts or continue to browse the following related articles I hope that you will support me more in the future!