Jump to content
  • Advertisement
Sign in to follow this  
daedalus316

Pygame Collision help

This topic is 2510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am attempting to create a VERY simple platformer just falling and moving and jumping, I decide i woudl start with falling function but ican't gt it to work, for some reason my collision statement isn't working or jsut always returns true. Can anyone help?:


import pygame, sys
from pygame.locals import *
pygame.init()
width=640
height= 480
screen= pygame.display.set_mode((width, height))
#images
player=pygame.image.load("ball.png").convert_alpha()
background= pygame.image.load("sky.png").convert()
platform= pygame.image.load("platform.png").convert_alpha()
#player coordinates
x = 300
y = 150
#movement variables
movex= 0
movey=0
while True:
#rects
platform_rect = platform.get_rect()
player_rect = player.get_rect()
#collsion
standing = player_rect.colliderect(platform_rect)
screen.blit(background, (0,0))
screen.blit(platform, (300, 250))
while standing == True:
movey = 10
x += movex
y += movey

print movey
screen.blit(player, (x, y))
pygame.display.update()

Share this post


Link to post
Share on other sites
Advertisement
I'm no expert, but there's a few things it could be.

Firstly, the indentation of movey = 10 might be it.

The second thing I can think of is the line before that, 'while standing == True:'
I'm not sure you need '== True:' You could just have 'while standing:'

If they don't work, it could be that the events happening whilst standing is True aren't actually doing anything to the object, so you would see no movement.

Share this post


Link to post
Share on other sites
well i did what you said and you were right the indentation was wron. It was a solid tab it needed to be 4 spaces. but now when i run the program all i get is a blank screen with no background or image at all. and its not responding.

Share this post


Link to post
Share on other sites
ok i fixed the code to this
import pygame, sys
from pygame.locals import *
pygame.init()
width=640
height= 480
screen= pygame.display.set_mode((width, height))
#images
player=pygame.image.load("ball.png").convert_alpha()
background= pygame.image.load("sky.png").convert()
platform= pygame.image.load("platform.png").convert_alpha()
#player coordinates
x = 300
y = 150
#movement variables
movex= 0
movey=0
while True:
#rects
platform_rect = pygame.Rect(300, 250, 150, 20)
player_rect = pygame.Rect(x, y, 24, 24)
#collsion
standing = player_rect.colliderect(platform_rect)
while standing == False:
print ("fish")
movey= 10
x += movex
y += movey
screen.blit(background, (0,0))
screen.blit(platform, (300, 250))
screen.blit(player, (x, y))
pygame.display.update()


and now everything works.. except the screen come completely blank. there are no errors it just for some reason is not blitting. Any help would be hugely appreciated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!