![]() ![]() BehaviourManager ( 'manager', 'behaviour-manager' ) world. is_dying = False def addedToWorld ( self, world ): """The snake was added to the world""" super ( Snake, self ). CompositeActor ): """Represents the snake""" def _init_ ( self ): """Initialise the snake""" super ( Snake, self ). Import pygame import serge.visual import serge.sound import serge.engine import serge.actor import import import import import class Snake ( serge. We can just use the setAngle method of the sprite (the actor’s visual) to rotate The other thing to notice is that we didn’t have to make sprites for all the different orientations You could create a multi-celled sprite to do this but it is just as Two sprites to represent the green and red states of the snake. We didn’t have to make too many changes to get this to work. moveTo ( 400, 300 ) # Run the game engine. registerItem ( 'red-tail', 'red-tail.png' ) # Create the snake snake = Snake () world. registerItem ( 'red-head', 'red-head.png' ) serge. registerItem ( 'tail', 'tail.png' ) serge. registerItem ( 'head', 'head.png' ) serge. getWorld ( 'lab' ) # Register sprites serge. ![]() getSimpleSetup ( 800, 600 ) world = engine. moveTo ( 400, 300 ) # Create the engine engine = serge. visible = True def restartGame ( self ): """Restart the game""" self. debug ( 'Removing part of the tail' ) if self. setSpriteName ( 'red-head' ) def removeTail ( self, world, actor, interval ): """Remove part of the tail""" self. y 600 def initiateDeathAnimation ( self ): """Begin showing the death of the snake""" self. addChild ( segment ) def hitBody ( self ): """Return True if the head has hit the body Look to see if we overlap with any body segment except the last (we are allowed to overlap the last since we just put it down) """ for segment in self. restartGame () def addSegment ( self ): """Add a new body segment""" segment = serge. initiateDeathAnimation () # Increase score self. addSegment () # Check if we hit the body if self. move ( * offset ) # Add a new segment if needed if not self. setAngle ( current_angle + rotation ) # Move if not self. getCardinalFromAngle ( current_angle + rotation ) self. K_RIGHT ): rotation = - 90 else : rotation = 0 # Change direction if rotation : current_angle = serge. updateActor ( interval, world ) # Quit if requested if self. NumericText ( 'text', 'score', 'Score: %04d ', ( 255, 255, 255 ), font_size = 20, value = 0, align = 'left' ), layer_name = 'front', center_position = ( 80, 30 )) def updateActor ( self, interval, world ): """Update the snake""" super ( Snake, self ). addVisualActorToWorld ( world, 'bg', 'bg', serge. visible = False # A background for the game self. Text ( 'Game Over - Press ENTER to restart', ( 255, 255, 255 ), font_size = 20 ), layer_name = 'front', center_position = ( 400, 300 )) self. addVisualActorToWorld ( world, 'text', 'restart', serge. manager ) # Text to display when the game is over self. Import pygame import serge.visual import serge.engine import serge.actor import import import import import class Snake ( serge. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |