Python之游戏开发-飞机大战

#!/usr/bin/env python
# coding: utf-8

import pygame
import time
import random
from pygame.locals import *

class Base(object):
    def __init__(self, x, y, imageName):
        self.x = x
        self.y = y
        self.imageName = imageName
        self.image = pygame.image.load(self.imageName).convert()

    def display(self):
        screen.blit(self.image, (self.x, self.y))

class Plane(Base):
    def __init__(self, screen, x, y, imageName, planeName):
        Base.__init__(self, x, y, imageName)
        self.screen = screen
        self.bulletList = []
        self.planeName = planeName

    def display(self):
        Base.display(self)

class EnemyPlane(Plane):
    def __init__(self, screen):
        Plane.__init__(self, screen, 0, 0, "./feiji/enemy-3.gif", "Enemy")
        self.directioin = "right"  # right表示向右  left表示向左
        self.speed = random.randint(1, 5)

    def move(self):
        if self.directioin == "right":
            self.x += self.speed
        elif self.directioin == "left":
            self.x -= self.speed
            # 到达另外一个边界时,需要反转方向
        if self.x > 480:
            self.directioin = "left"
        elif self.x < 0:
            self.directioin = "right"

    def shoot(self):
        shootFlagList = [2, 6]
        shootFlag = random.randint(1, 100)
        if shootFlag in shootFlagList:
            self.bulletList.append(Bullet(self.screen, self.planeName, self.x, self.y))
            # print("x:%d,y:%d"%(self.x, self.y))

    def display(self):
        Plane.display(self)
        # print(self.bulletList)

        for bullett in self.bulletList:
            if bullett.y <= 700:
                bullett.display()
                bullett.move()
                global hero
                # 以中点为心
                if ((bullett.x - hero.x - 40) ** 2 + (
                        bullett.y - hero.y - 40) ** 2) ** 0.5 < 40 and bullett.baozhaflag == 0:
                    bullett.baozhaflag = 1
                    global score
                    if score > 20:
                        score -= 20
                    else:
                        score = 0
                    global flagg
                    flagg = 1
                    print(hero.x, hero.y)
                    imageName = "./feiji/hero_blowup_n3.gif"
                    im = hero.image
                    hero.image = pygame.image.load(imageName).convert()
                    print("END")
            else:
                self.bulletList.remove(bullett)

class playerPlane(Plane):
    def __init__(self, screen):
        Plane.__init__(self, screen, 230, 600, "./feiji/hero.gif", "player")
        self.speed = 20

    def display(self):
        Plane.display(self)
        # print(self.bulletList)

        for bullett in self.bulletList:
            if bullett.y >= 0:
                bullett.display()
                bullett.move()
                global enemy
                if ((enemy.x + 40 - bullett.x) ** 2 + (
                        enemy.y + 80 - bullett.y) ** 2) ** 0.5 < 40 and bullett.baozhaflag == 0:
                    bullett.baozhaflag = 1
                    global escore
                    if escore > 0:
                        escore -= 20
                    global flagge
                    flagge = 1
                    print(enemy.x, enemy.y)
                    imageName = "./feiji/enemy2_down1.gif"
                    im = enemy.image
                    enemy.image = pygame.image.load(imageName).convert()
                    print("END")
            else:
                self.bulletList.remove(bullett)

    def moveRight(self):
        if self.x >= 0 and self.x <= 420:
            self.x += self.speed

    def moveLeft(self):
        if self.x <= 480 and self.x >= 20:
            self.x -= self.speed

    def sheBullet(self):
        bui = Bullet(self.screen, "player", self.x + 40, self.y - 4)
        self.bulletList.append(bui)

# 导弹类
class Bullet(Base):
    def __init__(self, screen, bulletName, x, y):
        self.bulletName = bulletName
        imageName1 = "./feiji/bullet-1.gif"
        self.baozhaflag = 0
        if bulletName == "player":
            imageName1 = "./feiji/bullet-3.gif"

        Base.__init__(self, x, y, imageName1)

    def move(self):
        if self.bulletName == "player":
            self.y -= 2
        else:
            self.y += 2

    def display(self):
        Base.display(self)

