zoukankan      html  css  js  c++  java
  • python3+tkinter实现的弹球小游戏

    弹球小游戏是使用Python3+内置的tkinter实现而成,代码量非常少,特别适合练手

    下载地址:https://www.itprojects.cn/web/material/details.html?id=19

    一、运行效果

    二、完整代码

    下面的代码中,主要封装了2个类,一个是Ball球类,一个是Paddle桨类,整体通过一个while无线循环控制球移动,检查桨的移动,从而实现游戏移动的效果

    完整的代码如下

     1 from tkinter import *
     2 import random
     3 import time
     4 
     5 
     6 class Ball:
     7     def __init__(self, canvas, paddle, color):
     8         self.canvas = canvas
     9         self.paddle = paddle
    10         self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
    11         self.canvas.move(self.id, 245, 100)
    12         startx = [-3, -2, -1, 1, 2, 3]
    13         random.shuffle(startx)
    14         self.x = startx[0]
    15         self.y = -3
    16         self.canvas_height = self.canvas.winfo_height()
    17         self.canvas_width = self.canvas.winfo_width()
    18         self.hit_bottom = False
    19 
    20     def draw(self):
    21         self.canvas.move(self.id, self.x, self.y)
    22         pos = self.canvas.coords(self.id)  # top-left bottom-right
    23         if (pos[1] <= 0 or self.hit_paddle(pos) == True):
    24             self.y = -self.y
    25         if (pos[0] <= 0 or pos[2] >= self.canvas_width):
    26             self.x = -self.x
    27         if (pos[3] >= self.canvas_height):
    28             self.hit_bottom = True
    29 
    30     def hit_paddle(self, pos):
    31         paddle_pos = self.canvas.coords(self.paddle.id)
    32         if (pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]):
    33             if (pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]):
    34                 return True
    35         return False
    36 
    37 
    38 class Paddle:
    39     def __init__(self, canvas, color):
    40         self.canvas = canvas
    41         self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
    42         self.x = 0
    43         self.canvas.move(self.id, 200, 300)
    44         self.canvas_width = self.canvas.winfo_width()
    45         self.canvas.bind_all("<Key-Left>", self.turn_left)
    46         self.canvas.bind_all("<Key-Right>", self.turn_right)
    47         self.canvas.bind("<Button-1>", self.turn)  # 鼠标单击事件
    48         self.canvas.bind("<B1-Motion>", self.turnmove)  # 鼠标拖动事件
    49 
    50     def draw(self):
    51         pos = self.canvas.coords(self.id)
    52         if (pos[0] + self.x >= 0 and pos[2] + self.x <= self.canvas_width):
    53             self.canvas.move(self.id, self.x, 0)
    54         # self.x = 0
    55 
    56     def turn_left(self, event):
    57         self.x = -4
    58 
    59     def turn_right(self, event):
    60         self.x = 4
    61 
    62     def turn(self, event):  # 鼠标单击事件函数
    63         print("clicked at", event.x, event.y)
    64         self.mousex = event.x
    65         self.mousey = event.y
    66 
    67     def turnmove(self, event):  # 鼠标拖动事件函数
    68         # print ("现在为止", event.x, event.y)
    69         self.x = event.x - self.mousex
    70         self.mousex = event.x
    71 
    72 
    73 if __name__ == '__main__':
    74     tk = Tk()
    75     tk.title("弹球小游戏 更多项目实例访问www.itprojects.cn")
    76     tk.resizable(0, 0)  # not resizable
    77     tk.wm_attributes("-topmost", 1)  # at top
    78     canvas = Canvas(tk, width=500, height=500, bd=0, highlightthickness=0)
    79     canvas.pack()
    80     tk.update()
    81     paddle = Paddle(canvas, 'blue')
    82     ball = Ball(canvas, paddle, 'red')
    83     while 1:
    84         if (ball.hit_bottom == False):  # 弹球是否碰到底部
    85             ball.draw()
    86             paddle.draw()
    87             tk.update()
    88             time.sleep(0.01)
    89         else:  # 游戏循环结束
    90             break
  • 相关阅读:
    接口与抽象类的区别
    全排列(按字典序)
    设置mysql数据库的密码
    android中操作SQLite注意事项
    Android: Fragment详解
    android设置组件所占的比例
    九度oj 1482:玛雅人的密码
    ACM模板
    洛谷 P1156 垃圾陷阱
    AtCoder Beginner Contest 187 F
  • 原文地址:https://www.cnblogs.com/dong4716138/p/14517932.html
Copyright © 2011-2022 走看看