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è)置屏幕和窗口標題
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
# 鼠標位置
mouse_x = mouse_y = 0
pos_x = 300
pos_y = 460
# 球落下的x和y軸坐標
bomb_x = random.randint(0, 500)
bomb_y = -50
# 球下落的速度
vel_y = 0.7捕捉事件
捕捉事件,如果游戲結(jié)束按下鼠標,則游戲重新開始, mouse_x, mouse_y捕捉鼠標位置, move_x, move_y獲取鼠標的偏移
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é)束。
這里沒有使用元素相碰撞原理,有兩個相對的位置,如果球大于擋板的垂直坐標,切球的x坐標大于擋板的開始位置,小于擋板的寬度,則分數(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ù)和分數(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, "分數(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è)置屏幕和窗口標題
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
# 鼠標位置
mouse_x = mouse_y = 0
pos_x = 300
pos_y = 460
# 球落下的x和y軸坐標
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, "分數(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實現(xiàn)文字轉(zhuǎn)語音的項目實踐
pyttsx3是一個Python庫,用于文字轉(zhuǎn)語音的功能,它可以將文本轉(zhuǎn)換為語音,并使用不同的音頻引擎進行輸出,本文就來詳細的介紹一下用法,具有一定的參考價值,感興趣的可以了解一下2023-08-08
Win10下配置tensorflow-gpu的詳細教程(無VS2015/2017)
這篇文章主要介紹了Win10下配置tensorflow-gpu(無VS2015/2017),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
解決python 輸出到csv 出現(xiàn)多空行的情況
這篇文章主要介紹了解決python 輸出到csv 出現(xiàn)多空行的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03

