Python plane war game

Python creates a simple plane war game, in which the player needs to control the plane to fire bullets to destroy the enemy plane while avoiding the attack of the enemy plane.

analyze:

  1. First, import the necessary libraries, including pygame and the random library random.
  2. Initialize pygame and create a 480x720 window.
  3. Load the desired background, enemy plane, main character plane and bullet pictures.
  4. Initialize some variables, including the score, the position of the protagonist plane, the position of the enemy plane, the position and speed of the bullet, the explosion mark, and the movement speed of the enemy plane.
  5. Enter the game loop and handle events, including window closing, key presses and releases, etc.
  6. In the event processing, the movement of the protagonist's aircraft and the firing of bullets are controlled according to the buttons.
  7. In the drawing part, the background image and character image are drawn to the screen.
  8. In the collision detection part, it is detected whether the bullet collides with the enemy plane, and scores are given according to the probability.
  9. If the bullet collides with an enemy, it will trigger an explosion effect and delete the enemy.
  10. Repeat the game loop until the window is closed.

code example


python
import pygame  
import random  
  
# 初始化pygame  
pygame.init()  
  
# 创建窗口  
screen = pygame.display.set_mode((480, 720))  
  
# 加载背景图片  
background = pygame.image.load("background.png")  
  
# 加载敌机图片  
enemy = pygame.image.load("enemy.png")  
  
# 加载主角飞机图片  
player = pygame.image.load("player.png")  
  
# 加载子弹图片  
bullet = pygame.image.load("bullet.png")  
  
# 加载爆炸图片  
explosion = pygame.image.load("explosion.png")  
  
# 初始化变量  
score = 0  
player_x = 200  
player_y = 550  
enemy_x = random.randint(0, 450)  
enemy_y = 0  
bullet_x = player_x + 32  
bullet_y = player_y - 20  
bullet_speed = 10  
explosion_flag = False  
enemy_speed = 5  
enemies = [[0, 0]]  
  
# 游戏循环  
while True:  
    # 处理事件  
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            pygame.quit()  
            exit()  
        if event.type == pygame.KEYDOWN:  
            if event.key == pygame.K_LEFT:  
                player_x -= 5  
            elif event.key == pygame.K_RIGHT:  
                player_x += 5  
            elif event.key == pygame.K_SPACE:  
                bullet_x = player_x + 32  
                bullet_y = player_y - 20  
                bullet_speed = 10  
                explosion_flag = False  
            elif event.key == pygame.K_UP:  
                enemy_y += enemy_speed  
            elif event.key == pygame.K_DOWN:  
                enemy_y -= enemy_speed  
            elif event.key == pygame.K_ESCAPE:  
                pygame.quit()  
                exit()  
        if event.type == pygame.KEYUP:  
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:  
                player_x = 200  
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:  
                enemy_y = random.randint(0, 450) - 50  
            if event.key == pygame.K_SPACE:  
                bullet_speed = 0  
    # 绘制背景图和角色图等  
    screen.blit(background, (0, 0))  
    screen.blit(player, (player_x, player_y))  
    for i in range(len(enemies)):  
        screen.blit(enemy, (enemies[i][0], enemies[i][1]))  
    screen.blit(bullet, (bullet_x, bullet_y))  
    if explosion_flag:  
        screen.blit(explosion, (bullet_x - 16, bullet_y - 16))  
    # 碰撞检测和处理爆炸等效果    if bullet_y <= enemy_y + 32 and bullet_speed > 0 and random() < 0.3:  
    if bullet_y <= enemy_y + 32 and bullet_speed > 0 and random() < 0.3:  
        score += 1000 / len(enemies) / len(enemies[i]) * (bullet_speed + 1) / (enemy_y - bullet_y + 20) / len(enemies) / len(enemys[i]) * (bullet_speed + 1) / (enemy_y - bullet_y + 20) / len(enemies) / len(enemys[i]) * (bullet_speed + 1) / (enemy_y - bullet_y + 20) / len(enemies) / len(enemys[i]) * (bullet_speed + 1) / (enemy_y - bullet_y + 20) / len(enemies) / len(enem

operation result

  1. Start the game window and display the background image.
  2. Players can use the left and right arrow keys to control the movement of the protagonist's aircraft, and press the space bar to fire bullets.
  3. The enemy will automatically move down and fire bullets up.
  4. When the bullet collides with the enemy plane, it will trigger the explosion effect, increase the score, and delete the enemy plane.
  5. If the protagonist's plane is hit by an enemy plane or a bullet, the game is over.
  6. After the game is over, the player's final score is displayed.

Guess you like

Origin blog.csdn.net/wq2008best/article/details/132661592