• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Crazylegs830

Need help with Python/Pygame Collision

15 posts in this topic

Hello, I just need some easy (if possible) code that prevents the player from going off screen, sorry if this is in the wrong place or is a stupid question thanks.

0

Share this post


Link to post
Share on other sites

Hello, I just need some easy (if possible) code that prevents the player from going off screen, sorry if this is in the wrong place or is a stupid question thanks.

 

Hi. 

 

Usually a collision test looks something like this: (not real code)

if yPosition > 460:
     yPosition = 460

If your window is 640 x 480 pixels and you have a platform at the bottom of your window that is 20 pixels by 20 pixels (copied across the bottom of your screen), then you will want to check if the falling object's Y-position has reached 460 pixels. And if it has reached 460 pixels, then you set it's position to 460 pixels.

 

The x coordinates are read from left to right. The Y coordinates are read from top to bottom (not like in math class). So counting from the top to the bottom, the y position of the top of the platform will be at 460 pixels (480-20 because the platform is 20 pixels tall). You can also use an if statement to print something once the falling object has reached 460 pixels to check if it works. 

 

I hope this photo isn't too big.

 

One more note. The falling object has a size also. If your falling object is 10 x 10 pixels, then from the top of your object to the middle is 5 pixels. I think pygame uses the center of your object graphic as the collision point, so you will want to offset that 460 by 5 pixels (if your graphic is  10 x 10). 

collision.jpg

Edited by Tutorial Doctor
2

Share this post


Link to post
Share on other sites
def is_within_borders():
    # Assume we have 500, 500 window with 50, 50 player size.
    if player.x < 0:
        player.x = 0
    elif player.x > 450:
        player.x = 450
    return True
1

Share this post


Link to post
Share on other sites

How about you show us your code then tongue.png

Alright sorry, I am very new to this. Also sorry if code isn't pretty or anything I am new.

 

pygame.init()

screen=pygame.display.set_mode((1600,1130),0,32)
mouse_c=pygame.image.load(you).convert_alpha()

#Variables for shapes
color=(128,64,0)
pos1=(0,0)
pos2=(0,1130)
pos3=(1600,0)
pos4=(1600,1130)
#Variables for shapes

x,y=0,0
movex, movey=0,0

#Attempting collision
def is_within_borders():
    if yPosition > 50:
     yPosition = 50
    elif xPosition > 1550:
        xPostion = 1550
    return True
#Attempting collision

#Controls
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_LEFT:
                movex=-4
            elif event.key==K_RIGHT:
                movex=+4
            elif event.key==K_UP:
                movey=-4
            elif event.key==K_DOWN:
                movey=+4
        if event.type==KEYUP:
            if event.key==K_LEFT:
                movex=0
            elif event.key==K_RIGHT:
                movex=0
            elif event.key==K_UP:
                movey=0
            elif event.key==K_DOWN:
                movey=0

#Controls
               
    screen.lock()
    line1=pygame.draw.line(screen, color, pos1, pos2, 50)
    line2=pygame.draw.line(screen, color, pos3, pos4, 50)
    line3=pygame.draw.line(screen, color, pos1, pos3, 50)
    line4=pygame.draw.line(screen, color, pos2, pos4, 50)
    screen.unlock()

    x+=movex
    y+=movey

    screen.blit(mouse_c,(x,y))

    pygame.display.update()

0

Share this post


Link to post
Share on other sites

Okay I have never programmed in Python/PyGame before so I might be completely wrong... but

if the variables x, y define the position of your object :

 

Then this

#Attempting collision
def is_within_borders():
    if yPosition > 50:
     yPosition = 50
    elif xPosition > 1550:
        xPostion = 1550
    return True

 

Should be:

#Attempting collision

def is_within_borders():
    if y > 50:
     y = 50
    elif x > 1550:
        x= 1550
    return True

 

Also. You want to check this every step/tick of the game. So it should be checked in your main loop. I have no idea how pygame works as I said before.. so it might have already been in there smile.png So check for the object being out of bounds just after you have moved your object.

 

EDIT: Thanks CoreLactose, I didn't even bother to check the parameters :/

Edited by PragmaOnce
1

Share this post


Link to post
Share on other sites

PragmaOnce's suggestion still looks wrong to me.

Assuming this is  the correct syntax, with a character sprite of 50x50...

def is_within_borders():
    if y < 50:
        y = 50
    elif y > 1080:
        y = 1080
    if x < 50:
        x = 50
    elif x > 1550:
        x = 1550
    return True

Something like that?

That should enforce both vertical and horizontal, in both ends.

Also, it needs to be called in your main loop, as PragmaOnce said.

 

EDIT: It should be called after you've updated the positions.

Edited by CoreLactose
1

Share this post


Link to post
Share on other sites

PragmaOnce's suggestion still looks wrong to me.

