Python击球游戏turtle创意编程

Python击球游戏turtle创意编程

"""本程序会自动生成一些小球,单击它们会消失,请设计策略让游戏结束"""

import os,sys
from turtle import *
from random import randint,choice
from time import sleep

class Ball(Turtle):
    clicks = 0
    containers = []
    def __init__(self,image,sound):
        Turtle.__init__(self,visible=False)         # 调用Turtle的初始化方法
        self.shape(image)                           # 设定形状
        self.sound = sound                          # 音效
        self.penup()                                # 抬笔
        self.speed(0)                               # 速度为最快
        self.dead = False                           # 标志死亡的逻辑变量
        self.screen_width = self.screen.window_width()
        self.screen_height = self.screen.window_height()
        x = randint(-100,100)
        y = randint(-100,100)
        self.goto(x,y)                              # 坐标定位
        self.setheading(randint(1,360))             # 随机方向
        self.showturtle()                           # 显示
        self.move()                                 # 调用自己的移动方法
        self.onclick(self.die,1)                    # 启动自己的onclick事件
        Ball.containers.append(self)                # 放入容器

   def move(self):
       """ 让小球移动"""       
       self.fd(2)
       if not self.dead and  abs(self.xcor())>self.screen_width/2 or abs(self.ycor())>self.screen_height/2:
          self.dead = True
          self.hideturtle()
          Ball.containers.remove(self)               # 移除自己
          
       if not self.dead:screen.ontimer(self.move,5)  # 没死则继续移动
       

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