一、前引
#已经学习完了:
#1.数据结构:集合、元组、字典
#2.函数
#3.类和对象
#4.继承与多态
#1.规划编程项目:
#(1)你想做什么什么样功能的项目?
# 接小球游戏,碰到挡板时自动反弹
#(2)功能有哪些?
# 自由运动的小球
# 小球触碰到势必会发生反弹
# 键盘左右移动挡板
# 小球触碰到挡板会发生反弹。
#(3)如何实现它的功能?
# 类Ball:初始化小球,小球自由移动,触边反弹等功能;
# 类Board:初始化挡板,挡板左右移动,触碰挡板反弹;
# 类Game:创建小球,挡板对象,小球一直在空间运动。
# 用伪代码==转化==>实际代码
#(4)你的时间规划?开始时间,结束时间。
# 30分钟,设计项目和伪代码实践
# 40分钟,球移动/碰撞反弹
# 20分钟,挡板移动/碰撞反弹
二、伪代码:创建小球+挡板对象
#伪代码:类似于流程图,用在程序的一开始,用来描写和帮助写出程序。
#------------------------------------------------------
#1.(注释)
# 游戏效果:
# 小球在空中自由的运动,当碰到左右移动的挡板时会发生反弹
# (1)导入模块
# (2)运动的小球
# (3)挡板,可以让小球反弹
# (4)创建小球和挡板对象,运行游戏
#2.伪代码:
# 导入模块
# 运动的小球
# class Ball():
# def __init__(self): #小球基本属性
# pass
#
# def move(self): #小球自由移动
# pass
#
# def checkbound(self): #检测小球触边反弹。
# pass
#
# # 挡板,可以让小球反弹
# class Board():
# def __init__(self): #挡板基本属性
# pass
#
# def go_left(self): #左移
# pass
#
# def go_right(self): #右移
# pass
#
# def collide(self,ball): #挡板与球发生碰撞,接着反弹
# pass
# #游戏运行
# class Game():
# def __init__(self):
# pass
# def run(self):
# pass
#
# game=Game()
#3.预制代码(创建了小球对象和挡板对象)
#turtle.Pen()是一个方法
## 游戏效果:
# 小球在空中自由的运动,当碰到左右移动的挡板时会发生反弹
# 导入模块
import turtle
# 运动的小球
class Ball(turtle.Pen):def __init__(self): #小球基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("circle")def move(self): #小球自由移动passdef checkbound(self): #检测小球触边反弹。pass# 挡板,可以让小球反弹
class Board(turtle.Pen):def __init__(self): #挡板基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("square")#正方形self.shapesize(1,5,1)#设置挡板的长度# stretch_wid:垂直方向的拉伸比例(宽度方向)# stretch_len:水平方向的拉伸比例(长度方向)# outline:形状的轮廓宽度self.goto(0,-200) #挡板的初始位置。def go_left(self): #左移passdef go_right(self): #右移passdef collide(self,ball): #挡板与球发生碰撞,接着反弹pass
#游戏运行
class Game():def __init__(self):turtle.Screen().setup(600,600)self.ball = Ball() #小球对象self.board = Board() #挡板对象passdef run(self):passgame=Game()input()
三、小球墙面反弹
#(4)小球移动效果。
## 游戏效果:
# 小球在空中自由的运动,当碰到左右移动的挡板时会发生反弹
# 导入模块
import turtle
# 运动的小球
class Ball(turtle.Pen):def __init__(self): #小球基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("circle")self.spd=10,5#元组 值(10, 5),赋值给 self.spd-------------------------------------------------1def move(self): #小球自由移动#self.goto(self.xcor()+10, self.ycor()-5)#出错1self.goto(self.xcor()+self.spd[0], self.ycor()+self.spd[1])#上面元祖的第一个、第二个----修改2def checkbound(self): #检测小球触边反弹if self.xcor()>300 or self.xcor()<-300:#-x---------------------------------------------3self.spd=-self.spd[0],self.spd[1]#水平取反,反弹
# if self.xcor()>300 or self.xcor()<-300: #错误修改2
# self.xcor()-10
# self.ycor()+5if self.ycor()>300 or self.ycor()<-300:#yself.spd=self.spd[0],-self.spd[1]
# if self.ycor()>300 or self.ycor()<-300: #错误修改2
# self.xcor()+10
# self.ycor()-5# 挡板,可以让小球反弹
class Board(turtle.Pen):def __init__(self): #挡板基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("square")#正方形self.shapesize(1,5,1)#设置挡板的长度self.goto(0,-200) #挡板的初始位置。def go_left(self): #左移passdef go_right(self): #右移passdef collide(self): #挡板与球发生碰撞,接着反弹pass
#游戏运行
class Game():def __init__(self):turtle.Screen().setup(600,600)self.ball = Ball() #小球对象self.board = Board() #挡板对象def run(self):while True: #球斜着飞(右上角)-----------------------------------4self.ball.move()self.ball.checkbound()game=Game()
game.run()input()
四、小球挡板反弹
(5)小球挡板反弹。
游戏效果:
小球在空中自由的运动,当碰到左右移动的挡板时会发生反弹
# 导入模块
import turtle
# 运动的小球
class Ball(turtle.Pen):def __init__(self): #小球基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("circle")#self.spd=10,5#修改======================================2self.spd=-1,-5def move(self):#self.goto(self.xcor()+10, self.ycor()-5)#出错1self.goto(self.xcor()+self.spd[0], self.ycor()+self.spd[1])#修改1def checkbound(self): #检测小球触边反弹if self.xcor()>300 or self.xcor()<-300:self.spd=-self.spd[0],self.spd[1]
# if self.xcor()>300 or self.xcor()<-300: #错误修改2
# self.xcor()-10
# self.ycor()+5if self.ycor()>300 or self.ycor()<-300:self.spd=self.spd[0],-self.spd[1]
# if self.ycor()>300 or self.ycor()<-300: #错误修改2
# self.xcor()+10
# self.ycor()-5# 挡板,可以让小球反弹
class Board(turtle.Pen):def __init__(self): #挡板基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("square")#正方形self.shapesize(1,5,1)#设置挡板的长度,# stretch_wid, stretch_len, outline,# 像素,每个单位对应 20 个像素,# 因此:挡板的宽度(水平方向)是 5 * 20 = 100 个像素self.goto(0,-200) #挡板的初始位置。def go_left(self): #左移passdef go_right(self): #右移passdef collide(self,ball): #挡板与球发生碰撞,接着反弹====================================================1if (-210<=ball.ycor()<=-190and abs(self.xcor()-ball.xcor())<50): #abs是差的绝对值 挡板的宽度为 100,因此 50 是其一半的宽度ball.spd=ball.spd[0],-ball.spd[1]#0=x,1=y#游戏运行
class Game():def __init__(self):turtle.Screen().setup(600,600)self.ball = Ball() #小球对象self.board = Board() #挡板对象def run(self):while True: #球斜着飞(右上角)self.ball.move()self.ball.checkbound()self.board.collide(self.ball)#调用方法#=========================================2game=Game()
game.run()input()
五、小球挡板移动_小球完整
import turtle# 运动的小球
class Ball(turtle.Pen):def __init__(self): # 小球基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("circle")self.spd = -1, -5def move(self):self.goto(self.xcor() + self.spd[0], self.ycor() + self.spd[1])def checkbound(self): # 检测小球触边反弹if self.xcor() > 300 or self.xcor() < -300:self.spd = -self.spd[0], self.spd[1]if self.ycor() > 300 or self.ycor() < -300:self.spd = self.spd[0], -self.spd[1]# 挡板,可以让小球反弹
class Board(turtle.Pen):def __init__(self): # 挡板基本属性turtle.Pen.__init__(self)self.penup()self.speed(0)self.shape("square") # 正方形self.shapesize(1, 5, 1) # 设置挡板的长度self.goto(0, -200) # 挡板的初始位置。# 事件监听screen = turtle.Screen()screen.onkey(self.go_left, "Left")screen.onkey(self.go_right, "Right")screen.listen()def go_left(self): # 左移x = self.xcor() # 获取当前挡板的x坐标if x > -275: # 限制挡板的左边界self.setx(x - 20) # 向左移动20像素def go_right(self): # 右移x = self.xcor() # 获取当前挡板的x坐标if x < 275: # 限制挡板的右边界self.setx(x + 20) # 向右移动20像素def collide(self, ball): # 挡板与球发生碰撞,接着反弹if (-210 <= ball.ycor() <= -190) and (abs(self.xcor() - ball.xcor()) < 50): # abs是差的绝对值ball.spd = ball.spd[0], -ball.spd[1]# 游戏运行
class Game():def __init__(self):turtle.Screen().setup(600, 600)self.ball = Ball() # 小球对象self.board = Board() # 挡板对象def run(self):while True: # 球斜着飞(右上角)self.ball.move()self.ball.checkbound()self.board.collide(self.ball) # 调用方法game = Game()
game.run()input()