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

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

用turtle模块也是能开发射击游戏的,这是一个基本的学习程序。
下面是部分代码预览:

"""
   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
        
    def move(self):
        self.fd(10)
        if self.碰到边缘():self.dead = True


    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坐标

def shoot(x,y):
    """发射子弹"""
    b = Bullet(x,y,blue_turtle.heading())

def born_turtle():
    """生成海龟对象"""
    blue= Turtle(shape='turtle') 

if __name__ =="__main__":
    
    blue_turtle = born_turtle()
    子弹们 = []
    screen = Screen()
    screen.setup(480,360)
    screen.delay(0)
    screen.bgcolor("cyan")
    
    screen.cv.bind("<Motion>",follow_mouse) # 画布绑定鼠标移动事件
    screen.onclick(shoot)                   # 单击屏幕,关闭窗口
    screen.mainloop()    

如需要查看完整源代码,请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。