国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python 一步一步教你用pyglet制作漢諾塔游戲

這篇具有很好參考價(jià)值的文章主要介紹了Python 一步一步教你用pyglet制作漢諾塔游戲。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Python 一步一步教你用pyglet制作漢諾塔游戲,Python,python,pyglet,hanoi

目錄

漢諾塔游戲

1. 抓取顏色

2. 繪制圓盤

3. 九層漢塔

4. 繪制塔架

5. 疊加圓盤

6. 游戲框架


?

漢諾塔游戲

漢諾塔(Tower of Hanoi),是一個(gè)源于印度古老傳說的益智玩具。這個(gè)傳說講述了大梵天創(chuàng)造世界的時(shí)候,他做了三根金剛石柱子,并在其中一根柱子上從下往上按照大小順序摞著64片黃金圓盤。大梵天命令婆羅門將這些圓盤從下面開始按大小順序重新擺放在另一根柱子上,并規(guī)定在小圓盤上不能放大圓盤,同時(shí)在三根柱子之間一次只能移動(dòng)一個(gè)圓盤。當(dāng)盤子的數(shù)量增加時(shí),移動(dòng)步驟的數(shù)量會(huì)呈指數(shù)級(jí)增長(zhǎng),圓盤數(shù)為n時(shí),總步驟數(shù)steps為2^n - 1。

n = 64, steps = 2^64 - 1 = 18446744073709551616 ≈ 1.845 x 10^19

漢諾塔問題是一個(gè)遞歸問題,也可以使用非遞歸法來解決,例如使用棧來模擬遞歸過程。這個(gè)問題不僅是一個(gè)數(shù)學(xué)和邏輯問題,也是一個(gè)很好的教學(xué)工具,可以用來教授遞歸、算法和邏輯思考等概念。同時(shí),漢諾塔游戲也具有一定的娛樂性,人們可以通過解決不同規(guī)模的漢諾塔問題來挑戰(zhàn)自己的智力和耐心。

1. 抓取顏色

本篇將展示如何用python pyglet庫(kù)制作這個(gè)小游戲,首先在上圖中抓取出需要用到的顏色RGB值,每種顏色畫一個(gè)矩形塊:

Rectangle(x, y, width, height, color=color)

Python 一步一步教你用pyglet制作漢諾塔游戲,Python,python,pyglet,hanoi

代碼:

import pyglet
 
window = pyglet.window.Window(800, 500, caption='漢諾塔')
pyglet.gl.glClearColor(1, 1, 1, 1)
batch = pyglet.graphics.Batch()

Color = (182,128,18),(25,65,160),(56,170,210),(16,188,78),(20,240,20),(240,240,20),(255,128,20),(240,20,20),(245,60,138)
 
class Rect:
    def __init__(self, x, y, color=(0,0,0), width=180, height=60):
        self.rect = pyglet.shapes.Rectangle(x, y, width, height, color=color, batch=batch)
 
@window.event
def on_draw():
    window.clear()
    batch.draw()

