Aug-07-2021, 12:51 AM
So I could fix my last error thanks to an user, but when I run the game my character just sticks in the air and only can move left when I press rioght and when I press right just moves a little, but when I press it again just moves so quickly to the right that leaves the screen.
The lines that I wrote so you guys can locate them easly: (49-70) collision movement, (111) code so the def move works
The lines that I wrote so you guys can locate them easly: (49-70) collision movement, (111) code so the def move works
import pygame import sys from pygame import * pygame.init() # Fps clock = pygame.time.Clock() # Title pygame.display.set_caption('HandsomeBeta') # Screen window = (1280, 720) screen = pygame.display.set_mode(window) display = pygame.Surface((1280, 720)) # player image player = pygame.image.load('assets4plyr/Handsome1.png') handsomeman = pygame.transform.scale(player, (55, 55)) handsomeman.set_colorkey((255, 255, 255)) grassimg = pygame.image.load('assets4game/handsomegrass.png') tile_size = grassimg.get_height() dirtimg = pygame.image.load('assets4game/handsomedirt.png') 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','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','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','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','0','0','0','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'], ['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2','2','2','2','2','2','2','2','2','2','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','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']] 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 moving_right = False moving_left = False playerpos = [640, 200] playerY_momentum = 0 plyrrect = pygame.Rect(50, 50, handsomeman.get_width(), handsomeman.get_height()) test_rect = pygame.Rect(640, 360, 100, 50) # GameLoop while True: display.fill((146, 244, 255)) tile_rects = [] y = 0 for row in game_map: x = 0 for tile in row: if tile == '1': display.blit(dirtimg, (x * tile_size, y * tile_size)) if tile == '2': display.blit(grassimg, (x * tile_size, y * tile_size)) if tile == '0': tile_rects.append(pygame.Rect(x * tile_size, y * tile_size, tile_size, tile_size)) x += 1 y += 1 player_movement = [0, 0] if moving_right: player_movement[0] += 2 if moving_left: player_movement[0] -= 2 player_movement[1] += playerY_momentum playerY_momentum += 0.2 if playerY_momentum > 3: playerY_momentum = 3 plyrrect, collisions = move(plyrrect, player_movement, tile_rects) display.blit(handsomeman, (plyrrect.x, plyrrect.y)) for event in pygame.event.get(): 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.type == KEYUP: if event.key == K_RIGHT: moving_right = False if event.key == K_LEFT: moving_left = False surf = pygame.transform.scale(display, window) screen.blit(surf, (0, 0)) pygame.display.update() clock.tick(60)