Nishiyuu
Home Projects

Goose game

Introduction


”Goose” is my first 2D video game that I developed during a free Python marathon at the GoIT school. During this marathon, we learned the basics of Python programming language syntax and how to integrate other libraries into our projects. The library used for developing the video game is called “Pygame.” It is quite popular among beginners in game development as it provides a straightforward syntax and clear documentation.

pygame

Description of the Pygame Library


Pygame is a library for programming games in the Python programming language. It provides a powerful and simple interface for creating 2D games and interactive applications. Pygame is based on the SDL (Simple DirectMedia Layer) library and gives Python access to its functionality through a convenient API.

Key Components of Pygame


Using Pygame


Popularity of Pygame Among Programmers


Pygame is very popular among programmers because it provides a convenient and powerful toolkit for creating various games and interactive applications. It is suitable for beginners in game development as well as experienced developers looking for a quick and efficient way to create games in Python.

Installing Pygame


In order to install pygame, you must first install Python.

Windows version | macOS version

Linux:


sudo apt-get install python3

and install pygame:


pip install pygame

creating a new project



import pygame


pygame.init()


screen = pygame.display.set_mode((width, height))

Game loop



while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.display.flip()

Result


After completing the development of the “Goose Game with GoIT,” the following results were achieved:

  1. Creation of a first 2D video game: Successfully created a full-fledged 2D video game, which is a first experience in game development using the Python programming language and the Pygame library.
  2. Programming and design skills: Gained valuable practical experience in programming in Python and in developing graphical design, object interaction, and game environment interaction during game development.
  3. Using the Pygame library: Mastered the use of the Pygame library for creating graphical games, animations, and handling user events.

Conclusion


The process of developing the “Goose Game with GoIT” was a significant step in learning and practically applying programming and game development knowledge. Using the Pygame library allowed creating an interesting and attractive game with unique gameplay. This project not only showcased my programming skills but also helped understand the fundamental principles of game development and influenced my desire to grow in this field. Such experience is crucial for my future professional growth and success in the game development industry.


View Source code This is the main file:
import random
import os
import pygame
from pygame.constants import QUIT, K_DOWN, K_LEFT, K_RIGHT, K_UP

pygame.init()

FPS = pygame.time.Clock()

HEIGHT = 800
WIDTH = 1200

FONT = pygame.font.SysFont('Verdana', 20)

COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
COLOR_BLUE = (0, 0, 255)
COLOR_YELLOW = (255, 255, 0)

main_display = pygame.display.set_mode((WIDTH, HEIGHT))

bg = pygame.transform.scale(pygame.image.load('background.png'), (WIDTH, HEIGHT))
bg_x1 = 0
bg_x2 = bg.get_width()
bg_move = 3

image_path = "goose"
player_image = os.listdir(image_path)

player_size = (20, 20)
player = pygame.image.load('player.png').convert_alpha()
player_rect = player.get_rect().move(0, HEIGHT / 2)
player_move_down = [0, 4]
player_move_up = [0, -4]
player_move_right = [4, 0]
player_move_left = [-4, 0]

def create_enemy():
    enemy_size = (30, 30)
    enemy = pygame.image.load('enemy.png').convert_alpha()
    enemy_rect = pygame.Rect(WIDTH, random.randint(enemy.get_height(), HEIGHT - enemy.get_height()), *enemy_size)
    enemy_move = [random.randint(-8, -4), 0]
    return [enemy, enemy_rect, enemy_move]

def create_bonus():
    bonus_size = (30, 30)
    bonus = pygame.image.load('bonus.png').convert_alpha()
    bonus_rect = pygame.Rect(random.randint(bonus.get_width(), WIDTH - bonus.get_width()), 0, *bonus_size)
    bonus_move = [0, random.randint(2, 6)]
    return [bonus, bonus_rect, bonus_move]

CREATE_ENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(CREATE_ENEMY, 1500)

CREATE_BONUS = pygame.USEREVENT + 2
pygame.time.set_timer(CREATE_BONUS, 5000)

CHANGE_IMAGE = pygame.USEREVENT + 3
pygame.time.set_timer(CHANGE_IMAGE, 200)


enemies = []
bonusies = []

image_index = 0

score = 0

playing = True

while playing:
    FPS.tick(120)

    for event in pygame.event.get():
        if event.type == QUIT:
            playing = False
        if event.type == CREATE_ENEMY:
            enemies.append(create_enemy())
        if event.type == CREATE_BONUS:
            bonusies.append(create_bonus())
        if event.type == CHANGE_IMAGE:
            player = pygame.image.load(os.path.join(image_path, player_image[image_index]))
            image_index += 1
            if image_index >= len(player_image):
                image_index = 0
            

    bg_x1 -= bg_move
    bg_x2 -= bg_move
    
    if bg_x1 < -bg.get_width():
        bg_x1 = bg.get_width()

    if bg_x2 < -bg.get_width():
        bg_x2 = bg.get_width()

    main_display.blit(bg, (bg_x1, 0))
    main_display.blit(bg, (bg_x2, 0))

    keys = pygame.key.get_pressed()

    if keys[K_DOWN] and player_rect.bottom < HEIGHT:
        player_rect = player_rect.move(player_move_down)

    if keys[K_RIGHT] and player_rect.right < WIDTH:
        player_rect = player_rect.move(player_move_right)

    if keys[K_LEFT] and player_rect.left > 0:
        player_rect = player_rect.move(player_move_left)
    
    if keys[K_UP] and player_rect.top > 0:
        player_rect = player_rect.move(player_move_up)

    for enemy in enemies:
        enemy[1] = enemy[1].move(enemy[2])
        main_display.blit(enemy[0], enemy[1])

        if player_rect.colliderect(enemy[1]):
            playing = False
    
    for bonus in bonusies:
        bonus[1] = bonus[1].move(bonus[2])
        main_display.blit(bonus[0], bonus[1])
        
        if player_rect.colliderect(bonus[1]):
            bonusies.pop(bonusies.index(bonus))
            score += 1

    main_display.blit(player, player_rect)
    main_display.blit(FONT.render(str(score), True, COLOR_WHITE), (WIDTH - 50, 20))

    for enemy in enemies:
        if enemy[1].left < 0:
            enemies.pop(enemies.index(enemy))

    for bonus in bonusies:
        if bonus[1].top > HEIGHT:
            bonusies.pop(bonusies.index(bonus))

    pygame.display.flip()
Report Issues

To report issues or provide feedback, please enter your problem in the console below and click “Send”.