turtle射击游戏基础_python turtle shoot game foundation

"""
   turtle射击游戏基础.py
   通过鼠标指针牵引海龟移动,单击鼠标按键可发射子弹.   

"""

import math
from turtle import *

class Bullet(Turtle):
    def __init__(self,x,y,h):
        Turtle.__init__(self,visible=False,shape="circle")
        self.shapesize(0.5,0.5) 
        self.penup()
        self.dead = False
        self.goto(x,y)
        self.setheading(h)
        self.color("red")
        self.showturtle()
        self.move()
        
    def move(self):
        self.fd(10)
        if self.碰到边缘():self.dead = True
        if self.dead:
            self.hideturtle()
            del self
            #print("我自杀好吧")
        else:
            screen.ontimer(self.move,10)

    def 碰到边缘(self):
        return abs(self.xcor())>240 or abs(self.ycor())>180
        

def follow_mouse(event):
    """本函数让小海龟面朝鼠标指针移动"""
    x = event.x - 240              # 转换成海龟坐标系中的x坐标
    y = 180 - event.y              # 转换成海龟坐标系中的y坐标
    dy = y - blue_turtle.ycor()
    dx = x - blue_turtle.xcor()
    angle = math.degrees(math.atan2(dy,dx))
    blue_turtle.setheading(angle)
    if blue_turtle.distance(x,y)>50:blue_turtle.fd(10)
    s = "坐标:" + str(int(x))
    s = s + "," + str(int(y)) + ",方向:" + str(int(angle))
    screen.title(s)

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