if __name__ == ‘__main__‘:
    score = 200
    escore = 100
    # 1.创建一个窗口
    screen = pygame.display.set_mode((480, 700), 0, 32)
    # 2.创建一个图片
    background = pygame.image.load("./feiji/background.png").convert()

    color_red = (255, 0, 0)
    color_green = (0, 255, 0)
    color_blue = (0, 0, 255)
    print(pygame.font.get_fonts())
    pygame.font.init()
    # font = pygame.font.SysFont(None, 48)

    # 使用系统字体
    fontObj3 = pygame.font.SysFont(‘arial‘, 20)

    # 加粗
    fontObj3.set_bold(True)

    # 斜体
    fontObj3.set_italic(True)

    # 文字具有蓝色背景
    textSurfaceObj3 = fontObj3.render("敌方血量:" + str(score), True, color_red, color_blue)
    textRectObj3 = textSurfaceObj3.get_rect()
    textRectObj3.center = (60, 510)

    # 文字具有蓝色背景
    textSurfaceObj2 = fontObj3.render("我方血量:" + str(escore), True, color_red, color_blue)
    textRectObj2 = textSurfaceObj2.get_rect()
    textRectObj2.center = (60, 10)

    # 创建玩家飞机
    # hero = pygame.image.load("./feiji/hero.gif").convert()
    global flagg
    flagg = 0
    flagge = 0
    global hero
    hero = playerPlane(screen)
    global enemy
    enemy = EnemyPlane(screen)
    # 3.图片到背景去
    imm = hero.image
    imme = enemy.image
    while True:
        screen.blit(background, (0, 0))
        textSurfaceObj3 = fontObj3.render("Me   :" + str(score), True, color_red, color_blue)
        textRectObj3 = textSurfaceObj3.get_rect()
        textRectObj3.center = (60, 510)

        # 文字具有蓝色背景
        textSurfaceObj2 = fontObj3.render("Enemy :" + str(escore), True, color_red, color_blue)
        textRectObj2 = textSurfaceObj2.get_rect()
        textRectObj2.center = (60, 10)

        screen.blit(textSurfaceObj3, textRectObj3)
        screen.blit(textSurfaceObj2, textRectObj2)
        # screen.blit(hero, (x, 600))
        # hero.display()
        # 获取事件,比如按键等
        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.moveLeft()

                # 检测按键是否是d或者right
                elif event.key == K_d or event.key == K_RIGHT:
                    print(‘right‘)
                    hero.moveRight()

                # 检测按键是否是空格键
                elif event.key == K_SPACE:
                    print(‘space‘)
                    hero.sheBullet()

                    # 更新需要显示的内容

        hero.display()
        if flagg == 1:
            hero.image = imm
            flagg = 0

        # 让敌机自己移动以及发射子弹
        enemy.move()
        enemy.shoot()
        enemy.display()
        if flagge == 1:
            enemy.image = imme
            flsgge = 0
        pygame.display.update()
        time.sleep(0.01)

  

