Python实现简单飞机大战

时间:2022-03-05 18:36:37

本文实例为大家分享了python实现简单飞机大战的具体代码,供大家参考,具体内容如下

功能

玩家飞机可以移动,可以发射子弹,敌机随机产生,*坠落。未添加击落敌机的功能。主要用来练习类的封装与继承。

源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding=utf-8 -*-
import pygame, random
from pygame.locals import *
 
 
class base(object):
    def __init__(self, x, y, screen):
        self.screen = screen
        self.x = x
        self.y = y
 
 
class player(base):
    def __init__(self):
        super().__init__(210, 600, screen)
        self.playername = "./feiji/hero.gif"
        self.player = pygame.image.load(self.playername).convert()
        self.bullet_list = []
 
    def display(self):
        self.screen.blit(self.player, (self.x, self.y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.mov()
 
    def move_left(self):
        self.x -= 10
 
    def move_right(self):
        self.x += 10
 
    def move_up(self):
        self.y -= 10
 
    def move_down(self):
        self.y += 10
 
    def shot(self):
        bullet = bullet()
        self.bullet_list.append(bullet)
 
 
class bullet(base):
    def __init__(self):
        super().__init__(player.x+40, player.y-20, screen)
        self.bulletname = "./feiji/bullet-3.gif"
        self.bullet = pygame.image.load(self.bulletname).convert()
 
    def display(self):
        self.screen.blit(self.bullet, (self.x, self.y))
 
    def mov(self):
        self.y -= 10
 
 
class enemy(base):
    def __init__(self):
        super().__init__(random.randint(1, 10)*40, 0, screen)
        self.enemyname = "./feiji/enemy-1.gif"
        self.enemy = pygame.image.load(self.enemyname).convert()
        self.enemy_list = []
        self.i = 0
 
    def mov(self):
        self.y += 2
 
 
class enemyarea(enemy):
    def display(self):
        # 每隔一段时间创建一架敌机
        if self.i % 80 == 0:
            new_enemy = enemy()
            self.enemy_list.append(new_enemy)
        self.i += 1
        for enemy in self.enemy_list:
            self.screen.blit(enemy.enemy, (enemy.x, enemy.y))
            enemy.mov()
 
 
if __name__ == '__main__':
    # 创建一个窗口,用来显示内容
    screen = pygame.display.set_mode((480, 890))
    # 创建一个和窗口大小的图片,用来充当背景
    background = pygame.image.load("./feiji/background.png").convert()
    player = player()
    enemy = enemy()
    enemy_area = enemyarea()
    while true:
        screen.blit(background, (0, 0))
        player.display()
        enemy_area.display()
        # 判断按键
        for event in pygame.event.get():
            if event.type == quit:
                exit()
            elif event.type == keydown:
                if event.key == k_a or event.key == k_left:
                    player.move_left()
                elif event.key == k_d or event.key == k_right:
                    player.move_right()
                elif event.key == k_w or event.key == k_up:
                    player.move_up()
                elif event.key == k_s or event.key == k_down:
                    player.move_down()
                elif event.key == k_space:
                    player.shot()
                elif event.key == k_q:
                    exit()
        pygame.display.update()
        pygame.time.delay(10)

结果展示

Python实现简单飞机大战

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/l_changyun/article/details/85805444