I'm trying to create a game where you are supposed to land on a platforms in the air, and if you fall under the camera's point of view, you die. You can jump in the air but that consumes fat you build up by eating. I'm not done with the game so you won't see that part, but the problem is with the collisions. I've made many objects and one I'm testing is a wooden box. It works, but the collision works like a triangle instead of a square. Half of the collision doesn't work in the top right, but the bottom left works. I have no clue why that can be the case, so help would come a long way. I will link the file and code.
import pygame, math
from pygame.locals import *
pygame.init()
Screen = pygame.display.set_mode((450, 800))
Running = True
ScreenWidth = Screen.get_width()
ScreenHeight = Screen.get_height()
CharacterSize = 25
Character = pygame.image.load('assets/game/Character.png').convert_alpha()
CharacterJumpPower = 15
JumpCooldown = 0
PlayerX = 0
PlayerY = 0
CameraX = PlayerX
CameraY = PlayerY
CameraSmoothness = 28
Gravity = -0.025
XVelocity = 0
YVelocity = 0
MouseX, MouseY = pygame.mouse.get_pos()
Keyboard = pygame.key.get_pressed()
Clock = pygame.time.Clock()
WoodenBox = pygame.transform.smoothscale(pygame.image.load('assets/game/WoodenBox.png').convert_alpha(), (25, 25))
Obstacles = [[WoodenBox, (400, -400)]]
def UpdateVariables():
global Screen, ScreenWidth, ScreenHeight, CharacterSize, Character, MouseX, MouseY, Keyboard, Clock
ScreenWidth = Screen.get_width()
ScreenHeight = Screen.get_height()
Character = pygame.transform.smoothscale(pygame.image.load('assets/game/Character.png').convert_alpha(), (CharacterSize, CharacterSize))
MouseX, MouseY = pygame.mouse.get_pos()
Keyboard = pygame.key.get_pressed()
Clock = pygame.time.Clock()
def CharacterPhysics():
global Gravity, CharacterSize, XVelocity, YVelocity, MouseX, MouseY, PlayerX, PlayerY, Keyboard, CharacterJumpPower, JumpCooldown, ScreenWidth, ScreenHeight
XVelocity = (MouseX - PlayerX) / 5
YVelocity += CharacterSize * Gravity
if Keyboard[K_SPACE] and CharacterSize > 5 and JumpCooldown == 0:
YVelocity = CharacterJumpPower
CharacterSize -= 1
JumpCooldown = 1
elif not Keyboard[K_SPACE]:
JumpCooldown = 0
if (-PlayerY + CameraY + ScreenHeight / 2) > ScreenHeight:
Dead()
if CheckCollisions():
print("True")
PlayerX += XVelocity
# if CheckCollisions():
# if XVelocity > 0:
# PlayerX -= 1
# else:
# PlayerX += 1
# PlayerX -= XVelocity
# XVelocity = 0
PlayerY += YVelocity
if CheckCollisions():
if YVelocity > 0:
PlayerY -= 1
else:
PlayerY += 1
PlayerY -= YVelocity
YVelocity = 0
def CameraMovement():
global PlayerX, PlayerY, CameraX, CameraY, CameraSmoothness
CameraY += (PlayerY - CameraY) / CameraSmoothness
def Dead():
global Running
print("You Died!")
Running = False
def BlitObstacles():
global Screen, Obstacles, CameraX, CameraY, ScreenWidth, ScreenHeight
for Obstacle in Obstacles:
Screen.blit(Obstacle[0], Obstacle[0].get_rect(center = (Obstacle[1][0] - CameraX, -Obstacle[1][1] + CameraY + ScreenHeight / 2)))
def CheckCollisions():
global Obstacles, PlayerX, PlayerY, Character, CharacterSize
for Obstacle in Obstacles:
if abs((PlayerX + PlayerY) - (Obstacle[1][0] + Obstacle[1][1])) < Obstacle[0].get_size()[0] / 2 + Obstacle[0].get_size()[1] / 2:
PlayerMask = pygame.mask.from_surface(Character)
ObstacleMask = pygame.mask.from_surface(Obstacle[0])
Offset = (PlayerX - Obstacle[1][0], PlayerY - Obstacle[1][1])
if ObstacleMask.overlap(PlayerMask, Offset):
return True
return False
while Running:
Screen.fill((255, 255, 255, 255))
UpdateVariables()
CharacterPhysics()
CameraMovement()
Screen.blit(Character, Character.get_rect(center = (PlayerX - CameraX, -PlayerY + CameraY + ScreenHeight / 2))) # Player
BlitObstacles()
pygame.display.flip()
Clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
Running = False
pygame.quit()
Here is the folder: https://www.dropbox.com/sh/26mmmtxmcwvazdd/AACAlbEpA3bUH5YwJILZ5t8fa?dl=0