用python实现弹球小游戏

作者:无水先生 时间:2023-10-25 06:57:31 

一、弹球游戏代码 

下文是tkinter的应用实例,实现弹球游戏,通过<--和-->件移动平板接球。

from tkinter import *
import random
import time

# Creating the window:
window = Tk()
window.title("Bounce")
window.geometry('600x600')
window.resizable(False, False)

# Creating the canvas containing the game:
canvas = Canvas(window, width = 450, height = 450, bg = "black")
canvas.pack(padx = 50, pady= 50)
score = canvas.create_text(10, 20, fill = "white")

window.update()

class Ball:
   def __init__(self, canvas1, paddle1, color):
       self.canvas = canvas1
       self.paddle = paddle1
       self.id = canvas1.create_oval(10, 10, 25, 25, fill = color)  # The starting point of the ball
       self.canvas.move(self.id, 190, 160)
       starting_direction = [-3, -2, -1, 0, 1, 2, 3]
       random.shuffle(starting_direction)
       self.x = starting_direction[0]
       self.y = -3
       self.canvas_height = self.canvas.winfo_height()
       self.canvas_width = self.canvas.winfo_width()

# Detecting the collision between the ball and the paddle:
   def hit_paddle(self, ballcoords):
       paddle_pos = self.canvas.coords(self.paddle.id)
       if ballcoords[0] <= paddle_pos[2] and ballcoords[2] >= paddle_pos[0]:
           if paddle_pos[3] >= ballcoords[3] >= paddle_pos[1]:
               return True
       return False

# Detecting the collision between the the ball and the canvas sides:
   def draw(self):
       self.canvas.move(self.id, self.x, self.y)
       ballcoords = self.canvas.coords(self.id)
       if ballcoords[1] <= 0:
           self.y = 3
       if ballcoords[3] >= self.canvas_height:
           self.y = 0
           self.x = 0
           self.canvas.create_text(225, 150, text = "Game Over!", font = ("Arial", 16), fill = "white")
       if ballcoords[0] <= 0:
           self.x = 3
       if ballcoords[2] >= self.canvas_width:
           self.x = -3
       if self.hit_paddle(ballcoords):
           self.y = -3

class Paddle:
   def __init__(self, canvas1, color):
       self.canvas1 = canvas
       self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color)
       self.canvas1.move(self.id, 180, 350)
       self.x = 0
       self.y = 0
       self.canvas1_width = canvas1.winfo_width()
       self.canvas1.bind_all("<Left>", self.left)
       self.canvas1.bind_all("<Right>", self.right)

def draw(self):
       self.canvas1.move(self.id, self.x, 0)
       paddlecoords = self.canvas1.coords(self.id)
       if paddlecoords[0] <= 0:
           self.x = 0
       if paddlecoords[2] >= self.canvas1_width:
           self.x = 0

def right(self, event):
       self.x = 3

def left(self, event):
       self.x = -3

paddle = Paddle(canvas, color = "white")
ball = Ball(canvas, paddle, color = "red")

# New code after here
def handler():
   global run
   run = False

window.protocol("WM_DELETE_WINDOW", handler)
run = True

while run:
   # New code before here
   ball.draw()
   paddle.draw()
   window.update_idletasks()
   window.update()
   time.sleep(0.01)

window.destroy()    # should always destroy window before exit

二、程序结果 

用python实现弹球小游戏

来源:https://blog.csdn.net/gongdiwudu/article/details/122530718

标签:Python,弹球
0
投稿

猜你喜欢

  • 天极网页版式设计的思考

    2008-01-18 12:44:00
  • python实现八大排序算法(2)

    2023-09-05 06:28:23
  • vue+moment实现倒计时效果

    2024-05-09 10:43:55
  • Python Selenium安装及环境配置的实现

    2023-01-02 18:19:18
  • 什么是XSLT,什么是XPath

    2008-01-21 13:12:00
  • python使用matplotlib绘制折线图

    2021-08-23 05:12:53
  • Python pip安装第三方库实现过程解析

    2022-01-04 02:17:31
  • 在matplotlib的图中设置中文标签的方法

    2023-10-10 07:17:53
  • 在Python程序中进行文件读取和写入操作的教程

    2023-05-22 10:31:56
  • javascript 数组去重复(在线去重工具)

    2024-04-16 09:14:51
  • python seaborn heatmap可视化相关性矩阵实例

    2022-02-08 13:12:30
  • Mysql 数据库双机热备的配置方法

    2010-06-09 19:13:00
  • Python函数关键字参数及用法详解

    2023-08-13 00:34:06
  • vuejs实现ready函数加载完之后执行某个函数的方法

    2024-05-29 22:48:43
  • Python requests模块cookie实例解析

    2023-11-18 15:44:56
  • 简单了解Python3 bytes和str类型的区别和联系

    2023-11-19 21:52:29
  • ASP新闻分页,将一篇过长的文章分页,生成静态页面

    2011-04-10 11:14:00
  • Python画柱状统计图操作示例【基于matplotlib库】

    2021-04-06 21:53:50
  • Python中def()函数的实战练习题

    2023-02-13 02:04:54
  • 关于JavaScript中的this指向问题总结篇

    2024-04-29 13:21:25
  • asp之家 网络编程 m.aspxhome.com