python - Pygame Collision Detection Error, Sprites -


i working on simple pygame has bunny , candies falling on screen, if bunny touches candy, candy disappears. collision detection not working. when run it, following error:

traceback (most recent call last):   file "c:\mashi.py", line 79, in <module>     candy.checkcollision(bunny.image_b, candy.image_b) typeerror: checkcollision() takes 2 arguments (3 given) 

here's code:

import pygame import os, sys import random import time  pygame.display.set_caption("mashimaro game")  # better have variable, extremely long line. img_path = os.path.join('mashimaro.png') img_path2 = os.path.join('candy.png') background_image = pygame.image.load('food.png')  class bunny(object):  # represents bunny, not game     def __init__(self):         """ constructor of class """         self.image_s = pygame.image.load(img_path)         self.image_b = self.image_s.get_rect()         # bunny's position         self.x = 0         self.y = 0      def handle_keys(self):         """ handles keys """         key = pygame.key.get_pressed()         dist = 5 # distance moved in 5 frame         if key[pygame.k_down]: # down key             self.y += dist # move down         elif key[pygame.k_up]: # key             self.y -= dist # move         if key[pygame.k_right]: # right key             self.x += dist # move right         elif key[pygame.k_left]: # left key             self.x -= dist # move left      def draw(self, surface):         """ draw on surface """         # blit @ current position         surface.blit(self.image, (self.x, self.y))  class candy(object):     def __init__(self, x=640, y=0):         self.image_s = pygame.image.load(img_path2)         self.image_b = self.image_s.get_rect()         self.x = x         self.y = y         dist = 10         self.dist = dist      def candy(self):         dist = 10         self.x -=dist      def candy_draw(self, surface):         surface.blit(self.image, (self.x, self.y))      def checkcollision(sprite1, sprite2):         col = pygame.sprite.spritecollide(sprite1, sprite2)         if col == true:             sys.exit()         return col  pygame.init() screen = pygame.display.set_mode((640, 400))  bunny = bunny() # create instance candy = candy() clock = pygame.time.clock()  running = true while running:     # handle every event since last frame.     event in pygame.event.get():         if event.type == pygame.quit:             pygame.quit() # quit screen             running = false          if candy.x < 0:             y = random.randint(10, 190)             candy = candy(640, y)     candy.checkcollision(bunny.image_b, candy.image_b)      bunny.handle_keys() # handle keys     candy.candy()      screen.blit(background_image,[0,0]) # fill screen background     bunny.draw(screen) # draw bunny screen     rock.rock_draw(screen)     pygame.display.update() # update screen      clock.tick(120) 

what have been doing wrong?

in candy.checkcollision() method forgot include self argument in definition. should be:

def checkcollision(self, sprite1, sprite2): 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -