python项目--飞机大战(第一阶段--未加子弹版)

2023-11-21 05:10

本文主要是介绍python项目--飞机大战(第一阶段--未加子弹版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

import tkinter
import time
import random as rd
class plane1():#移动量#px代表图像的锚点,height代表图像的高度def __init__(self, px, py, height, width):self.pl1x = 0self.pl1y = 0self.px = pxself.py = pyself.height = heightself.width = width#定义小飞机的图像self.imgpy1 = tkinter.PhotoImage(file="../img/飞机1.gif")# 小飞机移动def pl1_move(self):window_canvas.move("hero", self.pl1x, self.pl1y)self.pl1x = 0self.pl1y = 0window_canvas.after(100, self.pl1_move)
#飞机2的类
class plane2():#移动量#px代表图像的锚点,height代表图像的高度def __init__(self, px, py, height, width):self.pl2x = 0self.pl2y = 1self.px = pxself.py = pyself.height = heightself.width = width#定义小飞机的图像self.imgpy2 = tkinter.PhotoImage(file="../img/飞机2.gif")#小飞机移动def pl2_move(self):window_canvas.move("feiji2", self.pl2x, self.pl2y)# self.pl2y +=1window_canvas.after(100, self.pl2_move)#飞机3的类
class plane3():#移动量def __init__(self, px, py, height, width):self.pl3x = 0self.pl3y = 1self.px = pxself.py = pyself.height = heightself.width = width#定义小飞机的图像self.imgpy3 = tkinter.PhotoImage(file="../img/飞机3.gif")#小飞机移动def pl3_move(self):window_canvas.move("feiji3", self.pl3x, self.pl3y)# self.pl3y +=1window_canvas.after(100, self.pl3_move)#飞机4的类
class plane4():#移动量def __init__(self, px, py, height, width):self.pl4x = 1self.pl4y = 1self.px = pxself.py = pyself.height = heightself.width = width#定义飞机4的图像self.imgpy4 = tkinter.PhotoImage(file="../img/飞机4.gif")#飞机4移动def pl4_move(self):self.px +=self.pl4xself.py +=self.pl4y#进行碰壁检测if self.px >= 450:self.pl4x = -self.pl4x# print(self.pl4x)elif self.px <= 35:self.pl4x = -self.pl4x# print(self.pl4x)window_canvas.move("feiji4", self.pl4x, self.pl4y)# self.pl4y +=1window_canvas.after(100, self.pl4_move)
#天空类
class sky1():#移动量def __init__(self, px, py, height, width):self.skyx = 0self.skyy = 1self.px = pxself.py = pyself.height = heightself.width = width#定义天空的图像self.imgsky = tkinter.PhotoImage(file="../img/beijing.gif")#天空移动def sky1_move(self):self.py += self.skyyif self.py - 300 >= 600:self.py = -300window_canvas.move('bg', 0, -1197)print("123")else:window_canvas.move("bg", self.skyx, self.skyy)# self.skyy +=1window_canvas.after(100, self.sky1_move)
#天空类
class sky2():#移动量def __init__(self, px, py, height, width):self.sky2x = 0self.sky2y = 1self.px = pxself.py = pyself.height = heightself.width = width#定义天空的图像self.imgsky2 = tkinter.PhotoImage(file="../img/beijing.gif")#天空移动def sky2_move(self):self.py += self.sky2yif self.py - 300 >= 600:self.py = -300window_canvas.move('bg2', 0, -1197)print("345")else:window_canvas.move("bg2", self.sky2x, self.sky2y)# self.sky2y +=1window_canvas.after(100, self.sky2_move)
#子弹类
class Bullet():def __init__(self, px, py, height, width):self.bulletx = 0self.bullety = -1self.px = pxself.py = pyself.height = heightself.width = width#bullet_list.append(self)# self.hero_x = hero_x# self.hero_y = hero_yself.img_bullet = tkinter.PhotoImage(file="../img/zidan.gif")def Bullet_move(self):window_canvas.move("zidan", self.bulletx, self.bullety)# self.pl2y +=1window_canvas.after(100, self.Bullet_move)if __name__ == '__main__':root_window = tkinter.Tk()#设置窗口不可拉伸root_window.resizable(width=False, height=False)#创建画布window_canvas = tkinter.Canvas(root_window,width=480, height=600)window_canvas.pack()root_window.title('飞机大战')#在画布上画一个图片#要注意,photoimage只支持GIF格式的图片#画出背景# bg_img = tkinter.PhotoImage(file="../img/beijing.gif")# window_canvas.create_image(240, 300, anchor=tkinter.CENTER, image=bg_img, tags='bg')#天空实例化def sky_ss():sky_s = sky1(240, 300, 600, 480)window_canvas.create_image(sky_s.px, sky_s.py, anchor=tkinter.CENTER, image=sky_s.imgsky,tags='bg')sky_s.sky1_move()#天空实例化def sky2_ss():sky2_s = sky2(240, -300, 600, 480)window_canvas.create_image(sky2_s.px, sky2_s.py, anchor=tkinter.CENTER, image=sky2_s.imgsky2,tags='bg2')sky2_s.sky2_move()# 画出主飞机hero = plane1(250, 550, 30, 30)# 移动量def set_right(e):hero.pl1x += 5def set_left(e):hero.pl1x -= 5def set_up(e):hero.pl1y -= 5def set_down(e):hero.pl1y += 5#按键绑定root_window.bind('<Key-Left>',set_left)root_window.bind('<Key-Right>',set_right)root_window.bind('<Key-Up>',set_up)root_window.bind('<Key-Down>',set_down)def hero_s():window_canvas.create_image(hero.px, hero.py, anchor=tkinter.CENTER, image=hero.imgpy1,tags='hero')hero.pl1_move()#小飞机实例化def smallplane_s():smallplane = plane2(rd.randint(20,500),-30,20,20)window_canvas.create_image(smallplane.px, smallplane.py, anchor=tkinter.CENTER, image=smallplane.imgpy2, tags='feiji2')smallplane.pl2_move()#大飞机实例化def bigplane_s():bigplane = plane3(rd.randint(20,500), -30, 20, 20)window_canvas.create_image(bigplane.px, bigplane.py, anchor=tkinter.CENTER, image=bigplane.imgpy3,tags='feiji3')bigplane.pl3_move()#奖励飞机实例化def goodplane_s():goodplane = plane4(rd.randint(20,500), -30, 20, 20)window_canvas.create_image(goodplane.px, goodplane.py, anchor=tkinter.CENTER, image=goodplane.imgpy4,tags='feiji4')goodplane.pl4_move()#子弹实例化bullet = Bullet(240, 270, 10, 10)window_canvas.create_image(bullet.px, bullet.py, anchor=tkinter.CENTER, image=bullet.img_bullet,tags='zidan')bullet.Bullet_move()#定义列表#子弹l_bullet = []for i in range(0,30):a = 'bullet'+str(i)b = Bullet(240, 270, 10, 10)l_bullet.append(b)def bullet_s():for i in l_bullet:#time.sleep(1)window_canvas.create_image(i.px, i.py, anchor=tkinter.CENTER, image=bullet.img_bullet,tags='zidan')i.Bullet_move()## def photo():#     ##     # #画蜜蜂#     # bg_img_feiji4 = tkinter.PhotoImage(file="../img/飞机4.gif")#     # window_canvas.create_image(50, 50, anchor=tkinter.CENTER, image=bg_img_feiji4, tags='feiji4')##     #让蜜蜂动起来#     ap_move()#     root_window.mainloop()# photo()#函数调用部分sky2_ss()sky_ss()smallplane_s()bigplane_s()goodplane_s()hero_s()bullet_s()root_window.mainloop()

部分素材图片:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这篇关于python项目--飞机大战(第一阶段--未加子弹版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/400019

相关文章

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下