欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用python實現(xiàn)彈球小游戲

 更新時間:2022年01月19日 09:55:53   作者:無水先生  
大家好,本篇文章主要講的是用python實現(xiàn)彈球小游戲,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下

一、彈球游戲代碼 

下文是tkinter的應(yīng)用實例,實現(xiàn)彈球游戲,通過<--和-->件移動平板接球。

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

二、程序結(jié)果 

總結(jié)

到此這篇關(guān)于用python實現(xiàn)彈球小游戲的文章就介紹到這了,更多相關(guān)python彈球游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python標(biāo)準(zhǔn)庫06之子進(jìn)程 (subprocess包) 詳解

    Python標(biāo)準(zhǔn)庫06之子進(jìn)程 (subprocess包) 詳解

    本篇文章主要介紹了Python標(biāo)準(zhǔn)庫06之子進(jìn)程 (subprocess包) 詳解,具有一定的參考價值,有興趣的同學(xué)可以了解一下。
    2016-12-12
  • Python全局鎖中如何合理運用多線程(多進(jìn)程)

    Python全局鎖中如何合理運用多線程(多進(jìn)程)

    這篇文章主要介紹了Python全局鎖中如何合理運用多線程(多進(jìn)程),需要的朋友可以參考下
    2019-11-11
  • pytorch實現(xiàn)focal loss的兩種方式小結(jié)

    pytorch實現(xiàn)focal loss的兩種方式小結(jié)

    今天小編就為大家分享一篇pytorch實現(xiàn)focal loss的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python采集代理ip并判斷是否可用和定時更新的方法

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

    今天小編就為大家分享一篇Python采集代理ip并判斷是否可用和定時更新的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python字符串split及rsplit方法原理詳解

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

    這篇文章主要介紹了Python字符串split及rsplit方法原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Matplotlib 折線圖plot()所有用法詳解

    Matplotlib 折線圖plot()所有用法詳解

    這篇文章主要介紹了Matplotlib 折線圖plot()所有用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Python xlrd讀取excel日期類型的2種方法

    Python xlrd讀取excel日期類型的2種方法

    這篇文章主要介紹了Python xlrd讀取excel日期類型的2種方法,本文同時講解了xlrd讀取excel某個單元格的方法,需要的朋友可以參考下
    2015-04-04
  • Python語法學(xué)習(xí)之進(jìn)程間的通信方式

    Python語法學(xué)習(xí)之進(jìn)程間的通信方式

    進(jìn)程在創(chuàng)建之后是沒有辦法獲取返回值的,但有的時候兩個進(jìn)程之間需要進(jìn)行相互之間的配合才能完成工作,這就需要通信的幫助。本文主要介紹了Python中進(jìn)程間的通信方式,需要的可以了解一下
    2022-04-04
  • Python中的wordcloud庫安裝問題及解決方法

    Python中的wordcloud庫安裝問題及解決方法

    這篇文章主要介紹了Python中的wordcloud庫安裝問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • python3 判斷列表是一個空列表的方法

    python3 判斷列表是一個空列表的方法

    今天小編就為大家分享一篇python3 判斷列表是一個空列表的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05

最新評論