python飞机大战资料-Python之游戏开发-飞机大战
Python之游戏开发-飞机大战
想要代码文件,可以加我微信:nickchen121
#!/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)
python飞机大战资料-Python之游戏开发-飞机大战相关推荐
- 《欢乐坦克大战》微信小游戏开发总结
<欢乐坦克大战>微信小游戏开发总结 <欢乐坦克大战>微信小游戏开发总结 前言 <欢乐坦克大战>是一款支持3V3实时对战并首批参与上线的微信小游戏中的作品.因为该游戏 ...
- Java窗体小游戏开发飞机大作战Java小游戏开发源码
Java窗体小游戏开发飞机大作战Java小游戏开发源码
- 【游戏程序设计】完整二维游戏开发-飞机大战
学习了前面的许多知识,现在可以真正做个可以玩的游戏了. 之前是用MFC做的飞机大战,但是有许多的问题.这次我们用Win32程序来开发,所有的代码都是自己写成的,可以控制更多的细节.这次的游戏就没有之前 ...
- python scratch unity_Unity3D研究院之2D游戏开发制作原理(二十一)
经过了4个月不懈的努力,我和图灵教育合作的这本3D游戏开发书预计下个月就要出版了.这里MOMO先打一下广告,图灵的出版社编辑成员都非常给力,尤其是编辑小花为这本书付出了很大的努力,还有杨海玲老师,不然 ...
- Swing游戏开发——飞机大战
本章讲解利用javax.swing包下的Swing技术来开发一个飞机大战. 完整源码:https://download.csdn.net/download/JavaFanHuman/12713192 ...
- Python实现80后童年经典游戏:坦克大战
FC红白机上的"经典90坦克大战"是不少80后童年的集体回忆(暴露年龄了).今天我们就分享一个用Python制作的仿"坦克大战"小游戏. 废话不多说,让我们愉快 ...
- python学习教程,猜数字游戏开发
猜数字(又称 Bulls and Cows )是一种古老的的密码破译类益智类小游戏,起源于20世纪中期,一般由两个人或多人玩,也可以由一个人和电脑玩. 源码展示: ------------------ ...
- python基础知识资料-Python基础知识快速学习系列视频课程
Python 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年. Python是纯粹的自由软件, 源代码和解释器C ...
- python基础知识资料-Python基础知识(一)—简介
一.Python 简介 Python定义:是一个免费.开源.跨平台.动态.面向对象的编程语言. Python程序的执行(运行)方式有两种:交互式.文件式 交互式 在命令行输入指令,回城即可得到结果. ...
最新文章
- SqlAlchemy个人学习笔记完整汇总
- sql语句分别按日,按周,按月,按季统计金额
- [转]Listview的onItemClickListener无法响应的解决方法
- 【渝粤教育】国家开放大学2018年秋季 0553-21T色彩 参考试题
- java编程中的di是什么_java-在Spring IoC / DI中使用@Component注释对接口...
- 【java学习之路】(java框架)002.Git配置及使用
- python下pymysql的问题
- EF架构~对AutoMapper实体映射的扩展
- Linux基础知识总结一
- 变速恒频风电机组的优缺点_变速恒频风电机组控制系统可靠性分析
- BIOS里的 CSM 是什么意思?
- 【HCIE备考】笔试题库P1-10
- 人与自然超越彩虹-下
- 淘宝 模拟 登录 总结 【QQ 346767073 】
- wikioi 1219 骑士游历
- 牛逼!腾讯竟然打响了反对996的第一枪....
- 你炒的肉丝为何又柴又老又难吃?
- C# Serializable [转]
- 5款国产ARM芯片(对标stm32f103c8t6)测试评估
- Fabric.js 图形标注
热门文章
- SAP Analytics Cloud里取出SAP Cloud for Customer的Reports列表
- 十年前的网易,新浪,维基百科,百度百科在手机上的打开效果
- 502 Bad Gateway - Registered endpoint failed to handle the request
- time Interval in SAP UI5 SalesPipeline
- How does framework require TechnicalInfo.js
- Netweaver 服务器和客户端TLS版本号不匹配的解决方案
- 从ABAP Netweaver的SICF到SAP Kyma的Lambda Function
- 往Cloud Foundry上部署应用背后的技术实现
- 获得ABAP report里定义的所有变量及type - GET_GLOBAL_SYMBOLS
- SAP Cloud for Customer(C4C)的一些学习资料(持续更新)