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小游戏之 - 飞机大战美女 !
用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...
- Python小游戏之 - 飞机大战 !
用Python写的"飞机大战"小游戏 源代码如下: # coding=utf-8 import random import os import pygame # 用一个常量来存储屏 ...
- 500行代码,教你用python写个微信飞机大战
这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...
- 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)
微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...
- 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)
微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...
- 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)
微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...
- 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)
微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...
- 微信demo小游戏:飞机大战从无到有
微信demo游戏飞机大战从无到有 现在创建新项目会默认给飞机大战的demo,这里给大家从基础开始讲解游戏的从无到有是怎么实现的. 具体实现步骤: 创建背景图->背景图运动起来->创建飞机并 ...
- IOS学习之路五(SpriteKit 开发飞机大战小游戏一)
参考SpriteKit 创建游戏的教程今天自己动手做了一下,现在记录一下自己怎么做的,今天之做了第一步,一共有三个部分. 第一步,项目搭建. 项目所用图片资源:点击打开链接 1.在Xcode打开之后, ...
随机推荐
- P3158 [CQOI2011]放棋子
传送门 题解(因为公式太多懒得自己抄写一遍了--) //minamoto #include<bits/stdc++.h> #define ll long long #define R re ...
- Qt事件系统之二:鼠标事件和滚轮事件
在Qt中,事件作为一个对象,继承自 QEvent 类,常见的有键盘事件 QKeyEvent.鼠标事件 QMouseEvent 和定时器事件 QTimerEvent 等,与 QEvent 类的继承关系图 ...
- ROS学习笔记九:ROS工具
ROS有各种工具可以帮助用户使用ROS.应该指出,这些GUI工具是对输入型命令工具的补充.如果包括ROS用户个人发布的工具,那么ROS工具的数量很庞大.其中,本文讨论的工具是对于ROS编程非常有用的辅 ...
- [PKUWC2018]猎人杀
题解 感觉是一道神题,想不出来 问最后\(1\)号猎人存活的概率 发现根本没法记录状态 每次转移的分母也都不一样 可以考虑这样一件事情: 如果一个人被打中了 那么不急于从所有人中将ta删除,而是给ta ...
- NOIP-2018
时隔一年,再度踏入NOIp的考场,内心感慨万分 Day0 在中巴上昏睡了3h++,终于到了长沙理工大学,国际学术交流中心......不太对,好像是国际交流中心与综合实验楼连线--理工大学的另一个大门外 ...
- _bzoj1014 [JSOI2008]火星人prefix【Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1014 天,写kth()时,把判断条件k <= siz[ch[x][0]]错写成了k & ...
- Latex新人教程
1.LaTeX软件的安装和使用 方法A(自助):在MikTeX的官网下载免费的MikTeX编译包并安装.下载WinEdt(收费)或TexMaker(免费)等编辑界面软件并安装. 方法B(打包):在ct ...
- Spring---AOP注解开发&jdbc模板&Spring事务管理
一.AOP注解开发 此处需要回忆一遍AOP的概念.简单的来说,AOP就是利用动态代理技术,做到不触动源代码但却扩展了功能.那么就需要一个被扩展的对象和一个“新的功能”,例如说给某类的saveUser方 ...
- 简洁大方的wordpress主题,不容错过的主题,附带主题源码下载
cu主题是由疯狂的大叔设计,界面简洁大方是它最大的特点之一. 手残君也比较喜爱这款主题,在使用的过程中,根据手残君的个人习惯,对其进行了优化. 标题优化 标题居中显示 增加标题div背景色 标题div ...
- [Tunny]CSS LESS框架基础
[黄映焜/Tunny,20140711] Less 是一个Css 预编译器,意思指的是它可以扩展Css语言,添加功能如允许变量(variables),混合(mixins),函数(functions) ...