pygame game development | ice and snow hero club for game development and design of China Computer Design Competition (with source code)

Keywords: Python Game Development pygame

It is not easy to be original. Plagiarism and reprint are prohibited in this paper. Based on the summary of many years of practical development experience of reptiles, infringement must be investigated!

 

1, Game creativity and setting

1.1 game creativity

  • The game takes the 2022 Zhangjiakou Winter Olympic Games as the creative background. The picture and the picture are designed with the theme that the two mascots recapture 24 emblem in the battle and successfully meet
  • Provide UI, Bgm, rich game materials, and support rich functions such as dynamic interaction, battle blasting, random props, life bar control, etc

 

 

1.2 game settings

Because the emblem of the previous winter Olympic Games was stolen by aliens, Heroes (ice pier or snow Rongrong) were ordered to seize the emblem. With the help of the space station (navigation, tracking and other functions), they set foot on the road of the ice and snow hero conference. When 24 emblem were collected, the hero ice pier and snow Rongrong successfully joined forces, and the mission was successfully completed, Ice pier or snow Rongrong will return to the Beijing Winter Olympic Games. But the story of ice and snow heroes is far from over... So that players can understand the history of the Winter Olympics while playing the game


 

 

 

2, Function introduction

The main functions are as follows:

  • Game start interface: the start interface includes five function options: hero selection, difficulty selection, start, exit and about the game

 

  •   Preparation interface: click "Play" to jump to the preparation page of the game, click "space" to start the game, and click "ESC" to exit the game interface

 

  •   Game interface: the top of the game interface displays information such as "life value", "blood volume", "number of collected emblem" and so on; In the game, heroes should avoid obstacles such as ice cones and stones. Large ice cones can be dispersed into small ice cones. Operate up, down, left and right to move the hero's position. Use the space bar to launch an attack. Hit an obstacle to destroy the obstacle and drop the emblem randomly. The hero can pick up props when he encounters the emblem or the life orange gem randomly dropped in the background

 

  •   Game victory interface: if the game fails, the black page will be displayed. Click "Replay" to play the game again, and click "Quit" to exit the game

 

  •   Game failure interface: the game is successful, the white interface is displayed, click "Replay" to play the game again, and click "Quit" to exit the game

 

 

3, Technical route

