python星空代码_002.03 Tkinter 星空漫遊范例

这个博客分享了一段Python代码,使用Tkinter库创建了一个动态星空背景的模拟程序。程序通过Canvas画布和定时器更新星球的位置,实现了航天员在星空中前进的效果,可以作为游戏背景。主要涉及Tkinter的Canvas、坐标转换和定时更新等技术。
摘要由CSDN通过智能技术生成

主题: 002.03 Tkinter星空漫游范例

建檔日期: 2019/08/30

更新日期: None

语言: Python 3.7.4, tkinter 8.6

系统: Win10 Ver. 10.0.17763

002.03 Tkinter星空漫游范例

看到书上介绍有关星际大战的游戏, 自己也想来写一个. 第一件事情想到的就是加上一个背景, 而且是动态的星空背景, 顺手写了一个程序, 发上来供大伙参考.

主要内容如下:

1. Tkinter的Canvas画布及画圆部件的使用

2. 星空中三维空间的坐标转换为屏幕的二维空间坐标

3. 定时器定时触发, 宇宙飞船前进, 星球位置及大小的改变

输出画面

mkKvTsKZz6.png!large

程序代码

'''

星际漫游 - 模拟在星空中前近, 大小星球在屏幕中接近及离去, 可作为游戏背景

'''

from tkinter import *

from math import sqrt

import random

def cal(point):

# 计算星空中的坐标在屏幕中投影的坐标

delta = point[0] - here

if delta==0:

return [-1, -1, 1, 0, 0]

x = point[1]/delta # x坐标

y = point[2]/delta # y坐标

r = point[3]/delta # 星球半径

return [x, y, r]

def display_all(widget, star):

# 更新星球的位置及大小

# 远的星球放在画布最底层

star = sorted(star, reverse=False, key=lambda k:k[0]**2+k[1]**2+k[2]**2)

for i in range(stars):

star[i][4]=i+1

for i in range(stars):

position = cal(star[i])

x0 = position[0] - position[2] + width/2

y0 = position[1] - position[2] + height/2

x1 = position[0] + position[2] + width/2

y1 = position[1] + position[2] + height/2

widget.coords(star[i][4], x0, y0, x1, y1)

def process():

# 每一次更新所有的星球位置及大小

global here

here += step

for i in range(stars):

while True:

position = cal(star[i])

#超出屏幕, 改成新星球, 坐标及半径都更新

if (0 <= position[0]+width/2 < width and

0 <= position[1]+height/2 < height and star[i][0]>here):

break

else:

star[i][0] = random.randint(x_far, 2*x_far)+here

star[i][1] = random.randint(-y_far, y_far)

star[i][2] = random.randint(-z_far, z_far)

star[i][3] = random.randint(min_r, max_r)

display_all(canvas, star)

def threaded():

# 每0.1秒更新一次

process()

root.after(100,threaded)

width = 1536 #屏幕寛度

height = 800 #屏幕高度

step = 1 # 前进速度

min_r = 1 # 最小星球半径

max_r = 200 # 最大星球半径

stars = 1000 # 全部星球数量

x_far = 100 # 星空中最大X轴距离

y_far = width/2*x_far # 星空中最大Y轴距离

z_far = height/2*x_far # 星空中最大Z轴离离

here = 0 # 目前航天员X轴位置为0, 屏幕在X轴1的位置

x_center = int(width/2) # 屏幕中心X坐标

y_center = int(height/2)# 屏幕中心Y坐标

star = [[0,0,0,0,0] for i in range(stars)] # 星球资料[x,y,z,r,部件ID]

root = Tk()

canvas = Canvas(root, width=width, height=height, bg='black')

canvas.pack()

for i in range(stars): # 建立所有的星球部件

position = cal(star[i])

x0 = position[0] - position[2] + width/2

y0 = position[1] - position[2] + height/2

x1 = position[0] + position[2] + width/2

y1 = position[1] + position[2] + height/2

star[i][4] = canvas.create_oval(x0, y0, x1, y1, fill='white')

# root.bind('', process)

root.resizable(0, 0) # 设置窗口为固定大小, 不可改变

threaded() # 定时起动航天员前进

root.mainloop()

本作品采用《CC 协议》,转载必须注明作者和本文链接

Jason Yang

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python开发的真实星空显示软件 含真实恒星位置数据3144颗 代码讲解见: https://blog.csdn.net/xiaorang/article/details/106598307 数据格式例: {'long': 0.023278328898474372, 'lat': -0.09961466705757636, 'light': 46, 'const': 66}, {'long': 0.024870941840919196, 'lat': 0.2338062439126301, 'light': 55, 'const': 62}, {'long': 0.028107061526797, 'lat': 1.1204335039257496, 'light': 56, 'const': 18}, {'long': 0.03660100303760025, 'lat': 0.5077259659824991, 'light': 21, 'const': 1}, {'long': 0.04004802831028905, 'lat': 1.0323574005393255, 'light': 23, 'const': 18}, {'long': 0.03944444109507185, 'lat': 0.3178583859888262, 'light': 55, 'const': 62}, {'long': 0.040797071265367454, 'lat': -0.488478858963941, 'light': 54, 'const': 74}, {'long': 0.0410661312228549, 'lat': -0.798444499556106, 'light': 39, 'const': 64}, {'long': 0.043800486202076855, 'lat': 0.1945266317121166, 'light': 55, 'const': 66}, {'long': 0.045036755271142, 'lat': 0.804111967609767, 'light': 50, 'const': 1}, {'long': 0.043785947609407745, 'lat': -1.4350775693910554, 'light': 53, 'const': 58}, {'long': 0.04915283505929031, 'lat': -0.2699684886295715, 'light': 49, 'const': 21}, {'long': 0.050498187206605094, 'lat': -0.4851966800391031, 'light': 54, 'const': 74}, {'long': 0.05119631890740283, 'lat': -0.6131874860342564, 'light': 52, 'const': 74}, {'long': 0.05775584219505068, 'lat': 0.26500400429202875, 'light': 28, 'const': 62}, {'long': 0.05896303407877759, 'lat': 0.7162006931179011, 'light': 57, 'const': 1}, {'long': 0.06371905629046214, 'lat': 0.3526728525507925, 'light': 48, 'const': 62}, {'long': 0.06387905062299246, 'lat': -0.33043929519585447, 'light': 44, 'const': 21}, 代码解说详细的教程见: https://blog.csdn.net/xiaorang/article/details/106598307
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值