#!/usr/bin/env python# coding: utf-8import pygame import time import random from pygame.locals import * class Base(object):def __init__(self, x, y, imageName): self.x = x self.y = y self.imageName = imageName self.image = pygame.image.load(self.imageName).convert() def display(self): screen.blit(self.image, (self.x, self.y)) class Plane(Base):def __init__(self, screen, x, y, imageName, planeName): Base.__init__(self, x, y, imageName) self.screen = screen self.bulletList = [] self.planeName = planeName def display(self): Base.display(self) class EnemyPlane(Plane):def __init__(self, screen): Plane.__init__(self, screen, 0, 0, "./feiji/enemy-3.gif", "Enemy") self.directioin = "right"# right表示向右 left表示向左 self.speed = random.randint(1, 5) def move(self):if self.directioin == "right": self.x += self.speed elif self.directioin == "left": self.x -= self.speed # 到达另外一个边界时,需要反转方向if self.x > 480: self.directioin = "left"elif self.x < 0: self.directioin = "right"def shoot(self): shootFlagList = [2, 6] shootFlag = random.randint(1, 100) if shootFlag in shootFlagList: self.bulletList.append(Bullet(self.screen, self.planeName, self.x, self.y)) # print("x:%d,y:%d"%(self.x, self.y))def display(self): Plane.display(self) # print(self.bulletList)for bullett in self.bulletList: if bullett.y <= 700: bullett.display() bullett.move() global hero # 以中点为心if ((bullett.x - hero.x - 40) ** 2 + ( bullett.y - hero.y - 40) ** 2) ** 0.5 < 40and bullett.baozhaflag == 0: bullett.baozhaflag = 1global score if score > 20: score -= 20else: score = 0global flagg flagg = 1 print(hero.x, hero.y) imageName = "./feiji/hero_blowup_n3.gif" im = hero.image hero.image = pygame.image.load(imageName).convert() print("END") else: self.bulletList.remove(bullett) class playerPlane(Plane):def __init__(self, screen): Plane.__init__(self, screen, 230, 600, "./feiji/hero.gif", "player") self.speed = 20def display(self): Plane.display(self) # print(self.bulletList)for bullett in self.bulletList: if bullett.y >= 0: bullett.display() bullett.move() global enemy if ((enemy.x + 40 - bullett.x) ** 2 + ( enemy.y + 80 - bullett.y) ** 2) ** 0.5 < 40and bullett.baozhaflag == 0: bullett.baozhaflag = 1global escore if escore > 0: escore -= 20global flagge flagge = 1 print(enemy.x, enemy.y) imageName = "./feiji/enemy2_down1.gif" im = enemy.image enemy.image = pygame.image.load(imageName).convert() print("END") else: self.bulletList.remove(bullett) def moveRight(self):if self.x >= 0and self.x <= 420: self.x += self.speed def moveLeft(self):if self.x <= 480and self.x >= 20: self.x -= self.speed def sheBullet(self): bui = Bullet(self.screen, "player", self.x + 40, self.y - 4) self.bulletList.append(bui) # 导弹类class Bullet(Base):def __init__(self, screen, bulletName, x, y): self.bulletName = bulletName imageName1 = "./feiji/bullet-1.gif" self.baozhaflag = 0if bulletName == "player": imageName1 = "./feiji/bullet-3.gif" Base.__init__(self, x, y, imageName1) def move(self):if self.bulletName == "player": self.y -= 2else: self.y += 2def display(self): Base.display(self) if __name__ == ‘__main__‘: score = 200 escore = 100# 1.创建一个窗口 screen = pygame.display.set_mode((480, 700), 0, 32) # 2.创建一个图片 background = pygame.image.load("./feiji/background.png").convert() color_red = (255, 0, 0) color_green = (0, 255, 0) color_blue = (0, 0, 255) print(pygame.font.get_fonts()) pygame.font.init() # font = pygame.font.SysFont(None, 48)# 使用系统字体 fontObj3 = pygame.font.SysFont(‘arial‘, 20) # 加粗 fontObj3.set_bold(True) # 斜体 fontObj3.set_italic(True) # 文字具有蓝色背景 textSurfaceObj3 = fontObj3.render("敌方血量:" + str(score), True, color_red, color_blue) textRectObj3 = textSurfaceObj3.get_rect() textRectObj3.center = (60, 510) # 文字具有蓝色背景 textSurfaceObj2 = fontObj3.render("我方血量:" + str(escore), True, color_red, color_blue) textRectObj2 = textSurfaceObj2.get_rect() textRectObj2.center = (60, 10) # 创建玩家飞机# hero = pygame.image.load("./feiji/hero.gif").convert()global flagg flagg = 0 flagge = 0global hero hero = playerPlane(screen) global enemy enemy = EnemyPlane(screen) # 3.图片到背景去 imm = hero.image imme = enemy.image whileTrue: screen.blit(background, (0, 0)) textSurfaceObj3 = fontObj3.render("Me :" + str(score), True, color_red, color_blue) textRectObj3 = textSurfaceObj3.get_rect() textRectObj3.center = (60, 510) # 文字具有蓝色背景 textSurfaceObj2 = fontObj3.render("Enemy :" + str(escore), True, color_red, color_blue) textRectObj2 = textSurfaceObj2.get_rect() textRectObj2.center = (60, 10) screen.blit(textSurfaceObj3, textRectObj3) screen.blit(textSurfaceObj2, textRectObj2) # screen.blit(hero, (x, 600))# hero.display()# 获取事件,比如按键等for event in pygame.event.get(): # 判断是否是点击了退出按钮if event.type == QUIT: print("exit") exit() # 判断是否是按下了键elif event.type == KEYDOWN: # 检测按键是否是a或者leftif event.key == K_a or event.key == K_LEFT: print(‘left‘) hero.moveLeft() # 检测按键是否是d或者rightelif event.key == K_d or event.key == K_RIGHT: print(‘right‘) hero.moveRight() # 检测按键是否是空格键elif event.key == K_SPACE: print(‘space‘) hero.sheBullet() # 更新需要显示的内容 hero.display() if flagg == 1: hero.image = imm flagg = 0# 让敌机自己移动以及发射子弹 enemy.move() enemy.shoot() enemy.display() if flagge == 1: enemy.image = imme flsgge = 0 pygame.display.update() time.sleep(0.01)

