Creating a new Window in pygame

share link

by kanika dot icon Updated: Mar 29, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Creating a window using pygame means using the pygame library to create a graphical window for a game or other graphical application. This window can display graphics and receive user input from the keyboard and mouse. 


  • Pygame is a free and open-source library of Python modules designed for writing video games. It provides functions such as sound, graphics, and input, allowing programmers to create fully-featured games and multimedia programs using Python. Pygame is portable and runs on every platform and operating system. 


You can create a window in pygame by following some basic steps: 

  • Import the Pygame module. Initialize the game engine 
  • Set the window size. Create the window and Set the window title 
  • Load images, sounds, and other assets and also Set up the game loop 
  • Handle events and Update game logic 
  • Draw the game and then Refresh the screen 


Here is an example of Creating a new window with pygame. 



Fig 1: Preview of the output that you will get on running this code from your IDE

Code


In this solution, we create a new window with pygame

import pygame
import pygame.freetype

# So our game has 2 states.
# Either we're in the world and run around;
# or we're displaying a menu and the player has to make a choice.
WORLD = 0
MENU = 1

# from https://www.pygame.org/docs/ref/freetype.html#pygame.freetype.Font.render_to
def word_wrap(surf, text, font, color=(0, 0, 0)):
    font.origin = True
    words = text.split(' ')
    width, height = surf.get_size()
    line_spacing = font.get_sized_height() + 2
    x, y = 0, line_spacing
    space = font.get_rect(' ')
    for word in words:
        bounds = font.get_rect(word)
        if x + bounds.width + bounds.x >= width:
            x, y = 0, y + line_spacing
        if x + bounds.width + bounds.x >= width:
            raise ValueError("word too wide for the surface")
        if y + bounds.height - bounds.y >= height:
            raise ValueError("text to long for the surface")
        font.render_to(surf, (x, y), None, color)
        x += bounds.width + space.width
    return x, y

# This sprite handles the menu.
# It renders a box and a text and listens for key presses.
# If a key we're interessed in is pressed, we call the callback function.
class TextMenu(pygame.sprite.Sprite):
    def __init__(self, font, text, listen_to, callback):
        super().__init__()
        self.image = pygame.Surface((400, 400))
        self.image.fill(pygame.Color('white'))
        self.image.fill(pygame.Color('black'), self.image.get_rect().inflate((-50, -50)))
        self.rect = self.image.get_rect(topleft=(50, 50))
        word_wrap(self.image.subsurface(self.image.get_rect().inflate((-100, -100))), text, font, pygame.Color('white'))
        self.callback = callback
        self.listen_to = listen_to

    def update(self, events, dt):
        for e in events:
            if e.type == pygame.KEYDOWN and e.key in self.listen_to:
                self.callback(self, e.key)

# This sprite represents a building the player can "walk in" to trigger 
# a menu pop up. In this case, we want the user to either press 1 or 2.
# Then we change the color, because why not, something should happen.
class House(pygame.sprite.Sprite):
    def __init__(self, pos, player, show_text):
        super().__init__()
        self.image = pygame.Surface((64, 64))
        self.image.fill(pygame.Color('darkred'))
        self.rect = self.image.get_rect(center=pos)
        self.show_text = show_text
        self.player = player
        # Since the menu is triggered when the player touches the building,
        # we don't want an endless loop, so we need a flag that prevents
        # the menu until the player "leaves the building"
        self.triggered = False

    def change_color(self, key):
        if key == pygame.K_1:
            self.image.fill(pygame.Color('yellow'))
        if key == pygame.K_2:
            self.image.fill(pygame.Color('darkblue'))

    def update(self, events, dt):
        if pygame.sprite.collide_rect(self, self.player):
            if not self.triggered:
                self.show_text('Welcome, little blue rect. Please press (1) or (2).', (pygame.K_1, pygame.K_2), self.change_color)
                self.triggered = True
        else:
            self.triggered = False

# This is the player. 
# Does basically nothing but run around
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((32, 32))
        self.image.fill(pygame.Color('dodgerblue'))
        self.rect = self.image.get_rect()
        self.pos = pygame.Vector2((100, 200))

    def update(self, events, dt):
        pressed = pygame.key.get_pressed()
        move = pygame.Vector2((0, 0))
        if pressed[pygame.K_w]: move += (0, -1)
        if pressed[pygame.K_a]: move += (-2, 0)
        if pressed[pygame.K_s]: move += (0, 2)
        if pressed[pygame.K_d]: move += (2, 0)
        if move.length() > 0: move.normalize_ip()
        self.pos += move*(dt/5)
        self.rect.center = self.pos

def main():
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    font = pygame.freetype.SysFont(None, 32)
    clock = pygame.time.Clock()
    dt = 0

    player = Player()

    # keep track of the state we're in.
    # we start in the WORLD state, a.k.a. running around.
    # the state just tells us which sprites are "active", 
    # a.k.a. if they are updated by calling thier update function
    state = WORLD

    # sprite group for all MENU-sprites
    menu_sprites = pygame.sprite.Group()

    # sprite group for all WORLD-sprites
    sprites = pygame.sprite.Group(player)

    # this function allows other sprites to trigger a menu
    def show_text(text, listen_to, callback):

        # this function is called by the menu.
        # we change the state back to world and kill the TextMenu sprite
        def wrapped_callback(sprite, *args):
            nonlocal state
            state = WORLD
            callback(*args)
            sprite.kill()

        # so when this function is called , let's switch to the MENU state
        nonlocal state
        state = MENU
        # add the TextMenu sprite to the menu_sprites group so it "lives"
        menu_sprites.add(TextMenu(font, text, listen_to, wrapped_callback))

    # create some buildings. They are all the same...
    for pos in ((300, 300), (200, 400), (100, 100)):
        sprites.add(House(pos, player, show_text))

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        # see which sprites are "active". The WORLD sprites or the MENU sprites
        if state == WORLD:
            sprites.update(events, dt)
        else:
            menu_sprites.update(events, dt)

        screen.fill((30, 30, 30))
        sprites.draw(screen)
        menu_sprites.draw(screen)
        pygame.display.update()
        dt = clock.tick(60)

if __name__ == '__main__':
    main()

Instructions

Follow the steps carefully to get the output easily.

  1. Install Jupyter Notebook on your computer.
  2. Open terminal and install the required libraries with following commands.
  3. Install pygame - pip install pygame.
  4. Copy the code using the "Copy" button above, and paste it into your IDE's Python file.
  5. Run the file to create a new window using pygame.



I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for "Creating a window with pygame" in kandi. You can try any such use case!

Dependent Libraries

pygameby pygame

C doticonstar image 6066 doticonVersion:2.5.0.dev2doticon
no licences License: No License (null)

🐍🎮 pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.

Support
    Quality
      Security
        License
          Reuse

            pygameby pygame

            C doticon star image 6066 doticonVersion:2.5.0.dev2doticonno licences License: No License

            🐍🎮 pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.
            Support
              Quality
                Security
                  License
                    Reuse

                      If you do not have Pygame that is required to run this code, you can install it by clicking on the above link and copying the pip Install command from the Pygame page in kandi.


                      You can search for any dependent library on kandi like pygame

                      Environment Tested


                      I tested this solution in the following versions. Be mindful of changes when working with other versions.

                      1. The solution is created in Python 3.9.6
                      2. The solution is tested on pygame version 2.3.0


                      Using this solution, we are able to create a new window with pygame. It is also used for creating graphical applications.

                      See similar Kits and Libraries