当前位置:   article > 正文

Python飞机大战,小白动手的第一个游戏!(附源码和图片)_python飞机大战源代码

python飞机大战源代码

Python飞机大战,适合小白动手的项目(附源码和图片)

由于飞机是每按下一次移动键才会动一下,这就令人很难受,下面的代码将会告诉你如何一直按键不松飞机也会随之一直动!

一、先按如下路径关系创建 feiji.py 文件和 images 文件夹。
在这里插入图片描述
二、把代码复制进去,源码如下。

#coding=utf-8
import pygame
from pygame.locals import *
import time
import random
#飞机基类和子弹基类的基类
class Base(object):
    def __init__(self, screen_temp, x, y, image_name):
        self.x = x
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load(image_name)

#飞机基类
class BasePlane(Base):
    def __init__(self, screen_temp, x, y, image_name):
        Base.__init__(self,screen_temp,x,y,image_name)
        self.bullet_list = []

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            if bullet.judge(): #判断子弹是否越界
                self.bullet_list.remove(bullet)
#子弹基类
class BaseBullet(Base):
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
#玩家飞机类
class HeroPlane(BasePlane):
    def __init__(self, screen_temp):
        BasePlane.__init__(self, screen_temp,205,700,"./images/hero1.png")
        self.key_right_status = False
        self.key_left_status = False
        self.key_up_status = False
        self.key_down_status = False

    def move(self):
        if self.key_right_status:
            self.x += 3
        if self.key_left_status:
            self.x -= 3
        if self.key_down_status:
            self.y += 3
        if self.key_up_status:
            self.y -= 3

    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))

#敌机类
class EnemyPlane(BasePlane):
    def __init__(self, screen_temp):
        BasePlane.__init__(self, screen_temp,0,0,"./images/enemy0.png")
        self.direction = "right"#控制敌机默认显示方向

    def move(self):
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5
        if self.x > 420:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"

    def fire(self):
        random_num = random.randint(1,50)
        if random_num == 20 or random_num == 21:
            self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))

#玩家子弹类
class Bullet(BaseBullet):
    def __init__(self, screen_temp,x,y):
        BaseBullet.__init__(self,screen_temp,x+40,y-20,"./images/bullet.png")

    def move(self):
        self.y -= 10

    def judge(self):
        if self.y <  0:
            return True
        else:
            return False

#敌机子弹类
class EnemyBullet(BaseBullet):
    def __init__(self, screen_temp,x,y):
        BaseBullet.__init__(self,screen_temp,x+22,y+30,"./images/bullet1.png")

    def move(self):
        self.y += 10

    def judge(self):
        if self.y > 852:
            return True
        else:
            return False

#键盘控制函数
def key_control(hero_temp):
    #获取事件,比如按键等
    for event in pygame.event.get():

        #判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        #判断是否是按下了键
        elif event.type == KEYDOWN:
            #检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero_temp.key_left_status = True
                #hero_temp.move_left()
            #检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero_temp.key_right_status = True
                #hero_temp.move_right()
            #检测按键是否是w或者up
            elif event.key == K_w or event.key == K_UP:
                hero_temp.key_up_status = True
            #检测按键是否是s或者down
            elif event.key == K_s or event.key == K_DOWN:
                hero_temp.key_down_status = True
            #检测按键是否是空格键
            elif event.key == K_SPACE:
                print('space')
                hero_temp.fire()
        #判断是否是松了键
        elif event.type == KEYUP:
            if event.key == K_a or event.key == K_LEFT:
                hero_temp.key_left_status = False
            elif event.key == K_d or event.key == K_RIGHT:
                hero_temp.key_right_status = False
            elif event.key == K_w or event.key == K_UP:
                hero_temp.key_up_status = False
            elif event.key == K_s or event.key == K_DOWN:
                hero_temp.key_down_status = False


  • 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
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144

三、图片。
收集不易,点赞关注
https://download.csdn.net/download/qq_42776165/12243348?spm=1001.2014.3001.5501

四、运行。
运行效果如下,因为我一只手拿着手机录视频所以操作很low、了
在这里插入图片描述
制作不易,看完点赞是个好习惯~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/105352?site
推荐阅读
相关标签
  

闽ICP备14008679号