python飞机大战简单实现
小游戏飞机大战的简单代码实现:
# 定义敌机类
class Enemy:
def restart(self): # 重置敌机的位置和速度
self.x = random.randint(50, 400)
self.y = random.randint(-200, -50)
self.speed = random.random()*Level_Simpie_enemy
def __init__(self): # 初始化
self.restart()
self.image = pygame.image.load('enemy.png').convert_alpha()
def move(self):
if self.y < 800:
self.y += self.speed
else: # 重置
self.restart()
# 定义飞机类
class Plane:
def restart(self):
self.x = 200
self.y = 300
def __init__(self):
self.restart()
self.image = pygame.image.load('plant.png').convert_alpha()
def move(self):
x, y = pygame.mouse.get_pos()
x -= self.image.get_width() / 2
y -= self.image.get_height() / 2
self.x = x
self.y = y
# 定义Bullet类
class Bullet:
def __init__(self):
self.x = 0
self.y = -1
self.image = pygame.image.load('dian.jpg').convert_alpha()
self.active = False def move(self):
if self.active:
self.y -= Level_Simpie_bullet
if self.y < 0:
self.active = False def restart(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = mouseX - self.image.get_width() / 2
self.y = mouseY - self.image.get_height() / 2
self.active = True
#定义碰撞
def checkHit(enemy, bullet):
if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and \
(bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()):
enemy.restart()
bullet.active = False
return True
return False
def checkCrash(enemy, plane):
if (plane.x + 0.7 * plane.image.get_width() > enemy.x) and (
plane.x + 0.3 * plane.image.get_width() < enemy.x + enemy.image.get_width()) and \
(plane.y + 0.7 * plane.image.get_height() > enemy.y) and (
plane.y + 0.3 * plane.image.get_height() < enemy.y + enemy.image.get_height()):
return True
return False
#总体实现
pygame.init()
screen = pygame.display.set_mode(window_SIZE, 0, 32) # 制作窗口
pygame.display.set_caption('飞机大战') # 窗口名称
background = pygame.image.load('bg.jpg').convert() # 加载背景图
plane = Plane() # 创建飞机对象
enemies = [] # 创建敌机list
for i in range(5):
enemies.append(Enemy())
bullets = [] # 创建子弹list
for i in range(20): # 5发子弹
bullets.append(Bullet())
count_b = len(bullets) # 子弹总数
index_b = 0 # 即将激活的子弹序号
interval_b = 0 # 发射子弹间隔
gameover = False
score = 0
font = pygame.font.Font(None, 32)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if gameover and event.type == pygame.MOUSEBUTTONUP: #鼠标监听
plane.restart()
for e in enemies:
e.restart()
for b in bullets:
b.active = False
score = 0
gameover = False
index_b = 0
interval_b = 0
if event.type == pygame.KEYDOWN: #键盘监听
if event.key == pygame.K_LEFT:
move_x =move_x -10
elif event.key == pygame.K_RIGHT:
move_x =move_x+10
elif event.key == pygame.K_UP:
move_y =move_y+10
elif event.key == pygame.K_DOWN:
move_y = move_y-10
elif event.type == pygame.KEYUP:
move_y=0
move_x=0
screen.blit(background, (0, 0)) # 画背景图像,从最左上角开始即(0,0)
if not gameover:
interval_b -= 2 # !!!发射间隔递减,实际发射间隔受cpu影响
if interval_b < 0: # 间隔小于0时,激发一个子弹
bullets[index_b].restart()
interval_b = 100 # 重置时间间隔
index_b = (index_b + 1) % count_b # 子弹序号周期性递减
for b in bullets: # 绘制激活的子弹
if b.active:
b.move()
screen.blit(b.image, (b.x+move_x, b.y+move_y))
for e in enemies:
if checkHit(e, b):
score += 10
for e in enemies: # 敌机的移动和描述
if checkCrash(e, plane):
gameover = True
e.move()
screen.blit(e.image, (e.x, e.y))
plane.move()
screen.blit(plane.image, (plane.x+move_x, plane.y+move_y)) # 画飞机
text = font.render("Score:%d" % score, 1, (0, 0, 0))
screen.blit(text, (0, 0)) # 在屏幕左上角显示分数
else:
text1 = font.render("Score:%d" % score, 1, (0, 0, 0))
text2 = font.render("click restart", 1, (0, 0, 0))
screen.blit(text1, (190, 400)) # 在屏幕中间显示分数
screen.blit(text2, (170, 420))
pygame.display.update() # 刷新
python飞机大战简单实现的更多相关文章
- Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?
目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...
- python飞机大战
'''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...
- python 飞机大战 实例
飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...
- python飞机大战代码
import pygame from pygame.locals import * from pygame.sprite import Sprite import random import time ...
- 小甲鱼python基础教程飞机大战源码及素材
百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载
- 一、利用Python编写飞机大战游戏-面向对象设计思想
相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...
- Python版飞机大战
前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...
- 500行代码,教你用python写个微信飞机大战
这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...
- python之基础总结(飞机大战)
一.学习python有一段时间了,总体上手还是挺好的,但是有些东西还是和Java存在着一定的区别,这里主要是通过学习,然后自己去编写一个案例.从中学习到的一些东西,这里分享出来,如果存在不正确的地方还 ...
随机推荐
- HDU-1403-Longest Common Substring(后缀数组的高度数组运用)
这题要求两个串中的最长相同子串的长度.高度数组可以求一个串中的最长相同子串的长度.所以想到把两个串连起来,但是这样又会产生一些新的串(第一个串的结尾和第二个串的开头组成的)于是在两个串中间放一个'\0 ...
- textarea 是否换行的问题解决
需求:判断当前textarea是否已经换行(这个换行有2种方式:1.不断输入文字直到超过指定宽度后自动换行:2.按了回车以后进行换行) 单纯的解决第二种换行很简单.网上提供了很多常规的解决方案. De ...
- unittest(9)- 使用ddt给测试用例传参
# 1. http_request.py import requests class HttpRequest: def http_request(self, url, method, data=Non ...
- form组件及cookie和session
多对多关系表的三种创建方式 1.全自动创建 优势:不需要你手动创建第三张表 不足:由于第三张表不是你手动创建的,也就意味着,第三张表字段是固定的无法更改 class Book(models.Model ...
- 2020 将至,Tester 你过得还好么?
"昏天黑地地执行用例.跟踪 bug.与开发和产品争吵.工作被压在产品发布的最后阶段,因而要背负整个团队的压力,在 retro meeting 时承受着疯狂 diss......" ...
- C++走向远洋——34(友元函数,成员函数和一般函数的区别)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:youyuan.cpp * 作者:常轩 * 微信公众号:Worl ...
- 聊聊RabbitMQ那一些事儿之一基础应用
聊聊RabbitMQ那一些事儿之一基础应用 Hi,各位热爱技术的小伙伴您们好,今年的疫情害人啊,真心祝愿您和您的家人大家都平平安安,健健康康.年前到现在一直没有总结点东西,写点东西,不然久了自己感觉自 ...
- 《前端面试加分项目》系列 企业级Vue瀑布流
本文 GitHub github.com/ponkans/F2E 已收录,有一线大厂面试点思维导图,也整理了很多我的文档,欢迎Star和完善,大家面试可以参照考点复习.文末有福利~~ 前言 接水怪又来 ...
- 华为的Java面试题,仅供参考。
IP地址的编码分为哪俩部分? IP地址由两部分组成,网络号和主机号.不过是要和“子网掩码”按位与上之后才能区分哪些是网络位哪些是主机位. 2.用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该 ...
- 攻防世界Mobile5 EasyJNI 安卓逆向CTF
EasyJNI 最近正好在出写JNI,正好看到了一道JNI相关的较为简单明了的CTF,就一时兴起的写了,不得不说逆向工程和正向开发确实是可以互补互相加深的 JNI JNI(Java Native In ...