Implementation of the pygame genie walking and two-step jump

Source: Internet
Author: User

Implementation of the pygame genie walking and two-step jump

I have to admit that PythonGame programmingThe translation and layout of this book is very bad, but the demo in it is still very good. I have made some adaptations here.

First, material:

Background

Genie

All materials are taken from this book

The following is how the genie class is created:

 

Class MySprite (pygame. sprite. sprite): def _ init _ (self, target): pygame. sprite. sprite. _ init _ (self) self. master_image = None self. frame = 0 self. old_frame =-1 self. frame_width = 1 self. frame_height = 1 self. first_frame = 0 self. last_frame = 0 self. columns = 1 self. last_time = 0 # use the property method to make it easier for the genie class to operate on coordinates def _ getx (self): return self. rect. x def _ setx (self, value): self. rect. x = value X = property (_ getx, _ setx) def _ gety (self): return self. rect. y def _ sety (self, value): self. rect. y = value Y = property (_ gety, _ sety) def _ getpos (self): return self. rect. topleft def _ setpos (self, pos): self. rect. topleft = pos position = property (_ getpos, _ setpos)

# The load method defines the image position, length, and number of frames, and thus splits the clip into one frame. 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)-1 def update (self, current_time, Rate = 30): # update the number of frames if current_time> self. last_time + rate: self. frame + = 1 if self. frame> self. last_frame: self. frame = self. first_frame self. last_time = current_time # When the number of frames changes, create a new image if self. frame! = Self. old_frame: frame_x = (self. frame % self. columns) * self. frame_width frame_y = (self. frame // self. columns) * self. frame_height rect = Rect (frame_x, frame_y, self. frame_width, self. frame_height) self. image = self. master_image.subsurface (rect) self. old_frame = self. frame

 

Place the genie class on the game screen and add the background

Pygame. init () screen = pygame. display. set_mode (800,600) 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 an genie group = pygame. sprite. group () player = MySprite (screen) player. load ("caveman.png", 50, 64, 8) player. first_frame = 1player. last_frame = 7player. position = 400,303 group. add (player) while True: for event in pygame. event. get (): if event. type = QUIT: sys. exit () # set the number of frames framerate. tick (30) ticks = pygame. time. get_ticks ()

 

In this case, the genie is on the canvas, and we have to make it move left and right:

 

keys = pygame.key.get_pressed()    if keys[K_ESCAPE]:        sys.exit()    if keys[K_RIGHT]:        player.X += 8    if keys[K_LEFT]:        if player.X > 0:            player.X -= 8

 

 

 

Then achieve the jump and second-segment jump

Note the following points for Skip:

1. You can only Jump twice until it is implemented. That is to say, the Genie cannot jump again after the second jump.

2. Press the space to reset the acceleration of the genie

To modify the preceding code:

Jump_vel = 0.0 # Set a variable to record the number of hops space_number = 0 # 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: # if space_number <2: jump_vel =-15.0 space_number + = 1 player_jumping = True keys = pygame. key. get_pressed () if keys [K_ESCAPE]: sys. exit () if keys [K_RIGHT]: player. X + = 8 if keys [K_LEFT]: if player. x> 0: player. x-= 8 # set the number of frames to framerate. tick (30) ticks = pygame. time. get_ticks () # When the space is pressed, The jump_vel variable continues to grow until it is exposed to the ground if player_jumping: player. Y + = jump_vel + = 2 # after landing, reset the Skip speed and other judgment variables if player. y> = player_start_y: player_jumping = False player. Y = player_start_y jump_vel = 0 space_number = 0 # create a background screen. BITs (bg, (0, 0) # The sprite group updates the group. update (ticks, 50) group. draw (screen) pygame. display. update ()

 

All code:

Import sys, time, random, math, pygamefrom pygame. locals import * class MySprite (pygame. sprite. sprite): def _ init _ (self, target): pygame. sprite. sprite. _ init _ (self) self. master_image = None self. frame = 0 self. old_frame =-1 self. frame_width = 1 self. frame_height = 1 self. first_frame = 0 self. last_frame = 0 self. columns = 1 self. last_time = 0 # use the property method to make it easier for the genie class to operate on coordinates def _ getx (self): return se Lf. rect. x def _ setx (self, value): self. rect. x = value X = property (_ getx, _ setx) def _ gety (self): return self. rect. y def _ sety (self, value): self. rect. y = value Y = property (_ gety, _ sety) def _ getpos (self): return self. rect. topleft def _ 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)-1 def update (self, current_time, rate = 30): # update the number of frames if current_time> self. last_time + rate: self. frame + = 1 if self. frame> self. last_frame: self. frame = Self. first_frame self. last_time = current_time # When the number of frames changes, create a new image if self. frame! = Self. old_frame: frame_x = (self. frame % self. columns) * self. frame_width frame_y = (self. frame // self. columns) * self. frame_height rect = Rect (frame_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 (800,600) 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 an genie group = pygame. sprite. group () player = MySprite (screen) player. load ("caveman.png", 50, 64, 8) player. first_frame = 1player. last_frame = 7player. position = 400,303 group. add (player) jump_vel = 0.0 # Set a variable to record 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: # if space_number <2: jump_vel =-15.0 space_number + = 1 player_jumping = True keys = pygame. key. get_pressed () if keys [K_ESCAPE]: sys. exit () if keys [K_RIGHT]: player. X + = 8 if keys [K_LEFT]: if player. x> 0: player. x-= 8 # set the number of frames to framerate. tick (30) ticks = pygame. time. get_ticks () # When the space is pressed, The jump_vel variable continues to grow until it is exposed to the ground if player_jumping: player. Y + = jump_vel + = 2 # if player. y> = player_start_y: player_jumping = False player. Y = player_start_y jump_vel = 0 space_number = 0 rush_number = 0 # create a background screen. BITs (bg, (0, 0) # The sprite group updates the group. update (ticks, 50) group. draw (screen) pygame. display. update ()

In this way, a rough, second-hop genie is finished.

I am very grateful to this book for providing the idea of a single jump, which gives me the idea of thinking about the second hop. In fact, it seems easy to skip the second hop, but you still need to think about it.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.