Aug-16-2022, 12:18 PM
the code isnt producing any errors, but the player just falls through the ground and platforms i set up, im not sure what the problem is!! any help would be super appreciated
import pygame, sys # importing the library pygame and sys import time # imports time library from pygame.locals import * # import all pygames modules windowWidth = 960 # 64 * 15 windowHeight = 640 # 64 * 10 fps = 60 # frames per second/ updates and redraws per second main = True # main game loop is always true playerimg1 = pygame.image.load("idleghost.png") playerx = 0 playery = 512 playerspeed = 5 playerrect = pygame.Rect(playerx, playery, 64, 64) rightmovement = False # variables for player movement leftmovement = False jump = False player_y_momentum = 0 tilesize = 64 mapList = [['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], ['a', 'a', 'a', 'a', 'a', 'a', 'p', 'p', 'p', 'a', 'a', 'a', 'a', 'a', 'a'], ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'p', 'p', 'a'], ['a', 'a', 'p', 'p', 'p', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], ['a', 'a', 'a', 'a', 'a', 'a', 'p', 'p', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g']] background = pygame.image.load("wall.png") floor = pygame.image.load("floorboardtile.png") shelf = pygame.image.load("bookshelf.png") clock = pygame.time.Clock() # setting up the clock pygame.init() # initializing pygame window = pygame.display.set_mode((windowWidth, windowHeight)) # initializing the window/screen pygame.display.set_caption("Platform Game") # name of window 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, playerx, playery, tiles): collision_types = {'top': False, 'bottom': False, 'right': False, 'left': False} rect.x += playerx hit_list = collision_test(rect, tiles) for tile in hit_list: if rightmovement == True: rect.right = tile.left collision_types['right'] = True if leftmovement == True: rect.left = tile.right collision_types['left'] = True rect.y += playery hit_list = collision_test(rect, tiles) for tile in hit_list: if jump == False: rect.bottom = tile.top collision_types['bottom'] = True if jump == True: rect.top = tile.bottom collision_types['top'] = True return rect, collision_types while main == True: # main game loop window.blit(background, (0, 0)) # renders the background image, blit is used to place one surface upon another playerrect = pygame.Rect(playerx, playery, 60, 60) tile_rects = [] # sets up list of tile rects y = 0 # keeps track of x,y coordinates in tile map for row in mapList: # renders the tiles in the map list / for each row in the map list x = 0 for tile in row: # for each tile in each row if tile == 'g': # if a tile is the ground window.blit(floor, (x * tilesize, y * tilesize)) # render the floor image in the current co-ords if tile == 'p': # if a tile is a platform window.blit(shelf, (x * tilesize, y * tilesize)) if tile != 'a': tile_rects.append(pygame.Rect(x * tilesize, y * tilesize, tilesize, tilesize)) # add this tile to the tile rects list x += 1 # increase the x co-ord by 1 y += 1 # increase the y co-ord by 1 playery += player_y_momentum # add the players vertical momentum to their y position player_y_momentum += 0.2 # in every frame add 0.2 to the vertical momentum if player_y_momentum > 3: # stops the players vertical momentum being larger than 3 player_y_momentum = 3 playerrect, collisions_type = move(playerrect, playerx, playery, tile_rects) if collisions_type['bottom']: player_y_momentum = 0 window.blit(playerimg1, (playerx, playery)) # renders players image for event in pygame.event.get(): # the event loop if event.type == pygame.QUIT: # checks if the window is being closed pygame.quit() # stops pygame sys.exit() # stops the script running if event.type == KEYDOWN: if event.key == K_RIGHT or event.key == K_d: rightmovement = True if event.key == K_LEFT or event.key == K_a: leftmovement = True if event.key == K_UP or event.key == K_w: player_y_momentum = -5 if event.type == KEYUP: if event.key == K_RIGHT or event.key == K_d: rightmovement = False if event.key == K_LEFT or event.key == K_a: leftmovement = False if rightmovement == True: playerx = playerx + playerspeed if leftmovement == True: playerx = playerx - playerspeed pygame.display.update() # updates what is being displayed on screen clock.tick(fps) # maintains the frame rate (in this case 60)