Jul-17-2019, 11:32 AM
I've searched and nothing seemd to work. I will copy all the code, becuse there is no much of it and you can check it yourself if you need.
If you know How can I learn to ad even more mechanics like dash or stamina I will really appreciate it.
I've posted twice becuse I could't edit prevoius post and I have'nt seen this python function.
If you know How can I learn to ad even more mechanics like dash or stamina I will really appreciate it.

I've posted twice becuse I could't edit prevoius post and I have'nt seen this python function.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
import pygame, sys clock = pygame.time.Clock() from pygame. locals import * pygame.init() # initiates pygame pygame.display.set_caption( 'Pygame Platformer' ) WINDOW_SIZE = ( 600 , 400 ) 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 vertical_momentum = 0 air_timer = 0 game_map = [[ '1' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' ,], [ '1' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' ,], [ '1' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' ,], [ '1' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' ,], [ '1' , '0' , '0' , '0' , '0' , '0' , '0' , '2' , '2' , '2' , '2' , '2' , '0' , '0' , '0' , '0' , '0' , '0' , '0' ,], [ '1' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' ,], [ '1' , '2' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '2' , '2' ,], [ '1' , '1' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '2' , '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' , '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' , '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' ,], [ '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' ,]] grass_img = pygame.image.load( 'grass.png' ) dirt_img = pygame.image.load( 'dirt.png' ) player_img = pygame.image.load( 'player.png' ).convert() player_img.set_colorkey(( 255 , 255 , 255 )) player_rect = pygame.Rect( 100 , 100 , 5 , 13 ) 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 while True : # game loop display.fill(( 146 , 244 , 255 )) # clear screen by filling it with blue tile_rects = [] y = 0 for layer in game_map: x = 0 for tile in layer: if tile = = '1' : display.blit(dirt_img, (x * 16 , y * 16 )) if tile = = '2' : display.blit(grass_img, (x * 16 , y * 16 )) if tile ! = '0' : tile_rects.append(pygame.Rect(x * 16 , y * 16 , 16 , 16 )) x + = 1 y + = 1 player_movement = [ 0 , 0 ] if moving_right = = True : player_movement[ 0 ] + = 2 if moving_left = = True : player_movement[ 0 ] - = 2 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) if collisions[ 'bottom' ] = = True : air_timer = 0 vertical_momentum = 0 else : air_timer + = 1 display.blit(player_img, (player_rect.x, player_rect.y)) 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_UP: 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 screen.blit(pygame.transform.scale(display, WINDOW_SIZE), ( 0 , 0 )) pygame.display.update() clock.tick( 60 ) |