rectangle = [None]*9
for i,color in enumerate(Color):
    rectangle[i] = Rect(110+i//3*200, 120+i%3*100, color)

pyglet.app.run()

2. 繪制圓盤

圓盤用矩形加2個(gè)半圓表示,半圓用扇形控件繪制:

Sector(x,?y, radius=R, angle=pi, start_angle=-pi/2, color=color)

注意圓盤類中的矩形的寬度和坐標(biāo)需要調(diào)整,整個(gè)圓盤類的寬度是矩形寬度加2倍扇形半徑,圓盤的中心是矩形的中心而不是矩形左下角。

Python 一步一步教你用pyglet制作漢諾塔游戲,Python,python,pyglet,hanoi

import pyglet
 
window = pyglet.window.Window(800, 500, caption='漢諾塔')
pyglet.gl.glClearColor(1, 1, 1, 1)
batch = pyglet.graphics.Batch()

pi = 3.141592653589793
Color = (182,128,18),(25,65,160),(56,170,210),(16,188,78),(20,240,20),(240,240,20),(255,128,20),(240,20,20),(245,60,138)
 
class Bead:
    def __init__(self, x, y, width=180, height=60):
        self.sec1 = pyglet.shapes.Sector(x+width/2-height/2, y, radius=height/2, angle=pi, start_angle=-pi/2, color=Color[5], batch=batch)
        self.sec2 = pyglet.shapes.Sector(x-width/2+height/2, y, radius=height/2, angle=pi, start_angle=pi/2, color=Color[5], batch=batch)
        self.rect = pyglet.shapes.Rectangle(x-width/2+height/2, y-height/2, width-height, height, color=Color[1], batch=batch)
 
@window.event
def on_draw():
    window.clear()
    batch.draw()
 
ead1 = Bead(window.width/2, window.height/2)

pyglet.app.run()

3. 九層漢塔

疊加多個(gè)圓盤,繪制出漢諾塔的樣子:

Python 一步一步教你用pyglet制作漢諾塔游戲,Python,python,pyglet,hanoi

代碼:

import pyglet
 
window = pyglet.window.Window(800, 500, caption='漢諾塔')
pyglet.gl.glClearColor(1, 1, 1, 1)
batch = pyglet.graphics.Batch()

pi = 3.141592653589793
Color = (182,128,18),(25,65,160),(56,170,210),(16,188,78),(20,240,20),(240,240,20),(255,128,20),(240,20,20),(245,60,138)
 
class Disk:
    def __init__(self, x, y, color=(0,0,0), width=200, height=20):
        self.sec1 = pyglet.shapes.Sector(x+width/2-height/2, y, radius=height/2, angle=pi, start_angle=-pi/2, color=color, batch=batch)
        self.sec2 = pyglet.shapes.Sector(x-width/2+height/2, y, radius=height/2, angle=pi, start_angle=pi/2, color=color, batch=batch)
        self.rect = pyglet.shapes.Rectangle(x-width/2+height/2, y-height/2, width-height, height, color=color, batch=batch)
        assert(width>height and x-width/2+height/2>0)
 
@window.event
def on_draw():
    window.clear()
    batch.draw()

x, y = window.width/2, window.height/2
width, height = 200, 40
disk = []
for i in range(9):
    disk.append(Disk(x, y+height*(i-4), Color[i], width=width-20*(i-1), height=height))

pyglet.app.run()

4. 繪制塔架

把圓盤變簿(高度換成厚度),再加一條粗直線(直線的寬度等于圓盤的厚度)表示出“豎桿”,就畫出疊放的架子來:

? ? ? ? self.pole = pyglet.shapes.Line(x, y, x, y+height, width=thickness, color=color)
? ? ? ? self.disk = Disk(x, y, color=color, width=width, height=thickness)

Python 一步一步教你用pyglet制作漢諾塔游戲,Python,python,pyglet,hanoi

代碼:

import pyglet
 
window = pyglet.window.Window(800, 500, caption='漢諾塔')
pyglet.gl.glClearColor(1, 1, 1, 1)
batch = pyglet.graphics.Batch()

pi = 3.141592653589793
Color = (182,128,18),(25,65,160),(56,170,210),(16,188,78),(20,240,20),(240,240,20),(255,128,20),(240,20,20),(245,60,138)

class Disk:
    def __init__(self, x, y, color=(0,0,0), width=200, height=20):
        self.sec1 = pyglet.shapes.Sector(x+width/2-height/2, y, radius=height/2, angle=pi, start_angle=-pi/2, color=color, batch=batch)
        self.sec2 = pyglet.shapes.Sector(x-width/2+height/2, y, radius=height/2, angle=pi, start_angle=pi/2, color=color, batch=batch)
        self.rect = pyglet.shapes.Rectangle(x-width/2+height/2, y-height/2, width-height, height, color=color, batch=batch)
        assert(width>height and x-width/2+height/2>0)

class Hann:
    def __init__(self, x, y, color=(0,0,0), width=220, height=300, thickness=20):
        self.pole = pyglet.shapes.Line(x, y, x, y+height, width=thickness, color=color, batch=batch)
        self.disk = Disk(x, y, color=color, width=width, height=thickness)

@window.event
def on_draw():
    window.clear()
    batch.draw()

pole1 = Hann(window.width/2-250, 100)
pole2 = Hann(window.width/2, 100, color=Color[0])
pole3 = Hann(window.width/2+250, 100, color=Color[1])

pyglet.app.run()

5. 疊加圓盤

把多個(gè)圓盤疊加磊在塔架上,圓盤數(shù)至少為2。 注意Color顏色列表共有9種顏色,Color[i%8+1]只取后8種顏色,Color[0]僅用于塔架的涂色。

Hann類中各控件的坐標(biāo)計(jì)算有點(diǎn)復(fù)雜,以下方案可以解決問題但未必是最佳方案:

? ? ? ? self.x, self.y = x, y
? ? ? ? self.width = width
? ? ? ? self.height = (height-thickness*2)/order
? ? ? ? self.step = (width-thickness)/(order+1)
? ? ? ? self.beads = []
? ? ? ? self.coordinates = []
? ? ? ? for i in range(order):
? ? ? ? ? ? self.coordinates.append([self.x, self.y+(i+1)*self.height-(self.height-thickness)/2])

Python 一步一步教你用pyglet制作漢諾塔游戲,Python,python,pyglet,hanoi

?代碼:

import pyglet
 
window = pyglet.window.Window(800, 500, caption='漢諾塔')
pyglet.gl.glClearColor(1, 1, 1, 1)
batch = pyglet.graphics.Batch()

pi = 3.141592653589793
Color = (182,128,18),(25,65,160),(56,170,210),(16,188,78),(20,240,20),(240,240,20),(255,128,20),(240,20,20),(245,60,138)

class Disk:
    def __init__(self, x, y, color=(0,0,0), width=200, height=20):
        self.sec1 = pyglet.shapes.Sector(x+width/2-height/2, y, radius=height/2, angle=pi, start_angle=-pi/2, color=color, batch=batch)
        self.sec2 = pyglet.shapes.Sector(x-width/2+height/2, y, radius=height/2, angle=pi, start_angle=pi/2, color=color, batch=batch)
        self.rect = pyglet.shapes.Rectangle(x-width/2+height/2, y-height/2, width-height, height, color=color, batch=batch)
        assert(width>height and x-width/2+height/2>0)

class Hann:
    def __init__(self, x, y, order=2, thickness=20, width=220, height=300):
        assert(order>1)
        self.pole = pyglet.shapes.Line(x, y, x, y+height, width=thickness, color=Color[0], batch=batch)
        self.disk = Disk(x, y, color=Color[0], width=width+thickness, height=thickness)
        self.x, self.y = x, y
        self.width = width
        self.height = (height-thickness*2)/order
        self.step = (width-thickness)/(order+1)
        self.beads = []
        self.coordinates = []
        for i in range(order):
            self.coordinates.append([self.x, self.y+(i+1)*self.height-(self.height-thickness)/2])
        self.fillup()
    def fillup(self):
        for i,xy in enumerate(self.coordinates):
            self.beads.append(Disk(*xy, Color[i%8+1], width=self.width-i*self.step, height=self.height))
        
@window.event
def on_draw():
    window.clear()
    batch.draw()

hann1 = Hann(window.width/2-260, 100, 2)
hann2 = Hann(window.width/2-22, 180, 5, 25)
hann3 = Hann(window.width/2+230, 80, 10, 15, 300, 380)

pyglet.app.run()

6. 游戲框架

畫三個(gè)相同的塔架,左邊的磊放好圓盤。另外用兩個(gè)圓代替兩個(gè)扇形,效果一樣卻省了pi常量。

Circle(x+width/2-height/2, y, radius=height/2, color=color)

Python 一步一步教你用pyglet制作漢諾塔游戲,Python,python,pyglet,hanoi

代碼:?

import pyglet
 
window = pyglet.window.Window(800, 500, caption='漢諾塔')
pyglet.gl.glClearColor(1, 1, 1, 1)
batch = pyglet.graphics.Batch()

Color = (182,128,18),(25,65,160),(56,170,210),(16,188,78),(20,240,20),(240,240,20),(255,128,20),(240,20,20),(245,60,138)

class Disk:
    def __init__(self, x, y, color=(0,0,0), width=200, height=20):
        self.cir1 = pyglet.shapes.Circle(x+width/2-height/2, y, radius=height/2, color=color, batch=batch)
        self.cir2 = pyglet.shapes.Circle(x-width/2+height/2, y, radius=height/2, color=color, batch=batch)
        self.rect = pyglet.shapes.Rectangle(x-width/2+height/2, y-height/2, width-height, height, color=color, batch=batch)
        assert(width>height and x-width/2+height/2>0)

class Hann:
    def __init__(self, x, y, order=2, thickness=20, width=200, height=300):
        assert(order>1)
        self.pole = pyglet.shapes.Line(x, y, x, y+height, width=thickness, color=Color[0], batch=batch)
        self.disk = Disk(x, y, color=Color[0], width=width+thickness, height=thickness)
        self.x, self.y = x, y
        self.width = width
        self.height = (height-thickness*2)/order
        self.step = (width-thickness)/(order+1)
        self.beads = []
        self.coordinates = []
        for i in range(order):
            self.coordinates.append([self.x, self.y+(i+1)*self.height-(self.height-thickness)/2])
    def fillup(self):
        for i,xy in enumerate(self.coordinates):
            self.beads.append(Disk(*xy, Color[i%8+1], width=self.width-i*self.step, height=self.height))

class Game:
    def __init__(self, x, y, order=2, space=250):
        self.x, self.y = x, y
        self.space = space
        self.order = order
        self.hanns = Hann(x-space, y, order), Hann(x, y, order), Hann(x+space, y, order)
        self.hanns[0].fillup()

@window.event
def on_draw():
    window.clear()
    batch.draw()

hann = Game(window.width/2, 100, 8)

pyglet.app.run()

接下來就要添加鼠標(biāo)和鍵盤事件,用于操作在塔架上移動(dòng)圓盤。本篇完,下期繼續(xù)......文章來源地址http://www.zghlxwxcb.cn/news/detail-838906.html

到了這里,關(guān)于Python 一步一步教你用pyglet制作漢諾塔游戲的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Python 一步一步教你用pyglet制作可播放音樂的揚(yáng)聲器類

    Python 一步一步教你用pyglet制作可播放音樂的揚(yáng)聲器類

    目錄 揚(yáng)聲器類 1. 繪制喇叭 2. 揚(yáng)聲器類 3. 禁音狀態(tài)? 4. 設(shè)置狀態(tài) 5. 切換狀態(tài) 6. 播放音樂 本篇將教你用pyglet畫一個(gè)小喇叭,如上圖。這里要用到pyglety庫(kù)shapes模塊中的圓弧Arc和多邊形Pylygon畫出這個(gè)揚(yáng)聲器的圖片: Arc(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, closed=

    2024年03月10日
    瀏覽(35)
  • Python 一步一步教你用pyglet仿制鴻蒙系統(tǒng)里的時(shí)鐘

    Python 一步一步教你用pyglet仿制鴻蒙系統(tǒng)里的時(shí)鐘

    目錄 鴻蒙時(shí)鐘 1. 繪制圓盤 2. 創(chuàng)建表類 3. 繪制刻度 4. 刻度數(shù)值 5. 添加指針 6. 轉(zhuǎn)動(dòng)指針 7. 聯(lián)動(dòng)時(shí)間 8. 時(shí)鐘走動(dòng) 本篇將用python pyglet庫(kù)復(fù)刻華為手機(jī)鴻蒙系統(tǒng)鬧鐘程序的時(shí)鐘,先在上圖中抓取出時(shí)分秒針及刻度、表盤的顏色RGB值: bHour = (42, 43, 48, 255) bMinute = (70, 71, 75, 255) rSe

    2024年03月12日
    瀏覽(29)
  • FastAPI + NGINX + Gunicorn:一步一步教你部署一個(gè)高性能的Python網(wǎng)頁(yè)應(yīng)用

    FastAPI + NGINX + Gunicorn:一步一步教你部署一個(gè)高性能的Python網(wǎng)頁(yè)應(yīng)用

    部署一個(gè) FastAPI 應(yīng)用到你的服務(wù)器是一項(xiàng)復(fù)雜的任務(wù)。如果你對(duì) NGINX 、 Gunicorn 和 Uvicorn 這些技術(shù)不熟悉,可能會(huì)浪費(fèi)大量的時(shí)間。如果你是剛接觸 Python 語言不久或者希望利用 Python 構(gòu)建自己的Web應(yīng)用程序,本文的內(nèi)容可能會(huì)讓你第一次部署時(shí)更節(jié)省時(shí)間。 FastAPI 是用于開發(fā)

    2024年02月05日
    瀏覽(24)
  • Android一步一步教你實(shí)現(xiàn)Emoji表情鍵盤

    Android一步一步教你實(shí)現(xiàn)Emoji表情鍵盤

    背景: 說到聊天,就離不開文字、表情和圖片,表情和圖片增加了聊天的趣味性,讓原本無聊的文字瞬間用表情動(dòng)了起來,今天給大家?guī)淼氖潜砬殒I盤,教你一步一步實(shí)現(xiàn),先來看下效果圖: 效果圖 功能: 1、如何控制表情鍵盤與輸入法的切換 2、如何解析表情 3、如何處

    2024年02月16日
    瀏覽(17)
  • GitHub入門指南:一步一步教你使用GitHub

    GitHub入門指南:一步一步教你使用GitHub

    引言: GitHub是一個(gè)流行的代碼托管平臺(tái),它提供了強(qiáng)大的版本控制和協(xié)作功能,對(duì)于開發(fā)者來說是一個(gè)不可或缺的工具。本文將一步一步地教你如何使用GitHub,從注冊(cè)賬號(hào)到代碼同步,讓你能夠快速上手并充分利用這個(gè)平臺(tái)。 打開GitHub官網(wǎng)(github.com)。 點(diǎn)擊右上角的\\\"Sign

    2024年02月15日
    瀏覽(21)
  • Midjourney:一步一步教你如何使用 AI 繪畫 MJ

    Midjourney:一步一步教你如何使用 AI 繪畫 MJ

    一步一步如何使用 Midjourney 教程:教學(xué)怎么用 MJ? 原文:如何使用 Midjourney 教程 https://bysocket.com/saas-digital-marketing-channel/ Midjourney是一款使用文字描述來生成高質(zhì)量圖像的AI繪畫工具。這篇文章主要介紹了Midjourney及其用途,并針對(duì)Midjourney的使用提供了一些指南。該工具可以幫

    2023年04月21日
    瀏覽(25)
  • 文本轉(zhuǎn)語音-微軟Azure-一步一步教你從注冊(cè)到使用

    文本轉(zhuǎn)語音-微軟Azure-一步一步教你從注冊(cè)到使用

    牙叔教程 簡(jiǎn)單易懂 他們的中文也許還行, 但是英文我試了都不滿意, 我再網(wǎng)上搜到的我認(rèn)為最好的是 但是丫真貴 Best Free Text To Speech Voice Reader | Speechify 現(xiàn)在的匯率是 139 × 6.91 = 960.49 一年一千塊, 好像還行哈, 但是沒卡呀, 擦, 比來比去, 還是微軟Azure性價(jià)比最高, 沒有微軟Azure的

    2024年02月07日
    瀏覽(27)
  • 一步一步教你如何使用 Visual Studio Code 編譯一段 C# 代碼

    一步一步教你如何使用 Visual Studio Code 編譯一段 C# 代碼

    以下是一步一步教你如何使用 Visual Studio Code 編寫使用 C# 語言輸出當(dāng)前日期和時(shí)間的代碼: 1、下載并安裝 .NET SDK。您可以從 Microsoft 官網(wǎng)下載并安裝它。 2、打開 Visual Studio Code,并安裝 C# 擴(kuò)展。您可以在 Visual Studio Code 中通過擴(kuò)展菜單安裝它。 3、打開 Visual Studio Code 中的文

    2024年02月11日
    瀏覽(33)
  • 【沐風(fēng)老師】一步一步教你在3dMax中進(jìn)行UVW貼圖和展開UVW的方法

    【沐風(fēng)老師】一步一步教你在3dMax中進(jìn)行UVW貼圖和展開UVW的方法

    將簡(jiǎn)單或程序材質(zhì)應(yīng)用于對(duì)象并不難。但是當(dāng)表面需要在其上顯示某種紋理時(shí),它會(huì)變得更加復(fù)雜。任何紋理貼圖都放在材質(zhì)的 Diffuse 插槽中,但渲染的結(jié)果可能無法預(yù)測(cè)。這就是為什么我們需要了解 3DMAX 如何將紋理應(yīng)用于 3D 對(duì)象,什么是 UVW 貼圖,以及為什么要“展開”它

    2024年02月04日
    瀏覽(23)
  • 一步一步教你如何白嫖谷歌云Google Cloud服務(wù)器$300美金羊毛

    一步一步教你如何白嫖谷歌云Google Cloud服務(wù)器$300美金羊毛

    我們都知道,Depay(現(xiàn)在改名為Dupay了)卡平??梢杂糜谖⑿牛Ц秾?,美團(tuán)消費(fèi),直接用USDT做日常小額消費(fèi),還免收手續(xù)費(fèi),小額的話,這點(diǎn)還是很舒服的。 但其實(shí),Depay卡的用途遠(yuǎn)不止此,平??梢远嗤诰蛲诰?。今天教大家如何用Depay卡白嫖谷歌云服務(wù)器。申請(qǐng)成功后隨即可

    2024年02月04日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包