Create a new virtual environment
$ conda create --name igame python=3.8
Switch to the new environment
$ conda deactivate $ conda activate igame
Install pygame
$ pip install pygame
import pygame import sys from pygame.locals import * pygame.init() vec = pygame.math.Vector2 # 2 for two dimensional HEIGHT = 450 #window height WIDTH = 400 #window width displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Game") #game loop while True: #for now it only checks for the quit event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()
Some of the types of events generated:
<Event(1024-MouseMotion {'pos': (332, 76), 'rel': (-2, -5), 'buttons': (0, 0, 0), 'window': None})>
<Event(512-WindowEvent {'event': 11, 'window': None})>
<Event(256-Quit {})>
after running the program above, you should just see the black window and you should be able to quit the application when closing that window
Drawing the Surface
import pygame import sys from pygame.locals import * #we extend class Sprite #Sprite is any object that you may have on the screen that can move/change class platform(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((WIDTH, 30)) #width and height of the surface self.surf.fill((0,153,153)) # RGB values for the surface color self.rect = self.surf.get_rect(center = (WIDTH/2, HEIGHT - 10)) pygame.init() vec = pygame.math.Vector2 # 2 for two dimensional HEIGHT = 450 #window height WIDTH = 400 #window width displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Game") _platform = platform() #create sprite group to manage multiple sprite objects all_sprites = pygame.sprite.Group() all_sprites.add(_platform) # add the platform to the group #game loop while True: #for now it only checks for the quit event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) #draws the sprites pygame.display.update()
Adding a sprite that moves
import pygame import sys from pygame.locals import * #we extend class Sprite #Sprite is any object that you may have on the screen that can move/change class Platform(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((WIDTH, 30)) #width and height of the surface self.surf.fill((0,153,153)) # RGB values for the surface color self.rect = self.surf.get_rect(center = (WIDTH/2, HEIGHT - 10)) class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() #load image from png, covert_alpha handles the transparency img = pygame.image.load("./caveman.png").convert_alpha() scaled_img = pygame.transform.scale(img, (80, 80)) self.surf = scaled_img self.rect = self.surf.get_rect(center = (40, 30)) self.pos = vec((40, HEIGHT-15)) # initial position of the sprite self.vel = vec(0,0) self.acc = vec(0,0) def move(self): # set initial accelleration of the object to 0 self.acc = vec(0,0) #check if key was pressed pressed_keys = pygame.key.get_pressed() #if left arrow was pressed, deccelerate horizontally (x) if pressed_keys[K_LEFT]: self.acc.x = -ACC #if right arrow was pressed, accelerate horizontally (x) if pressed_keys[K_RIGHT]: self.acc.x = ACC #FRIC is a friction quotient self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc #if we go out of boundaries, rotate if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH #re-position the sprite based on the coalucations self.rect.midbottom = self.pos pygame.init() vec = pygame.math.Vector2 # 2 for two dimensional HEIGHT = 450 #window height WIDTH = 400 #window width ACC = 0.5 FRIC = -0.12 FPS = 60 white = [255, 255, 255] displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) #change the background color to white pygame.display.set_caption("Game") _platform = Platform() #create platform object _caveman = Player() #create caveman #create sprite group to manage multiple sprite objects all_sprites = pygame.sprite.Group() all_sprites.add(_platform) # add the platform to the group all_sprites.add(_caveman) #game loop while True: _caveman.move() #for now it only checks for the quit event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() #clear the screen displaysurface.fill(white) #and redraw all the sprites for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) #draws the sprites pygame.display.update()

