用python實現(xiàn)彈球小游戲
一、彈球游戲代碼
下文是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)文章希望大家以后多多支持腳本之家!
- python使用opencv進行人臉識別
- python實現(xiàn)讀取并顯示圖片的兩種方法
- Python實現(xiàn)彈球小游戲的示例代碼
- 用Python寫一個簡易版彈球游戲
- python實現(xiàn)簡單反彈球游戲
- python 實現(xiàn)彈球游戲的示例代碼
- Python實現(xiàn)彈球小游戲
- 使用python和pygame制作擋板彈球游戲
- python pygame實現(xiàn)擋板彈球游戲
- python運用pygame庫實現(xiàn)雙人彈球小游戲
- python3實現(xiàn)彈彈球小游戲
- Python基于Tkinter模塊實現(xiàn)的彈球小游戲
- python編寫彈球游戲的實現(xiàn)代碼
- Python實現(xiàn)的彈球小游戲示例
- Python彈球小游戲的項目代碼
相關(guān)文章
python結(jié)合shell自動創(chuàng)建kafka的連接器實戰(zhàn)教程
這篇文章主要介紹了python結(jié)合shell自動創(chuàng)建kafka的連接器,需要安裝連接oracle的python包,獲取oracle表信息,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-04-04
Python基于火山引擎豆包大模型搭建QQ機器人詳細教程(2024年最新)
這篇文章主要介紹了Python基于火山引擎豆包大模型搭建QQ機器人詳細的相關(guān)資料,包括開通模型、配置APIKEY鑒權(quán)和SDK安裝等步驟,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2025-01-01
對python的unittest架構(gòu)公共參數(shù)token提取方法詳解
今天小編就為大家分享一篇對python的unittest架構(gòu)公共參數(shù)token提取方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python 判斷txt每行內(nèi)容中是否包含子串并重新寫入保存的實例
這篇文章主要介紹了python 判斷txt每行內(nèi)容中是否包含子串并重新寫入保存的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python PEP8代碼規(guī)范常見問題以及解決方案
PEP8是Python的代碼規(guī)范,本文總結(jié)了常見的PEP8代碼規(guī)范問題及解決方法,幫助開發(fā)者編寫規(guī)范的代碼2024-11-11

