Python 模拟炮弹GUI 界面实现

编程入门 行业动态 更新时间:2024-10-07 19:21:49

Python 模拟<a href=https://www.elefans.com/category/jswz/34/1729510.html style=炮弹GUI 界面实现"/>

Python 模拟炮弹GUI 界面实现

第一次码的完整的python代码

主要目的

熟悉一下python 类,tkinter的使用,pyinstaller实现的对py直接封装成exe 以及水掉python期末的300行大作业

模拟炮弹

这个简陋的程序时用来模拟炮弹的飞行,通过输入炮弹的角度以及速度以使炮弹命中靶子。

功能包括

  1. 自行选择游戏难度(影响靶子的大小)
  2. 可存储的游戏排名(只包括前10名)
  3. 部分可能的非法输入时提示错误

界面截图




链接

emm这里是封装好了的exe

代码

程序的各个部分的注解都标上去了
使用了大量的GUI界面(不然我咋弄个300行来 )以及类
其实代码的核心功能实现非常简单.
以下是核心代码的实现:即从控制台输入相关参数实现炮弹轨迹

from graphics import *
import time
import math
from random import randint
##l为标靶的长度,h为标靶的高度
l=10
h=4
class Projectile:def __init__(self, angle, vel, height):self.xpos = 0.0self.ypos = heighttheta = math.radians(angle)self.xvel = vel * math.cos(theta)##x方向初速度self.yvel = vel * math.sin(theta)##y方向初速度def getX(self):return self.xposdef getY(self):return self.yposdef update(self, time):self.xpos = self.xpos + time * self.xvelyvel1 = self.yvel - time * 9.8self.ypos = self.ypos + time * (self.yvel + yvel1)/2.0self.yvel = yvel1def highest(self):return self.ypos+0.5*9.8*(self.yvel/9.8)**2def getInputs():a = float(input("输入你要发射炮弹的角度 (度数): "))v = float(input("发射炮弹的速度 (米/秒): "))h = float(input("发射台的高度 (米): "))return a,v,hclass ShotTracker:def __init__(self, win, angle, velocity, height):    self.proj = Projectile(angle, velocity, height)self.marker = Circle(Point(0,height), 2)self.marker.setFill("red")self.marker.setOutline("red")self.marker.draw(win)def update(self, dt):self.proj.update(dt)# move the circle to the new projectile locationcenter = self.marker.getCenter()dx = self.proj.getX() - center.getX()dy = self.proj.getY() - center.getY()self.marker.move(dx,dy)def getX(self):return self.proj.getX()def getY(self):""" return the current y coordinate of the shot’s center """return self.proj.getY()def undraw(self):""" undraw the shot """self.marker.undraw()
def main():# 打印出窗口win = GraphWin("Projectile Animation", 960, 480, autoflush=False)win.setCoords(-10, -10, 312, 155)#打印一个随机位置的标靶tag_x=randint(10+l,315)Line(Point(tag_x-l/2,h), Point(tag_x+l/2,h)).draw(win)Line(Point(tag_x-l/2,0), Point(tag_x-l/2,h)).draw(win)Line(Point(tag_x+l/2,0), Point(tag_x+l/2,h)).draw(win)# 打印出坐标轴Line(Point(-10,0), Point(315,0)).draw(win)# 打印坐标轴上的距离for x in range(0, 315, 50):Text(Point(x,-5), str(x)).draw(win)Line(Point(x,0), Point(x,2)).draw(win)n=0flag=0

更多推荐

Python 模拟炮弹GUI 界面实现

本文发布于:2024-03-11 17:35:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1729509.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:炮弹   界面   Python   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!