Have to admit that the Python
Game ProgrammingIntroduction "This book translation, typesetting is very bad, but the inside of the demo is very good, before doing some adaptations put here."
First Material:
Background
Elves
All materials are taken from this book
Next is the creation of the Elf class:
(self) Self.master_image = = = -1= 1= 1=== 1============rect = = (Rect.width//width) * (rect.height//height)-1 update (self, CU Rrent_time, rate=30 current_time > Self.last_time ++= 1 self.frame >== self.frame!== (self.frame% self.columns) *= (Self.frame//self.columns) *=== Self.frame
"Place" The Sprite class on the game screen and add the background
Pygame.init () screen = Pygame.display.set_mode ((+)) font = Pygame.font.Font (None) framerate = Pygame.time.Clock () bg = pygame.image.load ("Background.png"). Convert_alpha () pl = pygame.image.load (' caveman.png '). Convert_alpha () # Create Sprite group = Pygame.sprite.Group () player = MySprite (screen) player.load ("Caveman.png", 50, 64, 8) Player.first_frame = 1player.last_frame = 7player.position = +, 303group.add (player) while true:for event in Pygame.even T.get (): if Event.type = = QUIT: sys.exit () # Set the number of frames Framerate.tick () ticks = Pygame.time.get_ticks ()
So the genie is on the canvas and we have to let it move around:
Keys = pygame.key.get_pressed () if Keys[k_escape]: sys.exit () if keys[k_right]: player. X + = 8if keys[k_left]:if player. X > 0: player. X-= 8
And then jump and two-segment jumps.
Here are the points to note for two jumps:
1. Until the landing, can only jump two times, that is to say, two times after the elves jump can not jump
2. When the space is pressed, the wizard's acceleration resets
, this requires modifying the preceding code:
Jump_vel = 0.0# Sets a variable that records the number of hops Space_number = 0# Jump judgment player_jumping = falseplayer_start_y = player. Ywhile true:for event in Pygame.event.get (): if Event.type = = QUIT:sys.exit () if Event.type = = Keydown:if event. Key = = k_space:# jumps less than 2 times, if space_number < 2:jump_vel = -15.0space_number + = 1player_jumping = True keys = pygame.key.get_pressed () if Keys[k_escape]: Sys.exit () if Keys[k_right]: player. X + = 8if keys[k_left]:if player. X > 0:player. X-= 8# Sets the number of frames Framerate.tick (in) ticks = Pygame.time.get_ticks () # When a space is pressed, the jump_vel variable continues to grow larger until it touches the ground if player_jumping: Player. Y + = Jump_vel Jump_vel + = after landing, reset the jump speed and other judging variables if player. Y >= player_start_y:player_jumping = False player. Y = player_start_y Jump_vel = 0 Space_number = 0# Create background screen.blit (BG, (0, 0)) # Elf Group update GR Oup.update (Ticks,) group.draw (screen) pygame.display.update ()
All code:
Import sys, time, random, math, pygamefrom pygame.locals import *class MySprite (pygame.sprite.Sprite):d ef __init__ (self, Target): pygame.sprite.sprite.__init__ (self) self.master_image = None Self.frame = 0 self.old_ frame = -1self.frame_width = 1self.frame_height = 1self.first_frame = 0 Self.last_frame = 0 self.columns = 1 Self.last_time = 0# uses the property method to make the Sprite class more convenient for coordinate operations Def _GETX (self): return self.rect.xdef _setx (self, value): Self.rect. x = value x = Property (_getx, _setx) def _gety (self): return self.rect.ydef _sety (self, value): Self.rect.y = Valu E Y = Property (_gety, _sety) def _getpos (self): return self.rect.topleftdef _setpos (self, POS): Self.rect.topleft = pos Position = Property (_getpos, _setpos) def load (self, filename, width, height, columns): Self.master_image = Pygame.image.load (filename). Convert_alpha () self.frame_width = width Self.frame_height = height self . Rect = rect (0, 0, width, Height) self.columns = Columns rect = self.master_image.get_rect () Self.last_frame = (Rect.width// width) * (rect.height//height)-1def update (self, Current_time, rate=30): # Update the number of frames if current_time > Self.last_time + Rate:self.frame + = 1if self.frame > self.last_frame:self.frame = self.first_frame Self.last_time = current_time# When the number of frames changes, create a new picture if self.frame! = self.old_frame:frame_x = (self.frame% SELF.C Olumns) * Self.frame_width frame_y = (self.frame//self.columns) * self.frame_height rect = rect (fr Ame_x, Frame_y, Self.frame_width, self.frame_height) self.image = Self.master_image.subsurface (rect) Self.old_frame = self.framepygame.init () screen = Pygame.display.set_mode ((+)) font = Pygame.font.Font (None, 24) framerate = Pygame.time.Clock () bg = pygame.image.load ("Background.png"). Convert_alpha () pl = Pygame.image.load (' Caveman.png '). Convert_alpha () # Create Sprite Group Group = Pygame.sprite.Group () player = MySprite (screen) player.load ("Caveman.png", 8) Player.first_frame = 1player.last_frame = 7player.position = +, 303group.add (player) Jump_vel = 0.0# set a variable to record the number of hops Space_number = 0# jump Judgment Pl ayer_jumping = falseplayer_start_y = player. Ywhile true:for event in Pygame.event.get (): if Event.type = = QUIT:sys.exit () if Event.type = = Keydown:if event. Key = = k_space:# jumps less than 2 times, if space_number < 2:jump_vel = -15.0space_number + = 1player_jumping = True keys = pygame.key.get_pressed () if Keys[k_escape]: Sys.exit () if Keys[k_right]: player. X + = 8if keys[k_left]:if player. X > 0:player. X-= 8# Sets the number of frames Framerate.tick (in) ticks = Pygame.time.get_ticks () # When a space is pressed, the jump_vel variable continues to grow larger until it touches the ground if player_jumping: Player. Y + = Jump_vel Jump_vel + = after landing if player. Y >= player_start_y:player_jumping = False player. Y = player_start_y Jump_vel = 0 Space_number = 0 Rush_number = 0# Create background screen.blit (BG, (0, 0)) # Elf Group Update group.update (ticks , Group.draw (screen) pygame.display.update ()
In this way, a rough, two-stage wizard will be completed.
Thank you for the idea that this book offers a single jump and let me think about two jumps. In fact, like the two-stage jump this kind of looks easy, but the implementation still need to think about.