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

相关文章

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

SpringBoot项目中Maven剔除无用Jar引用的最佳实践

《SpringBoot项目中Maven剔除无用Jar引用的最佳实践》在SpringBoot项目开发中,Maven是最常用的构建工具之一,通过Maven,我们可以轻松地管理项目所需的依赖,而,... 目录1、引言2、Maven 依赖管理的基础概念2.1 什么是 Maven 依赖2.2 Maven 的依赖传递机

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element