朝向四面八方的子弹类设计测试程序

"""
   朝向四面八方的子弹类设计测试程序。
   本程序运行后,在屏幕中央会不断地有子弹射出来,
   随着时间的推移,速度不会越来越慢。
"""

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()       
        self.screen = self.getscreen()
        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):
        """不断移动直到边缘"""
        x = self.xcor() + 10*self.direction[0]
        y = self.ycor() + 10*self.direction[1]
        self.goto(x,y)
            
    def on_edge(self):
        """返回是否到边缘了"""
        c = abs(self.xcor()) > self.sw//2 or abs(self.ycor()) > self.sh//2
        return c    

You must log in to read the rest of this article. Please log in or register as a user.