game.zip |
Adding Jumping Motion
We need to add gravity to the object self.acc = vec(0,0.5)
However, now that we add gravity, unless there is something to hold the sprite, it will just fall
So we need to implement collision detection pygame.sprite.spritecollide
However, now that we add gravity, unless there is something to hold the sprite, it will just fall
So we need to implement collision detection pygame.sprite.spritecollide
import pygame import sys from pygame.locals import * #we extend class Sprite #Sprite is any object that you may have on the screen that can move/change class Platform(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((WIDTH, 30)) #width and height of the surface self.surf.fill((0,153,153)) # RGB values for the surface color self.rect = self.surf.get_rect(center = (WIDTH/2, HEIGHT - 10)) def move(self): pass class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() #load image from png, covert_alpha handles the transparency img = pygame.image.load("./caveman.png").convert_alpha() scaled_img = pygame.transform.scale(img, (80, 80)) self.surf = scaled_img self.rect = self.surf.get_rect() self.pos = vec((40, HEIGHT-15)) # initial position of the sprite self.vel = vec(0,0) self.acc = vec(0,0) def jump(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits: self.vel.y = -15 def update(self): hits = pygame.sprite.spritecollide(self,platforms, False) if self.vel.y > 0: if hits: self.vel.y = 0 self.pos.y = hits[0].rect.top + 1 def move(self): # set initial accelleration of the object to 0 self.acc = vec(0,0.5) #check if key was pressed pressed_keys = pygame.key.get_pressed() #if left arrow was pressed, deccelerate horizontally (x) if pressed_keys[K_LEFT]: self.acc.x = -ACC #if right arrow was pressed, accelerate horizontally (x) if pressed_keys[K_RIGHT]: self.acc.x = ACC #FRIC is a friction quotient self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc #if we go out of boundaries, rotate if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH #re-position the sprite based on the coalucations self.rect.midbottom = self.pos pygame.init() vec = pygame.math.Vector2 # 2 for two dimensional HEIGHT = 600 #window height WIDTH = 800 #window width ACC = 0.5 #acceleration quotient FRIC = -0.12 #friction FPS = 60 white = [255, 255, 255] displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) #change the background color to white pygame.display.set_caption("Game") _platform = Platform() #create platform object _caveman = Player() #create caveman #create sprite group to manage multiple sprite objects all_sprites = pygame.sprite.Group() all_sprites.add(_platform) # add the platform to the group all_sprites.add(_caveman) platforms = pygame.sprite.Group() platforms.add(_platform) #game loop while True: #for now it only checks for the quit event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: _caveman.jump() #clear the screen displaysurface.fill(white) _caveman.update() #and redraw all the sprites for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) #draws the sprites entity.move() pygame.display.update()

game.zip |
Adding Obstacles
import pygame import sys from pygame.locals import * #we extend class Sprite #Sprite is any object that you may have on the screen that can move/change class Platform(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((WIDTH, 30)) #width and height of the surface self.surf.fill((0,153,153)) # RGB values for the surface color self.rect = self.surf.get_rect(center = (WIDTH/2, HEIGHT - 10)) def move(self): pass class Wall(pygame.sprite.Sprite): def __init__(self, image, center): super().__init__() img = pygame.image.load(image).convert_alpha() scaled_img = pygame.transform.scale(img, (80, 80)) self.surf = scaled_img self.rect = self.surf.get_rect(center = center) def move(self): pass class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() #load image from png, covert_alpha handles the transparency img = pygame.image.load("./caveman.png").convert_alpha() scaled_img = pygame.transform.scale(img, (80, 80)) self.surf = scaled_img self.rect = self.surf.get_rect() self.pos = vec((40, HEIGHT-15)) # initial position of the sprite self.vel = vec(0,0) self.acc = vec(0,0) def jump(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits: self.vel.y = -15 def update(self): hits = pygame.sprite.spritecollide(self,platforms, False) if self.vel.y > 0: if hits: self.vel.y = 0 self.pos.y = hits[0].rect.top + 1 def move(self): # set initial accelleration of the object to 0 self.acc = vec(0,0.5) #check if key was pressed pressed_keys = pygame.key.get_pressed() #if left arrow was pressed, deccelerate horizontally (x) if pressed_keys[K_LEFT]: self.acc.x = -ACC #if right arrow was pressed, accelerate horizontally (x) if pressed_keys[K_RIGHT]: self.acc.x = ACC #FRIC is a friction quotient self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc #if we go out of boundaries, rotate if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH #re-position the sprite based on the coalucations self.rect.midbottom = self.pos pygame.init() vec = pygame.math.Vector2 # 2 for two dimensional HEIGHT = 600 #window height WIDTH = 800 #window width ACC = 0.5 #acceleration quotient FRIC = -0.12 #friction FPS = 60 white = [255, 255, 255] displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) #change the background color to white pygame.display.set_caption("Game") _platform = Platform() #create platform object _caveman = Player() #create caveman _wall1 = Wall("./obstacle1.png",(400, 550)) _wall2 = Wall("./obstacle3.png",(600, 550)) #create sprite group to manage multiple sprite objects all_sprites = pygame.sprite.Group() all_sprites.add(_platform) # add the platform to the group all_sprites.add(_caveman) all_sprites.add(_wall1) all_sprites.add(_wall2) platforms = pygame.sprite.Group() platforms.add(_platform) platforms.add(_wall1) platforms.add(_wall2) #game loop while True: #for now it only checks for the quit event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: _caveman.jump() #clear the screen displaysurface.fill(white) _caveman.update() #and redraw all the sprites for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) #draws the sprites entity.move() pygame.display.update()