Assuming this is  the correct syntax, with a character sprite of 50x50...

def is_within_borders():
    if y < 50:
        y = 50
    elif y > 1080:
        y = 1080
    if x < 50:
        x = 50
    elif x > 1550:
        x = 1550
    return True

Something like that?

That should enforce both vertical and horizontal, in both ends.

Also, it needs to be called in your main loop, as PragmaOnce said.

 

EDIT: It should be called after you've updated the positions.

Thanks, but it still won't work:

 

pygame.init()

screen=pygame.display.set_mode((1600,1130),0,32)
mouse_c=pygame.image.load(you).convert_alpha()

#Variables for shapes
color=(128,64,0)
pos1=(0,0)
pos2=(0,1130)
pos3=(1600,0)
pos4=(1600,1130)
#Variables for shapes

x,y=0,0
movex, movey=0,0

 

#Controls
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_LEFT:
                movex=-4
            elif event.key==K_RIGHT:
                movex=+4
            elif event.key==K_UP:
                movey=-4
            elif event.key==K_DOWN:
                movey=+4
        if event.type==KEYUP:
            if event.key==K_LEFT:
                movex=0
            elif event.key==K_RIGHT:
                movex=0
            elif event.key==K_UP:
                movey=0
            elif event.key==K_DOWN:
                movey=0
#Controls
#Collsion
               
    def is_within_borders():
        if y < 50:
            y = 50
        elif y > 1080:
            y = 1080
        if x < 50:
            x = 50
        elif x > 1550:
            x = 1550
        return True
#Collision
               
    screen.lock()
    line1=pygame.draw.line(screen, color, pos1, pos2, 50)
    line2=pygame.draw.line(screen, color, pos3, pos4, 50)
    line3=pygame.draw.line(screen, color, pos1, pos3, 50)
    line4=pygame.draw.line(screen, color, pos2, pos4, 50)
    screen.unlock()

    x+=movex
    y+=movey

    screen.blit(mouse_c,(x,y))

    pygame.display.update()

0

Share this post


Link to post
Share on other sites

I read up and the def keyword in python means that it is a function. A function is something that is initialized and then you can call the function whenever you want.

So put this code:

def is_within_borders():
    if y < 50:
        y = 50
    elif y > 1080:
        y = 1080
    if x < 50:
        x = 50
    elif x > 1550:
        x = 1550
    return True

Somewhere outside the loop. At the start of your code maybe.

Then you need to call the function when you want to use it so do this

x+=movex
y+=movey
is_within_borders()
1

Share this post


Link to post
Share on other sites

 

I read up and the def keyword in python means that it is a function. A function is something that is initialized and then you can call the function whenever you want.

So put this code:

def is_within_borders():
    if y < 50:
        y = 50
    elif y > 1080:
        y = 1080
    if x < 50:
        x = 50
    elif x > 1550:
        x = 1550
    return True

Somewhere outside the loop. At the start of your code maybe.

Then you need to call the function when you want to use it so do this

x+=movex
y+=movey
is_within_borders()

Ugh Still not working.

0

Share this post


Link to post
Share on other sites

So after not understanding why it does not work, I downloaded python and pygame. And found out some pretty interesting things about it. The reason the function won't work is because x,y are not global variables. So instead of using a function just pop this in after you move:

 x+=movex
 y+=movey

#Checking for out of bounds
if y < 0:
    y = 0
elif y > 1080:
    y = 1080
if x < 0:
    x = 0
elif x > 1550:
    x = 1550
Edited by PragmaOnce
2

Share this post


Link to post
Share on other sites

 

So after not understanding why it does not work, I downloaded python and pygame. And found out some pretty interesting things about it. The reason the function won't work is because x,y are not global variables. So instead of using a function just pop this in after you move:

 x+=movex
 y+=movey

#Checking for out of bounds
if y < 0:
    y = 0
elif y > 1080:
    y = 1080
if x < 0:
    x = 0
elif x > 1550:
    x = 1550

Thank you works perefectly!!!

0

Share this post


Link to post
Share on other sites

I would rather avoid such code.

 

I would really use OO programming, programming in a procedural way (like the code mentoined above) really leads to messy code very quickly.

 

global variables are usually a sign of bad coding practice.

Edited by axel1994
0

Share this post


Link to post
Share on other sites

You are certainly correct.

 

My point should have been:

This example solves the problem, it doesn't make things too complicated.

But you'll have to learn better, more complex patterns later on.

 

I do know some people who just get stuck. They keep doing what they're doing, and don't improve.

 

I'm not saying the op is like this (coming to this forum already proves the contrary).

I'm saying this solution isn't the end-all be all way. (such a solution, as you demonstrated, doesn't exist)

 

Thank you for the clarification, and my apologies for the previous answer :)

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0