So I'm making this 2D game in pygame and I have run with an issue were when I try to leap off a platform, my player will just float instead of fall https://gyazo.com/82e9029b230326f182067a9f4a3cc47c. I been trying to use true and false statements to make it work, but most of the time my player will not move at all also I have tried telling it if its not colliding with the platform and that dose not work to.
This is one of the things I have tried
for Platform in platforms:
if not playerman.rect.colliderect(Platform.rect):
playerman.y -= playerman.speed
a demo of my game
import pygame
pygame.init()
# width and height of winddow
window = pygame.display.set_mode((700,500))
# window name
pygame.display.set_caption("Game")
#Player class
class Player:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 4
self.rect = pygame.Rect(x,y,width,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
class Platform:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,width,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
class Platform2:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,width,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# Colors
white = (255,255,255)
green = (255,0,0)
# class's cordination, size, and color
#Player
playerman = Player(300,250,40,70,white)
#Platform
platform1 = Platform(0,460,800,40,white)
#The top platform
platform2 = Platform2(0,0,800,40,green)
# List's
platforms = [platform1]
platforms2 = [platform2]
# drawing things in main loop
def redraw():
# background color
window.fill((0,0,0))
# drawing the player in main loop
playerman.draw()
# Drawing all the bottom platforms in the main loop
for Platform in platforms:
Platform.draw()
# Drawining all the top platforms in the main loop
for Platform2 in platforms2:
Platform2.draw()
# Timer for player falling
#fps for the game
fps = 30
clock = pygame.time.Clock()
up = True
down = False
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
px,py = playerman.x,playerman.y
if down == False:
for Platform in platforms2:
if keys[pygame.K_SPACE] and playerman.rect.colliderect(Platform):
up = True
if up:
playerman.y += 10
fall = False
for Platform in platforms:
if playerman.rect.colliderect(Platform.rect):
up = False
if up == False and keys[pygame.K_SPACE]:
for Platform in platforms:
if playerman.rect.colliderect(Platform.rect):
down = True
up = False
if down:
playerman.y -= 10
playerman.direction = "tjump"
fall = False
for Platform in platforms2:
if playerman.rect.colliderect(Platform.rect):
down = False
playerman.direction = "uidle"
if keys[pygame.K_a] and px > playerman.speed:
for Platform in platforms:
for Platform2 in platforms2:
Platform.x += playerman.speed
Platform2.x += playerman.speed
elif keys[pygame.K_d] and px < 2000 - playerman.width + playerman.speed:
for Platform in platforms:
for Platform2 in platforms2:
Platform.x -= playerman.speed
Platform2.x -= playerman.speed
redraw()
pygame.display.update()
pygame.quit()
from Player not falling when it is not colliding with platform Pygame
No comments:
Post a Comment