Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python pygame collision

Options
  • 16-06-2013 5: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 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 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