Pygame: platformer 1

I am going to follow the tutorial of dafluffypotato, getting inspiration from his tutorial and trying to figure out a platform with pygame and Python.

Here is the png image for the player. Right click and save it in the folder where you saved the code of the game.

In the following code I resemble the code from the first 2 videos starting from skratch till we make the player move, jump and interact with a rectangle on the screen. This is the code:

import pygame
from pygame.locals import *

#pip install pygame into cmd, use 3.7 21/12/2019

pygame.init()
clock = pygame.time.Clock()
WINDOW_SIZE = (400, 400)
pygame.display.set_caption("Game")
screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
# Image
player_image = pygame.image.load("player.png")
# other variables for movements
location = [50, 50]
moving_right = False
moving_left = False

# collitions
# rectangle that wraps the player
player_rect = pygame.Rect(location[0], location[1], player_image.get_width(), player_image.get_height())
# another rectangle for test
test = pygame.Rect(100, 100, 100, 50)
momentum = 0

loop = 1
while loop:
    # clear the screen
    screen.fill((146, 244, 255))
    # show the player
    screen.blit(player_image, location)

    # check if the player collide the test rectangle
    if player_rect.colliderect(test):
        print("collided")
        # if they collide the rectangle goes red
        pygame.draw.rect(screen, (255, 0, 0), test)
    else:
        # if not it is drawed in black
        pygame.draw.rect(screen, (0, 0, 0), test)

    player_rect.x = location[0]
    player_rect.y = location[1]

    # Goes down until reaches 0 then goes up til top and down again...
    if location[1] > WINDOW_SIZE[1] - player_image.get_height():
        momentum = -momentum
    else:
        momentum += 0.2
    location[1] += momentum

    if moving_right == True:
        location[0] += 4

    if moving_left == True:
        location[0] -= 4

    for event in pygame.event.get():
        if event.type == QUIT:
            loop = 0
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False

    pygame.display.update()
    clock.tick(60)

pygame.quit()

Live coding

How can it become… a look at a basic map

I just wanna show a little more graphic in the game to see how can it become. To make the maps is very easy.

Some explanations

The collisions. The most important stuff in a game is the collision detection, that makes us know when two elements collide so that we can make something happen like collect an item, damage something, increase score etc. In pygame you create a rectangle area that wraps the items whose position needs to be controlled to check if they collides.

In the example above the two rectangle are here:

# collitions
# rectangle that wraps the player
player_rect = pygame.Rect(location[0], location[1], player_image.get_width(), player_image.get_height())
# another rectangle for test
test = pygame.Rect(100, 100, 100, 50)

The first surround the player the second is just a rectangle. as you can see in the video, when the player touches the rectangle it changes color. The rectangles are created with the Rect class of pygame that takes the coordinates of the starting point of the rectangle (from the player position that we stored in location) and the width and height of the image of the player (player_image) obtained with the method get_width() and get_height() of the pygame.image.load object. This code is in the while loop that loops 60 time each second.

    # check if the player collide the test rectangle
    if player_rect.colliderect(test):
        print("collided")
        # if they collide the rectangle goes red
        pygame.draw.rect(screen, (255, 0, 0), test)
    else:
        # if not it is drawed in black
        pygame.draw.rect(screen, (0, 0, 0), test)

The control is in the if statement that checks if the player_rect collides with the other (test) using the method with the intuitive name of colliderect that has as argument the other rectangle object. The test object created with pygame.Rect is drawed in red or black each frame if there is a collision or not.

Other posts about pygame

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.