pygame實(shí)現(xiàn)彈球游戲
本文實(shí)例為大家分享了pygame實(shí)現(xiàn)彈球游戲的具體代碼,供大家參考,具體內(nèi)容如下
pygame彈球游戲
寫的很簡陋
pip install pygame 安裝pygame模塊
代碼,復(fù)制運(yùn)行即可
import pygame
import random
pygame.init()
win = pygame.display.set_mode((600, 600)) # 畫布窗口的大小
pygame.display.set_caption("彈球游戲") # 窗口標(biāo)題
x, y = 300, 0 # 方塊的起點(diǎn)
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) # 窗口標(biāo)題
continue
if y >= 590:
status = 1
stop = not stop
str1 = "游戲結(jié)束,回車重新開始,反彈次數(shù)" + str(count)
count = 0
pygame.display.set_caption("彈球游戲") # 窗口標(biāo)題
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 高度坐標(biāo) 200 x 寬度坐標(biāo) 200
# x坐標(biāo)加300 大于 寬度初始坐標(biāo), 小于 寬度+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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Django框架利用Ajax實(shí)現(xiàn)點(diǎn)贊功能實(shí)例代碼
點(diǎn)贊這個(gè)功能是我們現(xiàn)在經(jīng)常會遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于基于Django框架利用Ajax實(shí)現(xiàn)點(diǎn)贊功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
python內(nèi)置模塊collections知識點(diǎn)總結(jié)
這篇文章主要介紹了python內(nèi)置模塊collections知識點(diǎn)總結(jié),有興趣的朋友們學(xué)習(xí)下。2019-12-12
淺析Python如何監(jiān)聽和響應(yīng)鍵盤按鍵
在許多編程場景中,接收并響應(yīng)用戶輸入是至關(guān)重要的,本文主要為大家詳細(xì)介紹如何使用Python來監(jiān)聽和響應(yīng)鍵盤按鍵,有需要的小伙伴可以參考下2024-03-03
python flask框架實(shí)現(xiàn)傳數(shù)據(jù)到j(luò)s的方法分析
這篇文章主要介紹了python flask框架實(shí)現(xiàn)傳數(shù)據(jù)到j(luò)s的方法,結(jié)合實(shí)例形式分析了前端數(shù)據(jù)序列化及后臺Flask交互數(shù)據(jù)返回相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
Pandas數(shù)據(jù)分組統(tǒng)計(jì)的實(shí)現(xiàn)示例
對數(shù)據(jù)進(jìn)行分組統(tǒng)計(jì),主要適用DataFrame對象的groupby()函數(shù),本文就來詳細(xì)的介紹下Pandas數(shù)據(jù)分組統(tǒng)計(jì)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解下2023-11-11
numpy矩陣乘法中的multiply,matmul和dot的使用
本文主要介紹了numpy矩陣乘法中的multiply,matmul和dot的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
一起用Python做個(gè)上課點(diǎn)名器的制作過程
今天給大家分享一個(gè)讀者粉絲投稿的,關(guān)于上課點(diǎn)名的實(shí)戰(zhàn)案例,對Python上課點(diǎn)名器實(shí)現(xiàn)過程感興趣的朋友,一起來看看是如何實(shí)現(xiàn)的吧2021-09-09
python 虛擬環(huán)境調(diào)用allure報(bào)錯(cuò):FileNotFoundError: [WinError
python代碼調(diào)用命令行 allure命令報(bào)錯(cuò),提示找不到allure這個(gè)命令,本文就詳細(xì)的介紹了具體的解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