The basic functions are as follows, and only some codes are shown:

  • The emblem image is stored in the list, and the emblem is dropped randomly by combining the for loop and random method
    1 huihui_images = []
    2 for i in range(1,25):
    3   huihui_images_dir = path.join(img_dir,'{}.jpg'.format(i))
    4   huihui_image = pygame.image.load(huihui_images_dir).convert()
    5   huihui_images.append(huihui_image)

 

  • Using Pygame_menu to realize dynamic menu
     1 def show_beign_menu():
     2     menu = pygame_menu.Menu(600,
     3             800,
     4             'Welcome',
     5             theme=pygame_menu.themes.THEME_ORANGE)
     6 
     7     # menu.add_text_input('Player', default='John')
     8     menu.add_selector('Hero', [('Bing dundun', 1), ('Xue rongrong', 2)],
     9             onchange=set_difficulty)
    10     menu.add_selector('Difficulty', [('Easy', 1), ('Hard', 2)],
    11             onchange=set_difficulty)
    12     menu.add_button('Play', main)
    13     menu.add_button('Quit', pygame_menu.events.EXIT)
    14     menu.add_button('About')
    15     menu.mainloop(screen)

 

  • The game Wizard of Pygame is used to load pictures and bgm
     1 img_dir = path.join(path.dirname(__file__),'img')
     2 background_dir = path.join(img_dir,'space.png')
     3 background_img1 = pygame.image.load(background_dir).convert()
     4 background_img2 = pygame.image.load(background_dir).convert()
     5 background_img3 = pygame.image.load('img/lantian.jpg').convert()
     6 background_rect = background_img1.get_rect()
     7 player_dir = path.join(img_dir,'hero_bingdundun.jpg')
     8 player_img = pygame.image.load(player_dir).convert()
     9 player_img_small = pygame.transform.scale(player_img,(35,40))
    10 player_img_small.set_colorkey((0,0,0))

 

  • The spriteGroup of Pygame is used to generate batch game sprites, and collision sprite detection can be used to judge whether there is interaction between game sprites
     1 hits = pygame.sprite.spritecollide(player,powerups,True)
     2         for hit in hits:
     3           if hit.type == 'add_hp':
     4             player.hp += 50
     5             if player.hp > 100:
     6               player.hp = 100
     7           elif hit.type == 'add_life':
     8             player.lives += 1
     9             if player.lives > 3:
    10               player.lives = 3
    11           else:
    12             player.fire_missile()
    13 
    14         hits = pygame.sprite.spritecollide(player,huihuis,True)
    15         for hit in hits:
    16           player.huihui += 1
    17           if player.huihui == 24:
    18             game_over == True
    19             show_success_menu()

 

  • Pygame's drawing function is used to realize life progress, number of emblem and life progress
     1 def draw_ui():
     2   pygame.draw.rect(screen,(0,255,0),(10,10,player.hp,15))
     3   if player.hp < 50:
     4     pygame.draw.rect(screen,(255,0,0),(10,10,player.hp,15)) 
     5   pygame.draw.rect(screen,(255,255,255),(10,10,100,15),2)
     6  
     7   # draw_text('scores:' + str(player.score),screen,(0,255,0),20,WIDTH/2,10)
     8   draw_text('emblem:' + str(player.huihui),screen,(0,255,0),20,WIDTH/2,10)
     9 
    10   img_rect = player_img_small.get_rect()
    11   img_rect.right = WIDTH - 10
    12   img_rect.top = 10
    13   for _ in range(player.lives):
    14     screen.blit(player_img_small,img_rect)
    15     img_rect.x -= img_rect.width + 10

 

  • Background picture loop playback recursive implementation of dynamic background
     1 class Dynamic_Background1(pygame.sprite.Sprite):
     2 
     3   def __init__(self):
     4     pygame.sprite.Sprite.__init__(self)
     5     self.image = pygame.transform.flip(background_img1,False,False)
     6     self.rect = self.image.get_rect()
     7     self.speed = 3
     8     self.last_time = pygame.time.get_ticks()
     9 
    10   def update(self):
    11     now = pygame.time.get_ticks()
    12     if now - self.last_time > 5:
    13       self.rect.y += self.speed
    14       self.last_time = now
    15       while self.rect.y >= HEIGHT:
    16         self.rect.y = -self.rect.height
    17         dynamic_background2.update()
    18 
    19 
    20     for event in pygame.event.get():
    21       if event.type == pygame.QUIT:
    22         game_over == True
    23       if event.type == pygame.KEYDOWN:
    24         if event.type == pygame.K_ESCAPE:
    25           game_over == True

 

  • Use Pygame's drawing function blit() or draw() to put all the picture materials on the game screen
     1 screen.blit(dynamic_background1.image,dynamic_background1.rect)
     2 screen.blit(dynamic_background2.image,dynamic_background2.rect)
     3 screen.blit(player.image,player.rect)
     4 screen.blit(spaceship_zhan_shiwu.image,spaceship_zhan_shiwu.rect)
     5 # screen.blit(space_plant_entity.image,space_plant_entity.rect)
     6 enemys.draw(screen)
     7 xuehuas.draw(screen)
     8 bullets.draw(screen)
     9 missiles.draw(screen)
    10 explosions.draw(screen)
    11 powerups.draw(screen)
    12 huihuis.draw(screen)
    13 spaceships.draw(screen)
    14 spaceship_bullets.draw(screen)

 

  • Use the for loop and rotation function to continuously play the static pictures to achieve a series of dynamic effects, such as snowflakes, explosions and so on
    1 explosion_animation = []
    2 for i in range(9):
    3   explosion_dir = path.join(img_dir,'regularExplosion0{}.png'.format(i))
    4   explosion_img = pygame.image.load(explosion_dir).convert()
    5   explosion_animation.append(explosion_img)

 

  • According to the amount of bingdun's blood, use the algorithm to realize the drop probability of props (add blood, add life, emblem, etc.)
     1 if self.hidden and now - self.hide_time > 1000:
     2       self.hidden = False
     3       self.rect.bottom = HEIGHT
     4       self.hp = 100
     5       if self.hp < 50:
     6         pygame.draw.rect(screen,(255,0,0),(10,10,player.hp,15))
     7 
     8 
     9     if self.is_missile_firing:
    10       if now - self.start_missile_time <= MISSILE_LIFETIME:
    11         if now - self.last_missile_time > MISSILE_INTERVAL:
    12           missile = Missile(self.rect.center)
    13           missiles.add(missile)
    14           self.last_missile_time = now
    15       else:
    16         self.is_missile_firing = False

 

 

4, Game effect

Display of game effects:

 

 

 

5, Source download

pygame game development source code download method:

  • Download link: Portal
  • Focus on my original official account, and get the complete project, including source code, game pictures, music and sound effects. The game has been packed and played directly.

Originality is not easy. If you think it's a little bit useful, I hope you can give me some praise. Thank you, old fellow!

 

 

6, Author Info

Author: under Nanke tree, Goal: make programming more interesting!

The original WeChat official account: "little hung starsky technology", focusing on algorithms, crawlers, web sites, game development, data analysis, Natural Language Processing, AI, etc., looking forward to your attention, let's grow together and Coding together!

Reprint Description: plagiarism and reprint are prohibited in this article, and infringement must be investigated!

 

  Welcome to the code of mine, I am interested in my original official account.

  

-  -  -  -  —  END  -  -  -  -  -- 

           Welcome to scan code to pay attention to my official account.

Xiaohong XingKong Technology

       

 

Posted by StathisG on Tue, 30 Nov 2021 13:20:44 -0800