伪3D拦彩球小游戏主程序

"""
   伪3D拦彩球小游戏.py
   本程序新建Ball类,球实例化后会在定时器的作用下不断地自我移动与进行碰撞检测。
"""
from glob import glob
from board import Board
from time import sleep
from turtle import Screen,Turtle
from random import randint,choice


class Ball(Turtle):
    def __init__(self,image,board ):
        """image:已注册的gif图,board:拦板对象"""
        Turtle.__init__(self,shape=image,visible=False)
        self.penup()
        self.board = board
        self.xspeed = choice(speeds)
        self.yspeed = choice(speeds)
        self.sw = self.screen.window_width()   # 屏幕宽度属性
        self.sh = self.screen.window_height()  # 屏幕高度属性 
        self.dead = False 
        self.goto(self.sw//2,self.sh//2)
        self.showturtle()
        self.move()
        
    def move(self):
        """移动小球方法"""        
        if self.dead == False:
            x = self.xcor() + self.xspeed        # 新的x坐标是原坐标加xspeed
            y = self.ycor() + self.yspeed        # 新的y坐标是原坐标加yspeed
            self.goto(x,y)
            left = x - 13                        # 小球最左边的x坐标
            right = x + 13                       # 小球最右边的x坐标
            top = y-13                           # 小球最上边的y坐标
            bottom = y + 13                      # 小球最下边的y坐标
            c1 = left <= 0
            c2 = right >= self.sw
            if c1 or c2 :                        # 左或右超过边缘
                self.xspeed = -self.xspeed       # x速度取反
                
            c3 = top <= 0
            c4 = top >= self.sh
            if c3 :                              # 超过上边缘
                self.yspeed = -self.yspeed       # y速度取反
            if c4 :                              # 超过下边缘
                self.dead = True                 # 死了
                self.ht()                        # 隐藏
            if self.is_bumped_board():           # 碰到拦板
                 self.yspeed = -self.yspeed       # y速度取反
            self.screen.ontimer(self.move,10)
        

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