eng
competition

Text Practice Mode

Python Platformer game

created Aug 4th 2020, 13:07 by Red Cube Gamedev


0


Rating

417 words
2 completed
00:00
import pygame, sys
 
clock = pygame.time.Clock()
 
from pygame.locals import *
pygame.init() # initiates pygame
 
pygame.display.set_caption('Pygame Platformer')
 
WINDOW_SIZE = (800,600)
 
screen = pygame.display.set_mode(WINDOW_SIZE,0,32) # initiate the window
 
display = pygame.Surface((300,200)) # used as the surface for rendering, which is scaled
 
moving_right = False
moving_left = False
moving_down = False
vertical_momentum = 0
air_timer = 0
 
scroll = [0,0]
game_map = [['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','2','2','2','2','2','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','2','2','2','2','2','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','2','1','2','2','2','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
            ['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2','0','0','2','2','0','0','0','0','0','0'],
            ['1','1','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','1','1','0','0','1','1','2','0','0','0','0','0'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','2','3','3','2','2'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','1','1','1','1','1','1','1'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','1','1','0','1','1','1'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0'],
            ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0']]
 
 
grass_img = pygame.image.load('grassTile.png')
dirt_img = pygame.image.load('dirtTile.png')
spikes_img = pygame.image.load('spikes.png')
 
player_img = pygame.image.load('player1.png')
 
player_rect = pygame.Rect(100,100,16,32)
 
def collision_test(rect,tiles):
    hit_list = []
    for tile in tiles:
        if rect.colliderect(tile):
            hit_list.append(tile)
    return hit_list
 
def move(rect,movement,tiles):
    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
    return rect, collision_types
 
def player_died():
    print("A")
     
while True: # game loop
    display.fill((146,244,255)) # clear screen by filling it with blue
    scroll[0] += (player_rect.x-scroll[0]-152)
    scroll[1] += (player_rect.y-scroll[1]-86)/2
    tile_rects = []
    spikes_rects = []
    y = 0
    for layer in game_map:
        x = 0
        for tile in layer:
            if tile == '1':
                display.blit(dirt_img,(x*16 - int(scroll[0]),y*16 - int(scroll[1])))
            if tile == '2':
                display.blit(grass_img,(x*16 - int(scroll[0]),y*16 - int(scroll[1])))
            if tile == '3':
                display.blit(spikes_img,(x*16 - int(scroll[0]),y*16 - int(scroll[1])))
            if tile != '0' and tile != '3':
                tile_rects.append(pygame.Rect(x*16,y*16,16,16))
            if tile == '3':
                spikes_rects.append(pygame.Rect(x*16,y*16,16,16))
            x += 1
        y += 1
 
    player_movement = [0,0]
    if moving_down and moving_left:
        player_movement[0] -= 0.5   
    #elif moving_left:
     #   player_movement[0] -= 2
         
    if moving_right and moving_down:
        player_movement[0] += 0.5
    elif moving_right:
        player_movement[0] += 2
         
     
 
    if not moving_down:
        player_rect.height = 32
    if moving_down:
        player_rect.height = 16
         
    player_movement[1] += vertical_momentum
    vertical_momentum += 0.2
    if vertical_momentum > 3:
        vertical_momentum = 3
     
         
    player_rect,collisions = move(player_rect,player_movement,tile_rects)
         
    for spike in spikes_rects:
        if player_rect.colliderect(spike):
            player_died()
     
    if collisions['bottom'] == True:
        air_timer = 0
        vertical_momentum = 0
    else:
        air_timer += 1
         
    display.blit(player_img,(player_rect.x-scroll[0],player_rect.y-scroll[1]))
 
 
    for event in pygame.event.get(): # event loop
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
            if event.key == K_DOWN:
                moving_down = True
            if event.key == K_UP and not moving_down:
                if air_timer < 6:
                    vertical_momentum = -5
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False
            if event.key == K_DOWN:
                moving_down = False
         
    screen.blit(pygame.transform.scale(display,WINDOW_SIZE),(0,0))
    pygame.display.update()
    clock.tick(60)
 

saving score / loading statistics ...