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
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python pygame collision

  • 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, 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