game.zip |
Collecting the Rewards
import pygame import sys from pygame.locals import * #we extend class Sprite #Sprite is any object that you may have on the screen that can move/change class Platform(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((WIDTH, 30)) #width and height of the surface self.surf.fill((0,153,153)) # RGB values for the surface color self.rect = self.surf.get_rect(center = (WIDTH/2, HEIGHT - 10)) def move(self): pass class Reward(pygame.sprite.Sprite): collected_items = 0 def __init__(self, image, center): super().__init__() self.collected = False img = pygame.image.load(image).convert_alpha() scaled_img = pygame.transform.scale(img, (30, 30)) self.surf = scaled_img self.rect = self.surf.get_rect(center = center) def collect(self): if not self.collected: self.collected = True Reward.collected_items += 1 print(Reward.collected_items) self.kill() def move(self): pass class Wall(pygame.sprite.Sprite): def __init__(self, image, center): super().__init__() img = pygame.image.load(image).convert_alpha() scaled_img = pygame.transform.scale(img, (80, 80)) self.surf = scaled_img self.rect = self.surf.get_rect(center = center) def move(self): pass class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() #load image from png, covert_alpha handles the transparency img = pygame.image.load("./caveman.png").convert_alpha() scaled_img = pygame.transform.scale(img, (80, 80)) self.surf = scaled_img self.rect = self.surf.get_rect() self.pos = vec((40, HEIGHT-15)) # initial position of the sprite self.vel = vec(0,0) self.acc = vec(0,0) def jump(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits: self.vel.y = -15 def update(self): hits = pygame.sprite.spritecollide(self,platforms, False) rhits = pygame.sprite.spritecollide(self, rewards, False) if rhits: rhits[0].collect() if self.vel.y > 0: if hits: self.vel.y = 0 self.pos.y = hits[0].rect.top + 1 def move(self): # set initial accelleration of the object to 0 self.acc = vec(0,0.5) #check if key was pressed pressed_keys = pygame.key.get_pressed() #if left arrow was pressed, deccelerate horizontally (x) if pressed_keys[K_LEFT]: self.acc.x = -ACC #if right arrow was pressed, accelerate horizontally (x) if pressed_keys[K_RIGHT]: self.acc.x = ACC #FRIC is a friction quotient self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc #if we go out of boundaries, rotate if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH #re-position the sprite based on the coalucations self.rect.midbottom = self.pos pygame.init() vec = pygame.math.Vector2 # 2 for two dimensional HEIGHT = 600 #window height WIDTH = 800 #window width ACC = 0.5 #acceleration quotient FRIC = -0.12 #friction FPS = 60 white = [255, 255, 255] displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) #change the background color to white pygame.display.set_caption("Game") _platform = Platform() #create platform object _caveman = Player() #create caveman _wall1 = Wall("./obstacle1.png",(400, 550)) _wall2 = Wall("./obstacle3.png",(600, 550)) _reward1 = Reward("./reward1.png",(600, 350)) _reward2 = Reward("./reward2.png",(300, 350)) #create sprite group to manage multiple sprite objects all_sprites = pygame.sprite.Group() all_sprites.add(_platform) # add the platform to the group all_sprites.add(_caveman) all_sprites.add(_wall1) all_sprites.add(_wall2) all_sprites.add(_reward1) all_sprites.add(_reward2) platforms = pygame.sprite.Group() platforms.add(_platform) platforms.add(_wall1) platforms.add(_wall2) rewards = pygame.sprite.Group() rewards.add(_reward1) rewards.add(_reward2) #game loop while True: #for now it only checks for the quit event for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: _caveman.jump() #clear the screen displaysurface.fill(white) _caveman.update() #and redraw all the sprites for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) #draws the sprites entity.move() pygame.display.update()

game.zip |