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函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as