ASAP it will come an update to the DIYPPG, Do It by Yourself a Python Platform Game. Here is a little video with some new stuffs.
The last episode is here.
Some code in preview
import pygame
import sys
from pygame.locals import *
class Map:
def __init__(self, num_map):
self.num_map = num_map
def big_map(self):
game_map1 = """
< >xxxx< xxxx< >xxxx< xxxx
ox< ooo ooox< ooo oo
o ooo oo ooo o
o >xxoo xxxxoo >xxoo xxxxo
o ooxxx< o x o
oxx< oo xxo xxx o
o oo o o
o >xxooxxxxxxx< o xxx o
o oo o o
oxxxxxxoo xxxxxxxooxxxxxxo xxxxxxxxo
o o
ooooooooooooooooooooo oooooooooooooo
""".splitlines()
return game_map1, (1150, 500), 200
def tiny_map(self):
map = """
>xxxxxxx< >xx
xxxxx< o o o
oo ooo >x< o
oo o o o
oo xxxx ooo >xxo
oo oooxxx< o
oo >xo o oo
ooooo ooo ooooo
oo o o o oo
oooooooooooooooooooooooo""".splitlines()
return map, (760, 376), 150
def choose_map(self):
if self.num_map == 1:
y = self.big_map()
else:
y = self.tiny_map()
return y
pygame.init()
pygame.display.set_caption('Game')
map = Map(1)
# ymap is for the position of the trees bg
game_map1, WINDOW_SIZE, ymap = map.choose_map()
screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
display = pygame.Surface((WINDOW_SIZE[0] // 2, WINDOW_SIZE[1] // 2))
# MAP AND SCREEN
clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)
# Movements
moving_right = False
moving_left = False
# For the pygame.transform.flip(player_img, 1, 0)
stay_right = True
momentum = 0
air_timer = 0
# ================== PLAYER IMAGES ================
player_img = pygame.image.load('imgs\\player.png').convert()
player_img.set_colorkey((255, 255, 255))
player_rect = pygame.Rect(100, 100, 5, 13)
p_right = player_img
p_left = pygame.transform.flip(player_img, 1, 0)
bg = pygame.image.load("imgs\\bg3.png")
# bg2 = pygame.image.load("imgs\\bg2.png")
game_map = [list(lst) for lst in game_map1]
dr = "imgs\\"
tl = {}
tl["o"] = pygame.image.load(f'{dr}dirt.png')
tl["x"] = pygame.image.load(f'{dr}grass.png')
tl["<"] = pygame.image.load(f'{dr}grassr.png')
tl[">"] = pygame.image.load(f'{dr}grassl.png')
tl[" "] = pygame.image.load(f'{dr}space.png')
def collision_test(rect, tiles):
"Returns the Rect of the tile with which the player collides"
hit_list = []
for tile in tiles:
if rect.colliderect(tile):
hit_list.append(tile)
return hit_list
def move(rect, movement, tiles):
global air_timer, momentum
collision_types = {
'top': False, 'bottom': False, 'right': False, 'left': False}
rect.x += movement[0]
hit_list = collision_test(rect, tiles)
for tile in hit_list:
if movement[0] > 0:
rect.right = tile.left
collision_types['right'] = True
elif movement[0] < 0:
rect.left = tile.right
collision_types['left'] = True
rect.y += movement[1]
hit_list = collision_test(rect, tiles)
for tile in hit_list:
if movement[1] > 0:
rect.bottom = tile.top
collision_types['bottom'] = True
elif movement[1] < 0:
rect.top = tile.bottom
collision_types['top'] = True
if collision_types['bottom']:
air_timer = 0
momentum = 0
else:
air_timer += 1
return rect
def display_tiles():
"Makes the Rects for the 'physics'"
tile_rects = []
y = 0
for line_of_symbols in game_map:
x = 0
for symbol in line_of_symbols:
display.blit(tl[symbol], (x * 16, y * 16))
if symbol != " ":
tile_rects.append(pygame.Rect(x * 16, y * 16, 16, 16))
x += 1
y += 1
return tile_rects
def display_player():
player_img = p_right if stay_right else p_left
display.blit(player_img, (player_rect.x, player_rect.y))
return player_img
def display_bg():
global WINDOW_SIZE
display.blit(bg, (0, 0))
# display.blit(bg2, (0, ymap))
def display_fps():
fps = font.render(str(int(clock.get_fps())), True, pygame.Color('black'))
display.blit(fps, (0, 0))
return fps
def clear_screen():
display.fill((73, 184, 250))
def move_player(player_movement):
global loop, stay_right, player_rect, moving_right, moving_left, momentum
if moving_right:
player_movement[0] += 2
if moving_left:
player_movement[0] -= 2
player_movement[1] += momentum
momentum += 0.3
if momentum > 3:
momentum = 3
player_rect = move(player_rect, player_movement, tile_rects)
# ============ Keyboard pressing detection
for event in pygame.event.get():
if event.type == QUIT:
loop = 0
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_right = True
stay_right = True
if event.key == K_LEFT:
moving_left = True
stay_right = False
if event.key == K_SPACE:
if air_timer < 6:
momentum = -5
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_right = False
if event.key == K_LEFT:
moving_left = False
loop = 1
while loop:
clear_screen()
display_bg()
tile_rects = display_tiles()
display_player()
display_fps()
player_movement = [0, 0]
move_player(player_movement)
screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
pygame.display.update()
clock.tick(60)
pygame.quit()
Pygame's Platform Game