python logo

Tag: game


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
Create Space Invaders with Python

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) )
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):
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

pygame.display.set_caption('Pygame 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):

def on_render(self):

def on_cleanup(self):

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

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

if (keys[K_RIGHT]):

if (keys[K_LEFT]):

if (keys[K_UP]):

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


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

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.

You may like
Create Space Invaders with Python

python text game

In this article we will demonstrate how to create a simple guessing game.
The goal of the game is to guess the right number.

An example run below:

python-text-game Simple text game with Python

You may like
Simple games with Python

Random number

The user will be asked to guess the random number. We first pick the random number:

from random import randint

x = randint(1,9)

The randint() function will pick a pseudo random number between 1 and 10. Then we have to continue until the correct number is found:

guess = -1

print("Guess the number below 10:")
while guess != x:
guess = int(raw_input("Guess: "))

if guess != x:
print("Wrong guess")
print("Guessed correctly")

Python Guessing Game

The code below starts the game:

from random import randint

x = randint(1,9)
guess = -1

print "Guess the number below 10:"
while guess != x:
guess = int(raw_input("Guess: "))

if guess != x:
print("Wrong guess")
print("Guessed correctly")

An example run:

Guess the number below 10:
Guess: 3
Wrong guess
Guess: 6
Wrong guess

You may like
Simple games with Python

python snake game