Python Pygame實現(xiàn)落球游戲詳解
引包
引入對應(yīng)的包,和原來一樣寫一個打印文字的方法
import sys, random, ?pygame from pygame.locals import * def print_text(font, x, y, text, color=(255, 255, 255)): ? ? img_text = font.render(text, True, color) ? ? screen.blit(img_text, (x, y))
初始化配置
初始化游戲,pygame窗口的一些信息,以及游戲中用的的一些參數(shù)。
pygame.init()
# 定時器
mainClock = pygame.time.Clock()
# 設(shè)置屏幕和窗口標(biāo)題
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("落球游戲")
# 設(shè)置字體
font1 = pygame.font.SysFont("方正粗黑宋簡體", 24)
pygame.mouse.set_visible(False)
# 設(shè)置顏色變量
white = 255, 255, 255
red = 220, 50, 50
yellow = 230, 230, 50
black = 0, 0, 0
# 生命數(shù)
lives = 3
# 分
score = 0
# 游戲開始flg
game_over = True
# 鼠標(biāo)位置
mouse_x = mouse_y = 0
pos_x = 300
pos_y = 460
# 球落下的x和y軸坐標(biāo)
bomb_x = random.randint(0, 500)
bomb_y = -50
# 球下落的速度
vel_y = 0.7捕捉事件
捕捉事件,如果游戲結(jié)束按下鼠標(biāo),則游戲重新開始, mouse_x, mouse_y捕捉鼠標(biāo)位置, move_x, move_y獲取鼠標(biāo)的偏移
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEMOTION:
mouse_x, mouse_y = event.pos
move_x, move_y = event.rel
elif event.type == MOUSEBUTTONUP:
if game_over:
game_over = False
lives = 3
score = 0
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
填充屏幕讓球下落
讓屏幕填充一個暖色調(diào),如果游戲未開始,屏幕中顯示 “點擊開始新游戲”, 否則球下落。
如果球大于500,則重置一個新球,生命減去一,如果生命沒有了則游戲結(jié)束。
這里沒有使用元素相碰撞原理,有兩個相對的位置,如果球大于擋板的垂直坐標(biāo),切球的x坐標(biāo)大于擋板的開始位置,小于擋板的寬度,則分?jǐn)?shù)添加,重置球的位置。
?? ?screen.fill((255, 166, 77)) ? ? if game_over: ? ? ? ? print_text(font1, 100, 200, "點擊新游戲") ? ? else: ? ? ? ? bomb_y += vel_y ? ? if bomb_y > 500: ? ? ? ? bomb_x = random.randint(0, 500) ? ? ? ? bomb_y = -50 ? ? ? ? lives -= 1 ? ? if lives == 0: ? ? ? ? game_over = True ? ? elif bomb_y > pos_y: ? ? ? ? if bomb_x > pos_x and bomb_x < pos_x + 120: ? ? ? ? ? ? score += 120 ? ? ? ? ? ? bomb_x = random.randint(0, 500) ? ? ? ? ? ? bomb_y = -50
繪制球和擋板,繪制屏幕
根據(jù)bomb_x,bomb_y來顯示球,并繪制一個陰影

矩形一樣

顯示生命數(shù)和分?jǐn)?shù),更新屏幕,設(shè)置每秒的頻率為20
這里明顯有很大的問題,因為圓繪制的時候點是圓心,所以比較的時候就會出錯,如果你用矩形的右部分去接球心左少部分,顯示還是接不到,這里我們不深究,簡單的游戲是實現(xiàn)了,優(yōu)化我放在第二部分。
pygame.draw.circle(screen, black, (bomb_x - 4, int(bomb_y) - 4), 30, 0) ? ? pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0) ? ? pygame.draw.rect(screen, black, (pos_x - 4, pos_y - 4, 120, 40), 0) ? ? pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0) ? ? print_text(font1, 0, 0, "生命數(shù): " + str(lives)) ? ? print_text(font1, 500, 0, "分?jǐn)?shù): " + str(score)) ? ? pygame.display.update() ? ? mainClock.tick(20)

