1 / 10

Objects in Pygame

Objects in Pygame. Lecture 07. Making an Object for Games. class Entity(): def __ init __ ( self ): pass. Entity. The most basic object to be drawn on screen We will use inheritance in the game to create player and enemy classes from it. Creating the Entity in Pygame.

gil
Télécharger la présentation

Objects in Pygame

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects in Pygame Lecture 07

  2. Making an Object for Games class Entity(): def__init__(self): pass

  3. Entity The most basic object to be drawn on screen We will use inheritance in the game to create player and enemy classes from it.

  4. Creating the Entity in Pygame The class should be defined before the pygame.init() class Entity(): def__init__(self, size, colour): self.rect = pygame.Rect(0,0,size,size) self.dx = 0 self.dy = 0 self.distance = 0

  5. Draw method We will use the basic Pygame drawing function def draw(self, screen): self.draw.rect(screen, red, self.rect)

  6. Actually Drawing The Entity object needs to be instantiated Then the draw method must be called.

  7. Move method def move(self): self.rect.x += self.dx self.rect.y += self.dy

  8. Actually Moving The move method must be called from within the main loop. dude.move()

  9. Bouncing off walls Let’s do a little bounds checking def move(self): if (self.rect.x<0orself.rect.right> WIDTH): self.dx*= -1 if (self.rect.y<0orself.rect.bottom> HEIGHT): self.dy*= -1 self.rect.x += self.dx self.rect.y += self.dy

  10. Changing the Colour The colour defaults to red but can be changed def__init__(self, size): self.rect = pygame.Rect(0,0,size,size) self.dx = 0 self.dy = 0 self.colour = red self.distance = 0

More Related