python实现弹球反弹

Python031

python实现弹球反弹,第1张

from tkinter import *

import time

import random

class Ball:

def init (self,canvas,color):

# print("begin create ball:",type(canvas),color)

self.canvas=canvas

self.id=canvas.create_oval(10,10,25,25,fill=color) #,绘制带颜色和上下坐标的椭圆形oval,保存小球ID

self.canvas.move(self.id,250,100) #用x方向和y方向位移,让小球移动到中心位置(500/2200/2)

starts=[-3,-2,-1,1,2,3]

random.shuffle(starts) #重新洗牌,对随机数重新放置

self.x=starts[0]

self.y=-3

self.canvas_height=self.canvas.winfo_height() #获取画布当前高度

self.canvas_width=self.canvas.winfo_width() #获取画布当前宽度

tk = Tk()

tk.title('弹球游戏')

tk['width']=400

tk['height']=300

tk.resizable(False,False) #窗口大小不能调整,也可以用0替代False

tk.wm_attributes("-topmost",1) #此窗口放到其他所有窗口之前

canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0) #后面两个具名函数 bd high...表示画笔之外无边框

canvas.pack() #调整画布大小

tk.update() #动画初始化

ball = Ball(canvas,'red') #把画布保存到对象变量中,准备在它上面画球

while True:

ball.draw()

#重画图像

tk.update_idletasks()

tk.update()

#等待一会

time.sleep(0.01)

tk.mainloop() # stops mainloop

用python  tkinter模块做一个击打反弹球和球拍的游戏。球在屏幕上飞,玩家需要把它击打回去,只要球落到屏幕底部,游戏就结束。

首先我们做一个在屏幕上到处移动的小球:

#coding:utf-8

from tkinter import *

import random

import time

class Ball:

    #创建一个球类

    def __init__(self, canvas, color):

        self.canvas = canvas

        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)

        #返回刚好划小球的id,create_oval创建一个椭圆

        self.canvas.move(self.id, 245, 100)

        #把椭圆移动到画布

        starts = [-3, -2, -1, 1, 2, 3]

        random.shuffle(starts)

        #随机排列

        self.x = starts[0]

        self.y = -3

        self.canvas_height = self.canvas.winfo_height()

        #获取画布当前高度

        self.canvas_width = self.canvas.winfo_width()

        #获取画布当前宽度

    def draw(self):

        self.canvas.move(self.id, self.x, self.y)

        #让小球水平和垂直移动

        pos = self.canvas.coords(self.id)

        #coords返回画布上画好的x和y坐标

        #判断小球是否撞到画布顶部或者底部,保证小球反弹回去,不消失

        if pos[1] <= 0:

            self.y = 3

        if pos[3] >= self.canvas_height:

            self.y = -3

        if pos[0] <= 0:

            self.x = 3

        if pos[2] >= self.canvas_width:

            self.x = -3

tk = Tk()

tk.title("Game")

tk.resizable(0, 0)

#窗口大小不可调整

tk.wm_attributes("-topmost", 1)

#使画布窗口置于所有窗口之前

canvas = Canvas(tk,width=500, height=400, bd=0, highlightthickness=0)

#bd和highlighttthickness是为了保证画布没有边框

canvas.pack()

tk.update()

#动画初始化

ball = Ball(canvas, 'red')

while 1:

    #画布一出现会马上消失,为了防止画布消失,用tkinter一直重画

    ball.draw()

    tk.update_idletasks()

    tk.update()

    time.sleep(0.01)

一个会动的小球就做好啦

效果图如下,只是它会动<( ̄3 ̄)>!

high = 100

n = 10

high_all = 0 #第n次落地时走过的长度

high_each = 0 #每次落地的高度

def ball_lands(n):

    global high_all, high_each, high

    #小球第一次落地时

    if n == 0:

        high_each = high

        high_all += high_each

        #print('1', high_each, high_all)

        return high_each

    #从小球第n次落地往前推

    high_each = high * (1 / 2) ** n

    print(u'第%d次小球弹起的高度为:%.2f' %(n, high_each))

    high_all += high_each * 2

    #print('2', high_each, high_all)

    ball_lands(n - 1)

ball_lands(n)

print(u'小球落地%d次,共经过%.2f米。' % (n,high_all))