本程序设计了一个子弹类。运行后,子弹们会朝向四面八方射击。由于子弹是可重复使用的,所以程序并不会运行得越来越慢。

下面是部分代码预览:
"""
朝向四面八方发射的子弹
"""
from turtle import *
from random import randint,choice
class Bullet(Turtle):
"""
子弹类,继承自Turtle
"""
def __init__(self,image,pos,direction):
"""
初始化方法,image:gif图片,代表子弹图
pos:初始坐标,heading:朝向,1或-1
"""
Turtle.__init__(self,visible=False)
self.penup()
if image in self.screen.getshapes():
self.shape(image)
self.sw = self.screen.window_width()
self.sh = self.screen.window_height()
self.setposition(pos) # 起始坐标
self.direction = direction# 速度向量
self.st() # 显示对象
def move(self):
"""不断移动直到边缘"""
if __name__ == '__main__':
screen = Screen()
screen.delay(0)
image = 'images/bullet.gif'
screen.addshape(image)
d = list(range(-5,6))
d.remove(0)
print(d)
.......
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
