美文网首页
Python游戏练习之弹球示例

Python游戏练习之弹球示例

作者: 极客匠 | 来源:发表于2020-01-17 23:34 被阅读0次

    这篇文章主要介绍了python实现的弹球小游戏,由键盘控制底部挡板移动,碰撞小球的游戏功能。

    需求描述:

    1. ball 弹球 颜色:红 ,大小:10,10,25,25, 初始位置:245,100

    2. paddle:挡板 颜色:蓝 大小:宽:100 高:10 初始位置:200,300

    3. 规则:当弹球碰到挡板上方,弹球反弹;当弹球越过挡板到游戏画板底部的时候,游戏结束

    基础知识:

    1. 基于python面向对象理解

    2. tkinter知识掌握

    3. Canvas如何绘制

    代码示例

    弹球ball class:

    class Ball:
        # 初始化操作
        def __init__(self,canvas, paddle,color):
            self.canvas = canvas
            self.paddle = paddle
            #   创建一个圆
            self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
            self.canvas.move(self.id, 245, 100)
            startx = [-3, -2, -1, 1, 2, 3]
            random.shuffle(startx)
            self.x = startx[0]
            self.y = -3
            self.canvas_height = self.canvas.winfo_height()
            self.canvas_width = self.canvas.winfo_width()
            self.hit_bottom = False
        #绘制
        def draw(self):
            self.canvas.move(self.id, self.x, self.y)
            pos = self.canvas.coords(self.id)  #top-left bottom-right
            if (pos[1] <= 0 or self.hit_paddle(pos) == True):
                self.y = -self.y
            if (pos[0] <= 0 or pos[2] >= self.canvas_width):
                self.x = -self.x
            if (pos[3] >= self.canvas_height):
                self.hit_bottom = True
        def hit_paddle(self, pos):
            paddle_pos = self.canvas.coords(self.paddle.id)
            if (pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]):
                if (pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]):
                    return True
            return False
    

    挡板paddle class:

    class Paddle:
     # 初始化
     def __init__(self, canvas, color):
     self.canvas = canvas
     # 画一个矩形
     self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color)
     self.x = 0
     self.canvas.move(self.id, 200, 300)
     self.canvas_width = self.canvas.winfo_width()
     self.canvas.bind_all("<Key-Left>", self.turn_left) # 左按键移动turn_left距离
     self.canvas.bind_all("<Key-Right>", self.turn_right)
     def draw(self):
     pos = self.canvas.coords(self.id)
     if (pos[0] + self.x >= 0 and pos[2] + self.x <= self.canvas_width):
     self.canvas.move(self.id, self.x, 0)
     #self.x = 0
     def turn_left(self, event):
     self.x = -4
     def turn_right(self, event):
     self.x = 4
    

    实现及控制逻辑:

    import tkinter
    import random
    import time
    ​
    ...
    ​
    tk = tkinter.Tk()
    tk.title("Game Ball")
    tk.resizable(0,0)
    tk.wm_attributes("-topmost", 1)
    canvas = tkinter.Canvas(tk,width= 500,height= 500, bd = 0, highlightthickness = 0)
    canvas.pack()
    tk.update()
    paddle = Paddle(canvas,"blue")
    ball = Ball(canvas,paddle,"red")
    while 1:
     if(ball.hit_bottom == False):
     ball.draw()
     paddle.draw()
     tk.update_idletasks()
     tk.update()
     time.sleep(0.01)
    

    总结:本章需要掌握的知识点有python面向对象、tkinter知识、Canvas如何绘制和CanvasAPI函数。

    每天多努力那么一点点,积少成多

    相关文章

      网友评论

          本文标题:Python游戏练习之弹球示例

          本文链接:https://www.haomeiwen.com/subject/slghzctx.html