Home > Article > WeChat Applet > Introduction to the example of developing WeChat masturbation game on PC with pygame
pygame develops PC-side WeChat masturbation game
1. Project Introduction
1. Introduction
This project is similar to the once popular WeChat masturbation game. The game will be developed using Python language, mainly using the pygame API. The game will eventually be completed in the form of python source file game.py. You only need to run python game.py to enter the game.
The screenshot of the final effect of the game is as follows:
2. Knowledge points
This experiment will introduce how to use the Linux desktop environment How to quickly develop small games using Python and pygame. You can get started with pygame game development through this game.
3. Reference documentation
Code reference PythonShootGame written by Kill-Console
Document reference pygame documentation
4. Install dependency packages
(below The content needs to be implemented on the official website of the experimental building, you can skip it if it is not needed)
The pygame library needs to be installed to support the running of the code required for this experiment.
Open the Xfce terminal in the experimental environment and enter the following command to install pygame. You will be prompted to enter shiyanlou’s password. The password is also shiyanlou:
$ sudo apt-get update$ sudo apt-get install python-pygame
2. Technical Design
1. Game Characters
The characters required in this game include player aircraft, enemy aircraft and bombs. Users can move the position of the player's aircraft on the screen through the keyboard to attack enemy aircraft at different locations. Therefore, the following three classes of Player, Enemy and Bullet are designed to correspond to the three game characters.
For Player, the required operations are shooting and movement. Movement is divided into four situations: up, down, left and right.
For Enemy, it is relatively simple, just move it, appear from the top of the screen and move to the bottom of the screen.
For Bullet, it is the same as the airplane, it only needs to move at a certain speed.
2. Game functions
I believe friends who have played WeChat masturbation are familiar with it, so the game has been simplified here. The speed of the plane is fixed, and the speed of the bomb is fixed. The basic operation is to move the player plane. The target plane randomly appears from the top of the screen and falls to the bottom at a constant speed. The bomb is sent from the player plane and will be destroyed when it hits the target plane. If the target plane If it touches the player's plane, the game will be over and the score will be displayed.
3. Code implementation
1. Interface display
The resources image files required for code implementation can be obtained through the following command:
$git clone https://github .com/shiyanlou/PythonShootGame.git
Detailed steps
Initialize pygame
Set the game interface size, background image and title
The game main loop needs to handle the initialization, update and exit of the game interface
Display player aircraft (the resources/image/shoot.png used in the code contains a variety of aircraft, you only need to use pygame .image's subsurface API intercepts the required pictures in shoot.png based on the position)
Sample code
#1. 初始化pygame pygame.init() #2. 设置游戏界面大小、背景图片及标题 # 游戏界面像素大小 screen = pygame.display.set_mode((480, 800)) # 游戏界面标题 pygame.display.set_caption('飞机大战') # 背景图 background = pygame.image.load('resources/image/background.png').convert() # Game Over的背景图 game_over = pygame.image.load('resources/image/gameover.png') # 飞机图片 plane_img = pygame.image.load('resources/image/shoot.png') # 截取玩家飞机图片 player = plane_img.subsurface(pygame.Rect(0, 99, 102, 126)) #3. 游戏主循环内需要处理游戏界面的初始化、更新及退出 while True: # 初始化游戏屏幕 screen.fill(0) screen.blit(background, (0, 0)) # 显示玩家飞机在位置[200,600] screen.blit(player, [200, 600]) # 更新游戏屏幕 pygame.display.update() # 游戏退出事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit()
2. Event processing
Processes keyboard input in the main loop Events (up, down, left, and right key operations), increase game operation interaction (up, down, left, and right movement of the player's aircraft).
Detailed steps
Get keyboard events (up, down, left and right keys)
Process keyboard events (moving the position of the aircraft)
Put the above step code into the game
Sample code in the main loop
#1. 获取键盘事件(上下左右按键) key_pressed = pygame.key.get_pressed() #2. 处理键盘事件(移动飞机的位置) if key_pressed[K_w] or key_pressed[K_UP]: player.moveUp() if key_pressed[K_s] or key_pressed[K_DOWN]: player.moveDown() if key_pressed[K_a] or key_pressed[K_LEFT]: player.moveLeft() if key_pressed[K_d] or key_pressed[K_RIGHT]: player.moveRight()
3. ***Processing
*** is emitted by the player's aircraft and moves towards the top of the interface at a certain speed.
Detailed steps
Generate ***, you need to control the emission frequency
Move at a fixed speed***
Delete after moving out of the screen***
Effect processing of enemy aircraft being hit by *** (processed in the next section)
Sample code
#1. 生成***,需要控制发射频率 # 首先判断玩家飞机没有被击中 if not player.is_hit: if shoot_frequency % 15 == 0: player.shoot(bullet_img) shoot_frequency += 1 if shoot_frequency >= 15: shoot_frequency = 0 for bullet in player.bullets: #2. 以固定速度移动*** bullet.move() #3. 移动出屏幕后删除*** if bullet.rect.bottom < 0: player.bullets.remove(bullet) # 显示*** player.bullets.draw(screen)
4. Enemy aircraft processing
Enemy aircraft It needs to be generated randomly above the interface and move downwards at a certain speed.
Detailed steps
To generate enemy aircraft, you need to control the generation frequency
Move the enemy aircraft
Handling the collision effect between enemy aircraft and player aircraft
Delete the enemy aircraft after moving out of the screen
Effect processing of enemy aircraft being hit by ***
Sample code
#1. 生成敌机,需要控制生成频率 if enemy_frequency % 50 == 0: enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0] enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos) enemies1.add(enemy1) enemy_frequency += 1if enemy_frequency >= 100: enemy_frequency = 0 for enemy in enemies1: #2. 移动敌机 enemy.move() #3. 敌机与玩家飞机碰撞效果处理 if pygame.sprite.collide_circle(enemy, player): enemies_down.add(enemy) enemies1.remove(enemy) player.is_hit = True break #4. 移动出屏幕后删除飞机 if enemy.rect.top < 0: enemies1.remove(enemy) #5. 敌机被***击中效果处理 # 将被击中的敌机对象添加到击毁敌机Group中,用来渲染击毁动画 enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1) for enemy_down in enemies1_down: enemies_down.add(enemy_down) # 敌机被***击中效果显示 for enemy_down in enemies_down: if enemy_down.down_index == 0: pass if enemy_down.down_index > 7: enemies_down.remove(enemy_down) score += 1000 continue screen.blit(enemy_down.down_imgs[enemy_down.down_index / 2], enemy_down.rect) enemy_down.down_index += 1 # 显示敌机 enemies1.draw(screen)
5. Score display
in A fixed position on the game interface displays how many target enemy aircraft have been destroyed.
Sample code
# 绘制得分 score_font = pygame.font.Font(None, 36) score_text = score_font.render(str(score), True, (128, 128, 128)) text_rect = score_text.get_rect() text_rect.topleft = [10, 10] screen.blit(score_text, text_rect)
The above is the detailed content of Introduction to the example of developing WeChat masturbation game on PC with pygame. For more information, please follow other related articles on the PHP Chinese website!