入门级用Python + tkinter的打飞机/飞机大战小游戏

一、

前置:python3.5、pycharm、pygame(pycharm内安装)

二、工程下载链接

点击下载代码(源码+资源文件)

https://download.csdn.net/download/m0_38078987/10420779

三、描述

1.运行截图

自己把敌机换成灭霸了,还挺有趣


四、源码

import pygame
from pygame.locals import*
import random
#是指游戏屏幕带下
SCREEN_WIDTH=480
SCREEN_HEIGHT=800
#子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self,bullet_img,init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image =bullet_img
        self.rect =self.image.get_rect()
        self.rect.midbottom =init_pos
        self.speed =10

    def move(self):
        self.rect.top -= self.speed

#2定义一个飞机类
class Player(pygame.sprite.Sprite):
    def __init__(self,plane_img,player_rect,init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image =[]
        for i in range(len(player_rect)):
            self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
        self.bullets = pygame.sprite.Group()
        self.rect =player_rect[0]#为图片的矩形初始化赋值
        self.rect.topleft=init_pos#初始化位置
        self.speed=10#初始化飞机的速度
        self.img_index=1#初始化图片的索引  1代表第二张图片
        #发射子弹前提是没被击中
        self.is_hit =False
#4.发射子弹
    def shoot(self,bullet_img):
        bullet =Bullet(bullet_img,self.rect.midtop)
        self.bullets.add(bullet)
    def moveUp(self):
        if self.rect.top <= 0:  # rect.top<=0  飞机不能出去
            self.rect.top = 0
        else:
            self.rect.top -= self.speed  # 飞机距离上边的位置 等于飞机距离x轴-速度

    def moveDown(self):
        if self.rect.top >= SCREEN_HEIGHT - self.rect.height:  # 屏幕的高度-飞机的高度
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top += self.speed  # 高度-速度

    def moveLeft(self):
        if self.rect.left <= 0:
            self.rect.left = 0
        else:
            self.rect.left -= self.speed

    def moveRight(self):
        if self.rect.left >= SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left += self.speed
#敌机类
class Enemy(pygame.sprite.Sprite):
    def __init__(self,enemy_img,enemy_down_imgs,init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image =enemy_img
        self.rect=self.image.get_rect()
        self.rect.topleft =init_pos
        self.down_imgs =enemy_down_imgs
        self.speed=2
        self.down_index=0

    def move(self):
        self.rect.top += self.speed
#初始化pygame
pygame.mixer.init()
pygame.init()




#设计游戏界面像素,第一个参数是分辨率  宽480 高800    第二个是标志位   三是色深
#返回值Surface对象   位图的一种
screen=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

#设计游戏的标题
pygame.display.set_caption("飞机大战")



#背景音乐
# pygame.mixer.music.load("resources/sound/bullet.mp3")
# pygame.mixer.music.play()
# pygame.mixer.music.load("resources/sound/background.mp3")
# pygame.mixer.music.play()

pygame.mixer.music.load("resources/sound/background.mp3")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)

# bullet_sound = pygame.mixer.Sound("resources/sound/surface.mp3")
# bullet_sound.set_volume(0.2)






#背景图设计
background=pygame.image.load("resources/image/background.png")
#player图片
plane_img=pygame.image.load("resources/image/shoot.png")
plane_mie=pygame.image.load("resources/image/mieba.png")
game_over=pygame.image.load("resources/image/gameover.png")
#子弹
bullet_rect = pygame.Rect(1004, 987, 9, 21)
bullet_img = plane_img.subsurface(bullet_rect)
#敌机图片
enemy1_rect = pygame.Rect(32, 2, 45, 54)
# enemy1_rect = pygame.Rect(534, 612, 57, 43)
enemy1_img = plane_mie.subsurface(enemy1_rect)

enemy1_down_imgs = []
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 347, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873, 697, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 296, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930, 697, 57, 43)))
enemies1 = pygame.sprite.Group()

#截取飞机图片
# pygame.Rect(left,top,width,height) 生成一个Rect对象的位置
# pygame.image的subsurface API接口    根据位置截取截图shoot.png
#player=plane_img.subsurface(pygame.Rect(0,100,102,127))

#玩家飞机图  玩家ssssss不同飞机的图片类表
#放飞机的容器


