I’m trying to make a simple 2d jumping game, I’ve made a jumping cube and a simple platform(map), collisions are working if I test it like print("collided"), but I don’t know how to make the rect stand on another rect,
tile[1] are actually all tiles (blocks) which were looped from the list called tile_list. In tile_list they are saved tiles. self.rect_img = img.get_rect() <- is the rect object i want player to stand on, but how I said, its looped through list so i need to work with tile[1]
I tried using:
for tile in self.tile_list:
collide = tile[1].colliderect(self.player_rect.x,self.player_rect.y,PLAYER_WIDTH,PLAYER_HEIGHT)
if collide:
tile[1].top = self.player_rect.bottom
But doesnt work, the player(self.player_rect), somehow pushes the second rect below it.
Full code main.py:
import pygame as pg
import sys
import os
from pygame import key
from mapdata import game_map_data
#ASSETS
GRASS_IMAGE = pg.image.load(r'C:\Users\user\Desktop\pygame-tilemap\1.11.2022\assets\platform.jpg')
sky = pg.image.load(r'C:\Users\user\Desktop\pygame-tilemap\1.11.2022\assets\sky.png')
CONTINUE = True
#CONSTANT VARIABLES
#WIDTH;HEIGHT
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 598
PLAYER_WIDTH = 20
PLAYER_HEIGHT = 20
#COLORS
COLOR_DARKBLUE = 'darkblue'
COLOR_RED = 'red'
#WINDOW
WINDOW = pg.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
sky = pg.transform.scale(sky,(800,599))
class Player(object):
def __init__(self,x,y,width,height,jumpRange):
self.x = x
self.y = y
self.width = width
self.height = height
self.isJump = False
self.jumpRange = jumpRange
def Draw(self):
self.player = pg.image.load(r'C:\Users\user\Desktop\pygame-tilemap\1.11.2022\assets\player.png')
self.player = pg.transform.scale(self.player,(PLAYER_WIDTH,PLAYER_HEIGHT))
self.player_rect = self.player.get_rect()
WINDOW.blit(self.player,dest=(self.x,self.y))
self.player_rect.x = self.x
self.player_rect.y = self.y
#COLLISION WITH BLOCKS
for tile in self.tile_list:
collide = tile[1].colliderect(self.player_rect.x,self.player_rect.y,PLAYER_WIDTH,PLAYER_HEIGHT)
if collide:
tile[1].top = self.player_rect.bottom
def handle_keys(self,speed,vel_y):
keys = pg.key.get_pressed()
self.speed = speed
self.vel_y = vel_y
if keys[pg.K_RIGHT]:
if CONTINUE == True:
self.x += speed
if keys[pg.K_LEFT]:
if CONTINUE == True:
self.x -= speed
def jump(self,jumpRange):
if self.isJump == True:
if self.jumpRange >= -11:
neg = 1
if self.jumpRange < 0:
neg = -1
self.y -= self.jumpRange**2 * 0.1 * neg
self.jumpRange -= 1
else:
self.isJump = False
self.jumpRange = jumpRange
###########
##TILEMAP##
###########
def tile(self,data,tile_size,img_size):
self.tile_list = []
#SAVING TILES IN A LIST
row_count = 0
self.tile_size = tile_size
self.img_size = img_size
for row in data:
col_count = 0
for tile in row:
if tile == 1:
self.rect_img = img.get_rect()
self.rect_img.x = col_count * tile_size
self.rect_img.y = row_count * tile_size
tile = (img,self.rect_img)
self.tile_list.append(tile)
col_count += 1
row_count += 1
#DRAWING TILES FROM THE LIST
def draw_tile(self):
for tile in self.tile_list:
WINDOW.blit(tile[0], tile[1])
#FPS
clock = pg.time.Clock()
game_map_data = game_map_data
#PLAYER
player = Player(x=30,y=500,width=PLAYER_WIDTH,height=PLAYER_HEIGHT,jumpRange=11)
img_size = 17
img = pg.transform.scale(GRASS_IMAGE,(img_size,img_size))
gamemap = player.tile(game_map_data,tile_size=17,img_size=20)
while True:
FPS = 60
pg.display.update()
clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
elif event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
# Start to jump by setting isJump to True.
player.isJump = True
SKY_POSITION = (0,0)
WINDOW.blit(sky,SKY_POSITION)
player.draw_tile()
player.jump(jumpRange=11)
player.Draw()
player.handle_keys(speed=3,vel_y=3)
pg.display.update()
pg.display.flip()
mapdata.py:
game_map_data = [
[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,0,0,0,0,0,0,0,0,0,0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,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,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,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],
]
Also if u want to test the code, game then create 2 files, main.py and mapdata.py(which is the tilemap), and change photos
>Solution :
You need to change the player’s position instead of the block’s position and you also need to set the player’s y attribute:
self.player_rect.x = round(self.x)
self.player_rect.y = round(self.y)
for tile in self.tile_list:
if self.player_rect.colliderect(tile[1]):
self.player_rect.bottom = tile[1].top
self.y = self.player_rect.y