用海龟画图模块模拟的星球大爆炸景像。下面是部分代码预览:
"""
星球大爆炸.py
这是用海龟画图模块制作的一个八大行星动画。不过,有一天闯入
了一个流浪星球,它不受太阳引力,莽撞地随机移动,碰到行星就把
它吞噬掉。为了拯救太阳系,你需要尽快用鼠标指针单击它,然后它就会
爆炸,否则如果太阳系只剩下三颗行星的活,整个恒星系将会发生大爆炸。
"""
import math
import pygame
from turtle import *
from random import randint,choice
class Demon(Turtle):
def __init__(self,images,pos,bomb):
Turtle.__init__(self, shape='d.gif',visible=False)
self.bomb = bomb
self.screen_width = self.screen.window_width()
self.screen_height = self.screen.window_height()
def move(self):
if not self.dead:
pass
def explode(self,x,y):
"""单击后它会爆炸"""
print("单击中了")
self.bomb.play()
self.dead = True
self.pu()
self.ht()
self.sety(self.ycor()+60)
self.animate()
def animate(self):
"""切换爆炸帧图"""
self.shape(self.images[self.count])
def set_speed(self):
self.xspeed = choice([-9,-8,-5,-2,2,4,7,8,9])/5
self.yspeed = choice([-9,-8,-7,-4,-2,2,4,7,8,9])/5
class Planet(Turtle):
pass
class Star(Turtle):
pass
def main():
pygame.init()
pygame.mixer.init()
bomb = pygame.mixer.Sound('sound/bomb.wav')
pygame.mixer.music.load('sound/backsound.mp3')
pygame.mixer.music.play()
screen = Screen()
screen.setup(1350,780)
screen.bgpic('bg.png')
screen.delay(0)
screen.title("turtle星球大爆炸_作者:李兴球")
images = [] # 漫游者爆炸造型表
for i in range(1,56):
path = "explosion/" + str(i)+'.gif'
screen.addshape(path)
images.append(path)
sunimages=[] # 太阳造型表
for i in range(27):
path = 'sun/' + str(i) + '.gif'
screen.addshape(path)
sunimages.append(path)
e_images=[] # 太阳爆炸造型表
for i in range(39):
path = 'end/' + str(i) + '.png'
e_images.append(path)
screen.addshape('d.gif') # 邪恶星球的造型
d = Demon(images,(-500,0),bomb)
planets=['m.gif','v.gif','e.gif','mars.gif','j.gif','s.gif','u.gif','n.gif']
pos=[(0,80),(0,100),(0,130),(0,160),(0,200),(0,270),(0,320),(0,380)]
period=[87.7,-224.7,365,687,4332.6,29.5*365,-84*365,164.8*365]
ps = [] # 行星表
for i in range(8):
screen.addshape(planets[i])
ps.append(Planet(planets[i],pos[i],period[i],d))
Star(sunimages,e_images,(0,0),ps,d)
screen.mainloop()
if __name__ == '__main__':
main()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)


