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


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

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

  • 60
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
非常感谢您的提问,以下是一个简单的飞机大战游戏Python 源码: ```python import pygame import random # 初始化 Pygame pygame.init() # 设置游戏窗口大小 screen_width = 480 screen_height = 700 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置游戏标题 pygame.display.set_caption("飞机大战") # 加载背景图片 background = pygame.image.load("images/background.png") # 加载飞机图片 player_img = pygame.image.load("images/player.png") player_rect = player_img.get_rect() player_rect.centerx = screen_width // 2 player_rect.bottom = screen_height - 10 # 加载子弹图片 bullet_img = pygame.image.load("images/bullet.png") bullet_rect = bullet_img.get_rect() # 加载敌机图片 enemy_img = pygame.image.load("images/enemy.png") enemy_rect = enemy_img.get_rect() # 设置字体 font = pygame.font.SysFont(None, 48) # 设置游戏状态 game_over = False # 设置计分 score = 0 # 设置敌机列表 enemies = [] for i in range(5): enemy_rect = enemy_img.get_rect() enemy_rect.left = random.randint(0, screen_width - enemy_rect.width) enemy_rect.top = random.randint(-screen_height, 0) enemies.append(enemy_rect) # 游戏循环 while not game_over: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True # 移动玩家飞机 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_rect.move_ip(-5, 0) if keys[pygame.K_RIGHT]: player_rect.move_ip(5, 0) if keys[pygame.K_UP]: player_rect.move_ip(0, -5) if keys[pygame.K_DOWN]: player_rect.move_ip(0, 5) # 移动子弹 for bullet_rect in bullets: bullet_rect.move_ip(0, -10) if bullet_rect.bottom < 0: bullets.remove(bullet_rect) # 移动敌机 for enemy_rect in enemies: enemy_rect.move_ip(0, 5) if enemy_rect.top > screen_height: enemy_rect.left = random.randint(0, screen_width - enemy_rect.width) enemy_rect.top = random.randint(-screen_height, 0) # 检测碰撞 for enemy_rect in enemies: if player_rect.colliderect(enemy_rect): game_over = True for bullet_rect in bullets: if bullet_rect.colliderect(enemy_rect): score += 1 enemies.remove(enemy_rect) bullets.remove(bullet_rect) # 绘制游戏界面 screen.blit(background, (0, 0)) screen.blit(player_img, player_rect) for bullet_rect in bullets: screen.blit(bullet_img, bullet_rect) for enemy_rect in enemies: screen.blit(enemy_img, enemy_rect) score_text = font.render("Score: " + str(score), True, (255, 255, 255)) screen.blit(score_text, (10, 10)) pygame.display.update() # 结束游戏 pygame.quit() ``` 希望这个源码能够帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值