原文地址:https://www.cnblogs.com/Lynn123/p/11824501.html

时间: 2024-10-07 11:46:26

Python之游戏开发-飞机大战的相关文章

python之游戏开发-坦克大战

新增功能:      优化:1.如果子弹碰到墙壁,让子弹消失            2.最多可以发射3颗子弹,不能一直发射 1 #导入pygame模块 2 import pygame,time,random 3 SCREEN_WIDTH=700 4 SCREEN_HEIGHT=500 5 BG_COLOR=pygame.Color(0,0,0) 6 TEXT_COLOR=pygame.Color(255,0,0) 7 class MainGame(): 8 window=None 9 my_ta

500行代码,教你用python写个微信飞机大战

这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右手有节奏有韵律的朝着同一个方向来回移动起来! 这是史诗级的发明,是浓墨重彩的一笔,是-- 在一阵抽搐后,我结束了游戏,瞬时觉得一切都索然无味,正在我进入贤者模式时,突然想到,如果我可以让更多人已不同的方式体会到这种美轮美奂的感觉岂不美哉? 所以我打开电脑,创建了一个 plan_game.py-- 先

Coco2d-x android win7 Python 搭建游戏开发环境

1:我用电脑配置 win7 3 核 内存8G 台式机,一直想研究Coco2d 游戏开发,所以经过一周的需找,终于把环境搭建好了 2:我用的版本是该版本,至于搭建android开发环境省略了, 3: 2.2>安装ndk,为了使用c++/c进行android开发 下载android-ndk-r8e,然后在eclipse或adt bundle中配置ndk路径. 4: 5:采用VS 编译环境 我之前用的VS2010 感觉用着不爽,所以改成现在的VS 2012 , 6:一般用python建立项目: 用py

python开发飞机大战

1.使用pygame包,使用Python3.5版本2.达到的效果是: 出现一个窗口,显示一张背景图片,出现一架敌机和一架自己的飞机 敌机在最上面,左右移动,随机发子弹,自己的飞机使用键盘左右键左右移动,使用空格键发子弹 当自己的飞机发出的子弹碰到敌机发出的子弹时,敌机的子弹消失 当自己的飞机发出的子弹碰到敌机时,敌机原地摧毁后,再次出现开始游戏 当敌机发出的子弹碰到自己的飞机时,飞机原地摧毁,并退出游戏 3.主要涉及,飞机运动,子弹检测,子弹碰撞检测等,使用面向对象的思路 import pyga

Swift coreAnimation 加计时器写的游戏《飞机大战》

最近在学习swift的动画和图层,就用现学的东西写了个游戏,基本思想 基本功能都实现了,图片都是在网上找得.希望能帮助大家更好的理解动画和图层. 声明下,我是初学者,代码写的不好.大家应该都能看懂 . 效果图 源代码地址 :  点击打开链接      地址失效了的话 我可以补上 版权声明:本文为博主原创文章,未经博主允许不得转载.

python之游戏开发

http://pygame.org/hifi.html https://pyweek.org/ https://wiki.python.org/moin/PythonGames

cocos2d-x(十一)Lua开发飞机大战-6-加入子弹

接下来我们为飞机加入子弹,首先创建一个BulletLayer: module("BulletLayer",package.seeall) local bulletBatchNode = nil local plane = nil local bulletArray = {} local bulletLayer = nil function create() bulletLayer = CCLayer:create() bulletBatchNode = CCSpriteBatchNod

纯JavaScript开发飞机大战项目

开发工具: HBuilder 编程语言:JavaScript 其他技术:Html + Css 项目截图: 视频: 源代码: 在线观看地址: (暂无) 百度网盘下载地址: 请加QQ群:915627672 Q群内免费领取视频教程和源码,并且站长在线免费答疑. 原地址:http://www.java520tz.com/?id=134 原文地址:https://www.cnblogs.com/skyblue-li/p/11600815.html

python之游戏开发-贪吃蛇

#!/usr/bin/env python# coding=utf-8import pygameimport sysimport random # 全局定义,screen的长和宽SCREEN_X = 600SCREEN_Y = 600 # 蛇类# 点以25为单位class Snake(object): # 定义一个类--Snake,默认使用object类,如果你有更好的话,可以替换它 # 初始化各种需要的属性 [开始时默认向右/身体块x5] def __init__(self): # 定义ini