player_rect=[]
player_rect.append(pygame.Rect(0,99,102,126))
player_rect.append(pygame.Rect(165,360,102,126))
player_rect.append(pygame.Rect(165, 234, 102, 126))  # 玩家爆炸图片
player_rect.append(pygame.Rect(330, 624, 102, 126))
player_rect.append(pygame.Rect(330, 498, 102, 126))
player_rect.append(pygame.Rect(432, 624, 102, 126))
player_pos=[200,500]
player=Player(plane_img,player_rect,player_pos)
#游戏循环内保持游戏界面的初始化   更新   或者退出等操作
#激毁动画
enemies_down = pygame.sprite.Group()
#初始化游戏射击和移动频率
shoot_frequency=0
score = 0
player_down_index = 15
#敌机的移动频率
enemy_frequency=0
running =True




# s = pygame.mixer.Sound("resources/sound/background.mp3")
# pygame.mixer.music.set_volume(0.9)
# s.play()
while running:
    # 初始化游戏屏幕
    screen.fill(0)
    # 将背景图画到界面中   blit  第一个参数是SUbsub对象   第二个是位置
    screen.blit(background,(0,0))
    #射击频率
    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_SPACE] and shoot_frequency%15==0:
        bullet_sound = pygame.mixer.Sound("resources/sound/bullet3.wav")
        pygame.mixer.music.set_volume(0.1)
        bullet_sound.play()
        player.shoot(bullet_img)
    #if key_pressed[k_SPACE]:
    # if not player.is_hit and shoot_frequency%15==0:
    #     player.shoot(bullet_img)
    shoot_frequency +=1
    if shoot_frequency >=15:
        shoot_frequency =0
    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 += 1
    if enemy_frequency >= 100:
        enemy_frequency = 0
    enemies1_down =pygame.sprite.groupcollide(enemies1,player.bullets,1,1)
    for enemy_down in enemies1_down:
        enemies_down.add(enemy_down)
    for enemy in  enemies1:
        enemy.move()
        if pygame.sprite.collide_circle(enemy,player):
            enemies_down.add(enemy)
            enemies1.remove(enemy)
            player.is_hit =True
            break
        if enemy.rect.top<0:
            enemies1.remove(enemy)


    enemies1.draw(screen)
    #循环我的子弹
    for bullet in player.bullets:
        # bullet_sound.play()
        bullet.move()
        if bullet.rect.bottom<0:
            player.bullets.remove(bullet)
        #将玩家飞机画面中的bilt函数
    if not player.is_hit:
        screen.blit(player.image[player.img_index], player.rect)
        player.img_index = shoot_frequency // 8
    else:
        player.img_index=player_down_index//8
        screen.blit(player.image[player.img_index],player.rect)
        player_down_index +=1
        if player_down_index>47:
            running =False
    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 +=100
            continue
        screen.blit(enemy_down.down_imgs[enemy_down.down_index//2],enemy_down.rect)
        enemy_down.down_index +=1

    player.bullets.draw(screen)
    #if player.img_index==0:
     #   player.img_index =1
    #else:
     #   player.img_index =0
    # 将玩家飞机画到界面中  blit函数
    #screen.blit(player,[200,500])
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    #更新游戏屏幕

    #绘图得分
    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)
    # 刷新   画图完之后一定要更新
    pygame.display.update()
    #获取键盘的事件
    key_pressed = pygame.key.get_pressed()
    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()
    #游戏的退出时间



#最终得分
pygame.mixer.music.stop()
font = pygame.font.Font(None,48)
text =font.render("Score:"+str(score),True,(250,0,0))
text_rect=text.get_rect()
text_rect.centerx=screen.get_rect().centerx
text_rect.centerx=screen.get_rect().centery
screen.blit(game_over,(0,0))
screen.blit(text,text_rect)

# key_pressed = pygame.key.get_pressed()
#  if key_pressed[K_r]:
#     screen.fill(0)
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    pygame.display.update()

阅读更多
版权声明: https://blog.csdn.net/m0_38078987/article/details/80351419
文章标签: Python 打飞机
个人分类: Python
想对作者说点什么? 我来说一句

小甲鱼-飞机大战python源码下载

2017年01月22日 1.54MB 下载

opengl做的打飞机小游戏

2013年04月21日 27.85MB 下载

SurfaceView 打飞机小游戏的实现

2015年08月14日 1.75MB 下载

没有更多推荐了,返回首页

不良信息举报

入门级用Python + tkinter的打飞机/飞机大战小游戏

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