Thread: snake game

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    407
    Rep Power
    25

    snake game


    im creating a snake game in python, having trouble with a fish.gif collision detection:


    Code:
    def display_rat(screen, back):
        rat_pos = []
        global rect, X_random, Y_random, rat_count
        
        if (rat_count < 1 ):
            X_random = random.randint(0,640)
            Y_random = random.randint(0,480)
            rect = screen.blit(RAT, [X_random,Y_random]) # draw a random rat.
            rat_pos.append(rect) #remember rat position for loop.
            rat_count =+ 1 #this is created to ensure the computers know there is a rat
    
    
        #this is done straight away after the rat is created - while a rat is there this not
        #repeted - this is to get around the randomizing of the X and Y variables, this way they
        #stay assigned
    
        
        if rat_count > 0:
           if rect.collidepoint(X_random,Y_random):   
               for rect in rat_pos:
                    screen.blit(back, rect, rect) # cover up the rect from the corresponding bit of the background
                    rat_pos.remove(rect)#
                    print "fish hit"
               rat_count = 0
    here is my function, the problem i am having is the last if statement (if rat_count > 0) the collidepoint is being run despite whether the coords match or not, so the collision detection is not working. any ideas?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by punkboii
    im creating a snake game in python, having trouble with a fish.gif collision detection:

    here is my function, the problem i am having is the last if statement (if rat_count > 0) the collidepoint is being run despite whether the coords match or not, so the collision detection is not working. any ideas?
    Your problem is exemplified by the following code:
    Code:
    def test(x)
    	if x < 1:
    		print "x is now less than 1"
    		x = 1
     
    	if x > 0:
    		print "x is now greater than 0"
    		x = 0
     
    test(0)
    print "----"
    test(1)
    You can fix the bug if you read the documentation for the if statement.

    --OH.

IMN logo majestic logo threadwatch logo seochat tools logo