SlideServe
Menu
Préférences
© SlideServe · v2026

Objects in Pygame

gil avatar
10 diapositives · 311 Views · Sep 04, 2014

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.

Objects in Pygame - slide 1
01 / 10Objects in Pygame
1 / 17

Télécharger la présentation

Objects in Pygame
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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