2d game design, pygame game development

This assignment is to design a 2D game independently using the pygame module.

The game produced this time was inspired by jumpKing, which is a high-tech game sold on Steam.

We chose their method of operation: A and D keys are needed to control the movement of the character, and the space bar makes the character jump, and the longer the space bar accumulates, the higher the jump

Let’s introduce the production process of our game.

One, design the basic logic and interface of the game.
This is some basic interface and event trigger judgment drawn on the draft.

The complete game design is not fully shown inside.

Second, the class design in the game
1, the player class
In the Player class, we give the basic properties of itself:

Rect is the initial position of the character. We place the initial position of the character on the upper middle of the first platform that randomly appears, so it needs to be imported when the character is created to facilitate the drawing of the initial position of the character.

The faceR attribute determines the direction of movement of the character, 1 represents rightward movement, and 2 represents leftward movement.

The speed attribute is the movement speed of the character on the x-axis.

The gravity attribute is the landing speed of the character on the y-axis.

The live attribute is to determine whether the character is alive.

Drop function to control the character landing.

The Move function controls the character's y-axis movement. You need to enter time. This time is the time the player holds down the space bar to control the length of the character's y-axis movement.

class Player():
def init (self, pos):
self.rect = pos
self.faceR = 1 #1 is to the right, 2 is to the left
self.speed = 2
self.gravity = 10
self.live = True #whether Survive

def Drop(self):
    self.rect.top += self.gravity
def Move(self, time):
    tmp = 0.2*self.speed*time*time  - self.gravity
    if tmp <= 0:
        tmp = 0
    self.rect.top -= tmp
    self.rect.left += self.speed*time*self.faceR

2. Land class In
this class, we give the basic attributes of the land class:

The image attribute is used to store pictures of land,

The rect attribute sets the Rect attribute of the land, and generates different left values ​​through the random function later to control the different positions of the land.

rect.topleft receives the init_pos value given by the outside world, which is the left value of land.

The gravity attribute is the landing speed of land on the y axis.

The Move function controls the landing of the land class.

class Land(pygame.sprite.Sprite):
def init (self, land_img, init_pos):
pygame.sprite.Sprite. init (self)
self.image = land_img
self.rect = pygame.Rect(0,0,100,20)
self .rect.topleft = init_pos
self.gravity = 4
def Move(self):
self.rect.top += self.gravity
Third, the basic code of the program
below is based on the basic settings of pygame to set the width and height of the screen. And some attribute values ​​that will be used next. It should be described in detail below when it is specifically used.

pygame.init()
width = 800
height = 800

window = pygame.display.set_mode((width,height))
pygame.display.set_caption(‘jumpK’)

pygame.font.init()

time = 0

white = 255, 255, 255
black = 0, 0, 0

land_frequency = 0

land_width = 100


#Save all generated land lands = pygame.sprite.Group()

#player = Player()
create_player = True

player_rect = pygame.Rect(0,0,30,54,) #Judge character creation

jk_begin = True
jk_space = False
jk_left = False
jk_right = False

max_time = 0

#fraction

score = 0

clock = pygame.time.Clock()
Below are some pictures and sound effects we loaded.

#载入图片
background = pygame.image.load(‘tmp/images/back_img.png’)
land = pygame.image.load(‘tmp/images/land.png’)
game_over = pygame.image.load(‘tmp/images/gameover.png’)

player1 = pygame.image.load(‘tmp/images/player_1.png’)
player2 = pygame.image.load(‘tmp/images/player_2.png’)
player3 = pygame.image.load(‘tmp/images/player_3.png’)
player4 = pygame.image.load(‘tmp/images/player_4.png’)
#载入游戏音效
jump = pygame.mixer.Sound(‘tmp/sounds/jump.wav’)
jump.set_volume(15)
game_over_sound = pygame.mixer.Sound(‘tmp/sounds/game_over.wav’)
game_over_sound.set_volume(0.05)
pygame.mixer.music.load(‘tmp/sounds/background.mp3’)
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.5)
接下来,就是需要不断更新的信息。

running = True #Whether the game is over
while running:
simply set the frame rate of the game, background or something.

clock.tick(30)

window.blit(background, (0, 0))
pygame.draw.rect(window, white, [260, 740, 260, 50], 0)

Here is the display velocity bar, the length is displayed according to the time, and we will reset the time later.


#Display velocity bar pygame.draw.rect(window, black, [260, 740, time / 20 * 260, 50], 0)

