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)文章
Python實(shí)現(xiàn)腳本轉(zhuǎn)換為命令行程序
使用Python中的scaffold和click庫,你可以將一個簡單的實(shí)用程序升級為一個成熟的命令行界面工具,本文就來帶你看看具體實(shí)現(xiàn)方法,感興趣的可以了解下2022-09-09
Python itertools庫中product函數(shù)使用實(shí)例探究
這篇文章主要為大家介紹了Python itertools庫中product函數(shù)使用實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
一文帶你探索Python中的eventlet通信機(jī)制
這篇文章主要為大家詳細(xì)介紹了Python中的eventlet通信機(jī)制的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們深入了解Python有一定幫助,需要的可以參考一下2023-06-06
Python Arrow處理時間數(shù)據(jù)使用詳解(標(biāo)準(zhǔn)庫之外另一種選擇)
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫之外Arrow處理時間數(shù)據(jù)的另一種選擇使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python對XML文件實(shí)現(xiàn)增刪改查操作
這篇文章主要為大家詳細(xì)介紹了Python對XML文件進(jìn)行實(shí)現(xiàn)增刪改查操作的方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的可以了解一下2022-11-11
Python數(shù)據(jù)分析pandas之布爾索引使用詳解
這篇文章主要為大家介紹了Python數(shù)據(jù)分析pandas之布爾索引使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Python網(wǎng)絡(luò)編程之socket與socketserver
這篇文章介紹了Python網(wǎng)絡(luò)編程之socket與socketserver,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

