logo


Tag: pygame

Jump and run in Pygame

In this article you will learn how to implement jump and run logic in Pygame. To do so, we will implement the player logic.

You may like
Teach Your Kids to Code: Learn Python Programming at Any Age

Movement
Moving left and right is similar to the previous tutorial and simply mean changing the (x,y) position of the player. For jumping, we use a formula from classical mechanics:

F = 1/2 * m * v^2

Where F is the force up/down, m is the mass of your object and v is the velocity. The velocity goes down over time because when the player jumps the velocity will not increase more in this simulation. If the player reaches the ground, the jump ends. In Python, we set a variable isjump to indicate if the player is jumping or not. If the player is, its position will be updated according to the above formula.

Full Code:

from pygame.locals import *
import pygame
import math
from time import sleep

class Player:
x = 10
y = 500
speed = 10

# Stores if player is jumping or not.
isjump = 0

# Force (v) up and mass m.
v = 8
m = 2

def moveRight(self):
self.x = self.x + self.speed

def moveLeft(self):
self.x = self.x - self.speed

def jump(self):
self.isjump = 1

def update(self):
if self.isjump:
# Calculate force (F). F = 0.5 * mass * velocity^2.
if self.v > 0:
F = ( 0.5 * self.m * (self.v*self.v) )
else:
F = -( 0.5 * self.m * (self.v*self.v) )

# Change position
self.y = self.y - F

# Change velocity
self.v = self.v - 1

# If ground is reached, reset variables.
if self.y >= 500:
self.y = 500
self.isjump = 0
self.v = 8


class App:

windowWidth = 800
windowHeight = 600
player = 0

def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self.player = Player()

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

pygame.display.set_caption('Pygame pythonspot.com example')
self._running = True
self._image_surf = pygame.image.load("pygame.png").convert()

def on_event(self, event):
if event.type == QUIT:
self._running = False

def on_loop(self):
pass

def on_render(self):
self._display_surf.fill((0,0,0))
self._display_surf.blit(self._image_surf,(self.player.x,self.player.y))
self.player.update()
pygame.display.flip()
sleep(0.03)

def on_cleanup(self):
pygame.quit()

def on_execute(self):
if self.on_init() == False:
self._running = False

while( self._running ):
pygame.event.pump()
keys = pygame.key.get_pressed()

if (keys[K_RIGHT]):
self.player.moveRight()

if (keys[K_LEFT]):
self.player.moveLeft()

if (keys[K_UP]):
self.player.jump()

if (keys[K_ESCAPE]):
self._running = False

self.on_loop()
self.on_render()
self.on_cleanup()

if __name__ == "__main__" :
theApp = App()
theApp.on_execute()

If you want to jump on objects, simply add them to the screen, do collision detection and reset the jump variables if collision is true.

Maze in Pygame

Game development with Pygame

Pygame Python Pygame Python

Welcome to the first tutorial of the series: Building games with Pygame. Games you create with Pygame can be run on any machine that supports Python, including Windows, Linux and Mac OS.

In this tutorial we will explain the fundamental of building a game with Pygame. We’ll start of with the basics and will teach you how to create the basic framework. In the next tutorials you will learn how to make certain types of games.

You may like

PyGame introduction
You’ll end up with a program similar to the one on the right:

A game always starts in an order similar to this (pseudo code):

initialize()
while running():
game_logic()
get_input()
update_screen()
deinitialize()

The game starts with initialization. All graphics are loaded, sounds are loaded, levels are loaded and any data that needs to be loaded. The game continues running until it receives a quit event. In this game loop we update the game, get input and update the screen. Depending on the game the implementation widely varies, but this fundamental structure is common in all games.

In Pygame we define this as:

import pygame
from pygame.locals import *

class App:

windowWidth = 640
windowHeight = 480
x = 10
y = 10

def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)
self._running = True
self._image_surf = pygame.image.load("pygame.png").convert()

def on_event(self, event):
if event.type == QUIT:
self._running = False

def on_loop(self):
pass

def on_render(self):
self._display_surf.blit(self._image_surf,(self.x,self.y))
pygame.display.flip()

def on_cleanup(self):
pygame.quit()

def on_execute(self):
if self.on_init() == False:
self._running = False

while( self._running ):
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
self.on_cleanup()

if __name__ == "__main__" :
theApp = App()
theApp.on_execute()

The Pygame program starts with the constructor init(). Once that is finished on_execute() is called. This method runs the game: it updates the events, updates the screen. Finally, the game is deinitialized using on_cleanup().

In the initialiasation phase we set the screen resolution and start the Pygame library:

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

We also load the image.

self._image_surf = pygame.image.load("pygame.png").convert()

This does not draw the image to the screen, that occurs in on_render().

def on_render(self):
self._display_surf.blit(self._image_surf,(self.x,self.y))
pygame.display.flip()

The blit method draws the image (image_surf) to the coordinate (x,y). In Pygame the coordinates start from (0,0) top left to (wind0wWidth, windowHeight). The method call pygame.display.flip() updates the screen.

Continue the next tutorial and learn how to add game logic and build games :-)