pygame實現(xiàn)彈球游戲
本文實例為大家分享了pygame實現(xiàn)彈球游戲的具體代碼,供大家參考,具體內(nèi)容如下
pygame彈球游戲
寫的很簡陋
pip install pygame 安裝pygame模塊
代碼,復制運行即可
import pygame import random pygame.init() win = pygame.display.set_mode((600, 600)) # 畫布窗口的大小 pygame.display.set_caption("彈球游戲") # 窗口標題 x, y = 300, 0 # 方塊的起點 width, height = 10, 10 # 方塊的寬,高 speed = 1 # 速度 def _randomOK(): return random.randint(0, 1) stop = False _random = _randomOK() str1 = "暫停中" baffle = 250 status = 0 count = 0 top = 0 while True: # 刷新頻率, 小球移動速度 pygame.time.Clock().tick(1000) for event in pygame.event.get(): # 窗口x事件 if event.type == pygame.QUIT: exit(0) elif event.type == pygame.KEYDOWN: # 回車事件 if event.key == 13: str1 = "暫停中" stop = not stop if status == 1: x, y = 300, 0 keys = pygame.key.get_pressed() if stop: pygame.display.set_caption(str1) # 窗口標題 continue if y >= 590: status = 1 stop = not stop str1 = "游戲結(jié)束,回車重新開始,反彈次數(shù)" + str(count) count = 0 pygame.display.set_caption("彈球游戲") # 窗口標題 if y == 0: top = 0 if top == 0: if _random == 0: # 向下左彈 x -= speed y += speed elif _random == 1: x += speed y += speed else: if _random == 0: # 向上左彈 x -= speed y -= speed elif _random == 1: # 向上右彈 x += speed y -= speed # 方向箭頭響應(yīng) if keys[pygame.K_LEFT]: baffle -= speed if baffle < 0: baffle = 0 if keys[pygame.K_RIGHT]: baffle += speed if baffle > 500: baffle = 500 # 碰撞邏輯 if 500 <= y <= 520: print(x, y) print(baffle) # y 高度坐標 200 x 寬度坐標 200 # x坐標加300 大于 寬度初始坐標, 小于 寬度+300 if baffle <= x <= baffle + 100: count += 1 top = 1 # 防止跑出邊界 if x > win.get_size()[0] - width: _random = _randomOK() x = win.get_size()[0] - width if x < 0: _random = _randomOK() x = 0 if y > win.get_size()[1] - height: _random = _randomOK() y = win.get_size()[1] - height if y < 0: _random = _randomOK() y = 0 # 將每一幀的底色先填充成黑色 win.fill((64, 158, 255)) # 畫方塊 pygame.draw.rect(win, (255, 0, 0), (x, y, width, height)) # 擋板設(shè)置, pygame.draw.rect(win, (255, 255, 255), (baffle, 500, 100, 20)) # 更新畫布 pygame.display.update() pygame.quit()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Django框架利用Ajax實現(xiàn)點贊功能實例代碼
點贊這個功能是我們現(xiàn)在經(jīng)常會遇到的一個功能,下面這篇文章主要給大家介紹了關(guān)于基于Django框架利用Ajax實現(xiàn)點贊功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08python內(nèi)置模塊collections知識點總結(jié)
這篇文章主要介紹了python內(nèi)置模塊collections知識點總結(jié),有興趣的朋友們學習下。2019-12-12淺析Python如何監(jiān)聽和響應(yīng)鍵盤按鍵
在許多編程場景中,接收并響應(yīng)用戶輸入是至關(guān)重要的,本文主要為大家詳細介紹如何使用Python來監(jiān)聽和響應(yīng)鍵盤按鍵,有需要的小伙伴可以參考下2024-03-03python flask框架實現(xiàn)傳數(shù)據(jù)到j(luò)s的方法分析
這篇文章主要介紹了python flask框架實現(xiàn)傳數(shù)據(jù)到j(luò)s的方法,結(jié)合實例形式分析了前端數(shù)據(jù)序列化及后臺Flask交互數(shù)據(jù)返回相關(guān)操作技巧,需要的朋友可以參考下2019-06-06Pandas數(shù)據(jù)分組統(tǒng)計的實現(xiàn)示例
對數(shù)據(jù)進行分組統(tǒng)計,主要適用DataFrame對象的groupby()函數(shù),本文就來詳細的介紹下Pandas數(shù)據(jù)分組統(tǒng)計的實現(xiàn),具有一定的參考價值,感興趣的可以了解下2023-11-11numpy矩陣乘法中的multiply,matmul和dot的使用
本文主要介紹了numpy矩陣乘法中的multiply,matmul和dot的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02python 虛擬環(huán)境調(diào)用allure報錯:FileNotFoundError: [WinError
python代碼調(diào)用命令行 allure命令報錯,提示找不到allure這個命令,本文就詳細的介紹了具體的解決方法,具有一定的參考價值,感興趣的可以了解一下2023-09-09