完整代碼
import sys, random, ?pygame
from pygame.locals import *
def print_text(font, x, y, text, color=(255, 255, 255)):
? ? img_text = font.render(text, True, color)
? ? screen.blit(img_text, (x, y))
pygame.init()
# 定時器
mainClock = pygame.time.Clock()
# 設(shè)置屏幕和窗口標(biāo)題
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("落球游戲")
# 設(shè)置字體
font1 = pygame.font.SysFont("方正粗黑宋簡體", 24)
pygame.mouse.set_visible(False)
# 設(shè)置顏色變量
white = 255, 255, 255
red = 220, 50, 50
yellow = 230, 230, 50
black = 0, 0, 0
# 生命條數(shù)
lives = 3
# 分
score = 0
# 游戲開始flg
game_over = True
# 鼠標(biāo)位置
mouse_x = mouse_y = 0
pos_x = 300
pos_y = 460
# 球落下的x和y軸坐標(biāo)
bomb_x = random.randint(0, 500)
bomb_y = -50
# 球下落的速度
vel_y = 0.7
while True:
? ? for event in pygame.event.get():
? ? ? ? if event.type == QUIT:
? ? ? ? ? ? sys.exit()
? ? ? ? elif event.type == MOUSEMOTION:
? ? ? ? ? ? mouse_x, mouse_y = event.pos
? ? ? ? ? ? move_x, move_y = event.rel
? ? ? ? elif event.type == MOUSEBUTTONUP:
? ? ? ? ? ? if game_over:
? ? ? ? ? ? ? ? game_over = False
? ? ? ? ? ? ? ? lives = 3
? ? ? ? ? ? ? ? score = 0
? ? keys = pygame.key.get_pressed()
? ? if keys[K_ESCAPE]:
? ? ? ? sys.exit()
? ? screen.fill((255, 166, 77))
? ? if game_over:
? ? ? ? print_text(font1, 100, 200, "點擊新游戲")
? ? else:
? ? ? ? bomb_y += vel_y
? ? if bomb_y > 500:
? ? ? ? bomb_x = random.randint(0, 500)
? ? ? ? bomb_y = -50
? ? ? ? lives -= 1
? ? if lives == 0:
? ? ? ? game_over = True
? ? elif bomb_y > pos_y:
? ? ? ? if bomb_x > pos_x and bomb_x < pos_x + 120:
? ? ? ? ? ? score += 120
? ? ? ? ? ? bomb_x = random.randint(0, 500)
? ? ? ? ? ? bomb_y = -50
? ? pygame.draw.circle(screen, black, (bomb_x - 4, int(bomb_y) - 4), 30, 0)
? ? pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0)
? ? pos_x = mouse_x
? ? if pos_x < 0:
? ? ? ? pos_x = 0
? ? elif pos_x > 500:
? ? ? ? pos_x = 500
? ? pygame.draw.rect(screen, black, (pos_x - 4, pos_y - 4, 120, 40), 0)
? ? pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0)
? ? print_text(font1, 0, 0, "生命數(shù): " + str(lives))
? ? print_text(font1, 500, 0, "分?jǐn)?shù): ?" + str(score))
? ? pygame.display.update()
? ? mainClock.tick(20)到此這篇關(guān)于Python Pygame實現(xiàn)落球游戲詳解的文章就介紹到這了,更多相關(guān)Python Pygame落球游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中IO多路復(fù)用模塊selector的用法詳解
selector?是一個實現(xiàn)了IO復(fù)用模型的python包,實現(xiàn)了IO多路復(fù)用模型的?select、poll?和?epoll?等函數(shù),下面就跟隨小編一起來學(xué)習(xí)一下它的具體使用吧2024-02-02
python實現(xiàn)敲木魚加功德包含加音效和敲擊動作(附demo)
敲木魚加功德是一款很火的動畫,本文主要介紹了python實現(xiàn)敲木魚加功德包含加音效和敲擊動作,具有一定的參考價值,感興趣的可以了解一下2023-11-11
python由已知數(shù)組快速生成新數(shù)組的方法
這篇文章主要介紹了python由已知數(shù)組快速生成新數(shù)組的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
pyqt5+opencv?實現(xiàn)讀取視頻數(shù)據(jù)的方法
這篇文章主要介紹了pyqt5+opencv?實現(xiàn)讀取視頻數(shù)據(jù)的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01