Here, the speed generated by the platform is controlled by the land_frequency set by yourself.

#产生下落的平台
if land_frequency % 50 == 0:
    land1_pos = [random.randint(0, width-land_width), 0]
    land1 = Land(land, land1_pos)
    lands.add(land1)

land_frequency += 1
if land_frequency >= 100:
    land_frequency = 0

Judgement is used here to ensure that the code is only run once. Prevent strange control of later players.


    #Create characters 
if create_player: 
    create_player = False player_rect.left = land1.rect.left+40 
    player_rect.top = land1.rect.top-55 
    player = Player(player_rect) #Create 
    initial characters 
    window.blit(player1, player. rect)

We will store the land generated each time in a group later, so that we can draw continuously here, using the Move function of land.

And here to judge whether there is a collision between the character and the land, and set the Boolean class at the beginning of "jk_", which will control the process of the program later.

And when the platform is below a certain height, we will eliminate that part of the land from the group.

for tmp_land in lands: #Draw the 
    falling platform 
    window.blit(land, tmp_land.rect) 
    #
    下落tmp_land.Move() 
    if pygame.sprite.collide_circle(tmp_land,player): 
        #score += 100 
        jk_right = False 
        jk_left = False 
        jk_space = False 
        jk_begin = True 
        player.gravity = 4 
    #Remove 

    the platform at the bottom if tmp_land.rect.y >= 650: 
        lands.remove(tmp_land)

When the player’s live attribute is True, we accept the keyboard value,

If it is a space, change the relevant attributes of the player.

add 0.5 to the time, draw the velocity bar,

When time reaches 20, time will not increase.

If there is a left-click or right-click input at the same time, we will change the faceR property of the player to control the x-axis movement direction of the character.

#人物空格移动
if player.live:
    if jk_begin:
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_SPACE]:
            jump.play()
            jk_space = True
            jk_left = False
            jk_right = False
            window.blit(player2, player.rect)
            player.faceR = 0
            player.gravity = 10
            time += 0.5
            player.Move(time)
        else:
            jump.stop()
            jk_begin = False
            time = 0
        if time >= 20:
            jump.stop()
            time = 20
            max_time += 1
            if max_time >= 10:
                jk_begin = False
                time = 0


if key_pressed[pygame.K_LEFT] and key_pressed[pygame.K_SPACE]:
        jk_space = False
        jk_left = True
        jk_right = False
        window.blit(player3, player.rect)
        player.faceR = -1
        player.Move(time)
if key_pressed[pygame.K_RIGHT] and key_pressed[pygame.K_SPACE]:
        jk_space = False
        jk_left = False
        jk_right = True
        window.blit(player4, player.rect)
        player.faceR = 1
        player.Move(time)

We need to make some corrections to the characters to prevent the image of the characters from going beyond the boundaries of the picture.


    #Correct the character over the edge 
if player.rect.y <= -54: 
    player.rect.y = -54 
if player.rect.x <= 0: player.rect.x = 
    0 
    time = 0 jk_begin = False 
if player. rect.x >= 770: player.rect.x 
    = 770

At the same time, when the character's rect.y exceeds 750, the game over picture will be displayed. Also, the player’s live attribute is FALSE so that it will not continue to accept the player’s keyboard commands.


    #Game over 
if player.rect.y >= 750: game_over_sound.play() 
    player.live = False 
    window.blit(game_over, (0, 0))

At the end, we uniformly update the character drawing and when the player types different keys, the different actions of the character (including standing normally, squatting, looking left, looking right)

#更新人物,判断人物动作
if (not jk_right) and (not jk_left) and (not jk_space):
    window.blit(player1, player.rect)
if jk_space:
    window.blit(player2, player.rect)
if jk_left:
    window.blit(player3, player.rect)
if jk_right:
    window.blit(player4, player.rect)
player.Drop()

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
pygame.display.update()

4. Conclusion
This is the first time we use pygame to develop our own games .

Here, we have deleted the operation of pressing the left, right and A, D keys on the platform to control the movement of the characters, and only use the left, right and A, D keys to control the way the characters move.

At the same time, we did not create a large number of terrains to enrich the richness and fun of the game, but only used a constantly descending platform instead of a fixed terrain.

There are also disadvantages that the strange movement trajectory, the character can penetrate the platform from below, and the character will float in the air (this should be related to the judgment of pygame)

If there is another time, I hope to correct these shortcomings.

Guess you like

Origin blog.51cto.com/14621511/2679147
Recommended