Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Python pygame collision

  • 16-06-2013 04:50PM
    #1
    Closed Accounts Posts: 1,386 ✭✭✭


    Okay so I want to make a basic game and so far I have he character moving corresponding to the keys but the problem is it goes past the windows, how do I stop this? I don't know how to do the collision detection in Python and any help would be amazing.
    bif="bg.jpg"
    mif="man.png"
    
    import pygame, sys
    from pygame.locals import *
    
    pygame.init()
    screen=pygame.display.set_mode((1109,630),0,32)
    
    
    
    background=pygame.image.load(bif).convert()
    mouse_c=pygame.image.load(mif).convert_alpha()
    
    x,y=0,400
    movex, movey=0,0
    
    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=-10
                elif event.key==K_RIGHT:
                    movex=+10
                elif event.key==K_UP:
                    movey=-10
                elif event.key==K_DOWN:
                    movey=+10
            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
    
            
    
            x+=movex
            y+=movey
    
            screen.blit(background,(0,0))
            screen.blit(mouse_c,(x,y))
    
            
    
            pygame.display.update()
    


Comments

  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    Check if x + movex > width of the window or less than 0.
    set x to the width or 0 depending on which condition is true.
    Do a similar check on y.


  • Closed Accounts Posts: 1,386 ✭✭✭Troxck


    duffman85 wrote: »
    Check if x + movex > width of the window or less than 0.
    set x to the width or 0 depending on which condition is true.
    Do a similar check on y.

    When I do
    x+movex>0
    y+movey>0
    
    My character moves only forward and down but still goes past the boundaries of the window.

    When I do
    x+movex>659
    y+movey>659
    
    My character does not move at all/

    I'm probably doing something wrong, sorry to be bothering you


  • Registered Users, Registered Users 2 Posts: 341 ✭✭Mo14


    Troxck wrote: »
    When I do
    x+movex>0
    y+movey>0
    
    My character moves only forward and down but still goes past the boundaries of the window.

    When I do
    x+movex>659
    y+movey>659
    
    My character does not move at all/

    I'm probably doing something wrong, sorry to be bothering you
    You want to move the character when x + movex is less than the window size.

    So what you'll have is something like:
    if x + movex > 0 and x + movex < 659:
        x+=movex
    
    Also, good practice would be to use
    screen.get_size()
    
    to get the width and height of the screen instead of using number values.


Advertisement