Friday, 15 January 2010

python - Rect Errors on Pygame -


Hi, I am making a game where a player has to dodge the oil barrel and I am working on getting the rectit so that I can make it. When some 2 sprites collide, though it seems that the collision boxes have got damaged because whenever I start the game, it shows the confrontation between the player and the barrel.

This is my code:

(Self): self.stopped = false thread .__ init __ (self) def run (self): while not itself. Closed: self. Download () time.sleep (2) def downloadValue (self): rand = random.randrange (1, 10) class oil (object): def __init __ (self): self.image = pygame.image.load (' Self.png (self.image, (300300)) self.rect = self.image.get_rect () self.x = 1600 self.y = 400 def handle_keys (self): Key = pygame.key.get_pressed () distance = 10 self.x - = distance def draw (self, surf ace): surface. Blist (self image, (self., Self.)) DEF check exam (self, oil, phantom): col = sprite.colliderect (oil) if col == true: print ("game over") Def blit_alpha Target, source, location, opacity): x = location [0] y = location [1] temp = pygame.Surface (source.get_width), source.get_height ())). Convert () temp.blit (target, (-x, -y)) temp.blit (source, (0, 0)) temp.set_alpha () target.blit (temporary, location) class phantom (object): def __init __ (self): Self.image = pygame.image.load ('image1.png') self.rect = self.image.get_rect () self.x = 0 self.y = 0 def handle_keys (self): key = Pygame.key.get_pressed () distance = 10 if key [pygame.K_DOWN]: self Y + = Distance if key [pygame.K_UP]: self Y = = if key [pygame.K_RIGHT]: self.x + = distance if key [pygame.K_L EFT]: self. X - = Distance DER draw (self, surface): # surface. Bleet (Self Image, (Self X, SAI)) Animated. Blit (Surface, (self. X, self.)) Anim = pyganim.PygAnimation ((("image1.png", 0.5), ("image2.png", 0.5)]) pygame.init () screen = pygame .display.set_mode ((1600, 800)) Oil = oil () sprite = sprite () clock = pygame.time.Clock () background = pygame.image.load ('background.png') background = pygame.transform. Scale (Background, (1600, 800)) Anime Play () myThread = UpdateThread () myThread.start () oil.checkCollision (oil.rect, sprite.rect) While true: in pygame.event.get () For: if event.type == pygame.QUIT: pygame .quit () is running = incorrect sprite.handle_keys () oil.handle_keys () screen.fill ((255,255,255)) blit_alpha (screen, background, (0, 0) ), 128) sprite.draw (screen) oil.draw (screen) Pygame.display.update () clock.tick (30)

Any help would be greatly appreciated.

Since Your oil and are not obtained from the phantom class pygame.Rect class, I believe that you have to clash between them To test:

  def check exam (self, phantom): if self. Colliderect (sprite.rect): print ("GameOver")  

and it should be called in such a way:

  oil.checkCollision (sprite)  

What do you have:

  oil.checkCollision (oil could, sprite.rect)  

in the object - Oriented programming is called "has_a" relationships between classes, which is achieved through subclassing or receiving from another class.

According to BTW, which I recommend strongly that you follow, those two class names should have been capitalized and oil And Sprite .


No comments:

Post a Comment