用python实现弹球小游戏

 更新时间:2022年01月19日 09:55:53   作者:无水先生  
大家好,本篇文章主要讲的是用python实现弹球小游戏,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

一、弹球游戏代码 

下文是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实现弹球小游戏的文章就介绍到这了,更多相关python弹球游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python标准库06之子进程 (subprocess包) 详解

    Python标准库06之子进程 (subprocess包) 详解

    本篇文章主要介绍了Python标准库06之子进程 (subprocess包) 详解,具有一定的参考价值,有兴趣的同学可以了解一下。
    2016-12-12
  • Python全局锁中如何合理运用多线程(多进程)

    Python全局锁中如何合理运用多线程(多进程)

    这篇文章主要介绍了Python全局锁中如何合理运用多线程(多进程),需要的朋友可以参考下
    2019-11-11
  • pytorch实现focal loss的两种方式小结

    pytorch实现focal loss的两种方式小结

    今天小编就为大家分享一篇pytorch实现focal loss的两种方式小结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python采集代理ip并判断是否可用和定时更新的方法

    Python采集代理ip并判断是否可用和定时更新的方法

    今天小编就为大家分享一篇Python采集代理ip并判断是否可用和定时更新的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • Python字符串split及rsplit方法原理详解

    Python字符串split及rsplit方法原理详解

    这篇文章主要介绍了Python字符串split及rsplit方法原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Matplotlib 折线图plot()所有用法详解

    Matplotlib 折线图plot()所有用法详解

    这篇文章主要介绍了Matplotlib 折线图plot()所有用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Python xlrd读取excel日期类型的2种方法

    Python xlrd读取excel日期类型的2种方法

    这篇文章主要介绍了Python xlrd读取excel日期类型的2种方法,本文同时讲解了xlrd读取excel某个单元格的方法,需要的朋友可以参考下
    2015-04-04
  • Python语法学习之进程间的通信方式

    Python语法学习之进程间的通信方式

    进程在创建之后是没有办法获取返回值的,但有的时候两个进程之间需要进行相互之间的配合才能完成工作,这就需要通信的帮助。本文主要介绍了Python中进程间的通信方式,需要的可以了解一下
    2022-04-04
  • Python中的wordcloud库安装问题及解决方法

    Python中的wordcloud库安装问题及解决方法

    这篇文章主要介绍了Python中的wordcloud库安装问题及解决方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • python3 判断列表是一个空列表的方法

    python3 判断列表是一个空列表的方法

    今天小编就为大家分享一篇python3 判断列表是一个空列表的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05

最新评论