Python Pygame實(shí)現(xiàn)落球游戲詳解
引包
引入對(duì)應(yīng)的包,和原來(lái)一樣寫一個(gè)打印文字的方法
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() # 定時(shí)器 mainClock = pygame.time.Clock() # 設(shè)置屏幕和窗口標(biāo)題 screen = pygame.display.set_mode((600, 500)) pygame.display.set_caption("落球游戲") # 設(shè)置字體 font1 = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 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 # 游戲開(kāi)始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),則游戲重新開(kāi)始, 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()
填充屏幕讓球下落
讓屏幕填充一個(gè)暖色調(diào),如果游戲未開(kāi)始,屏幕中顯示 “點(diǎn)擊開(kāi)始新游戲”, 否則球下落。
如果球大于500,則重置一個(gè)新球,生命減去一,如果生命沒(méi)有了則游戲結(jié)束。
這里沒(méi)有使用元素相碰撞原理,有兩個(gè)相對(duì)的位置,如果球大于擋板的垂直坐標(biāo),切球的x坐標(biāo)大于擋板的開(kāi)始位置,小于擋板的寬度,則分?jǐn)?shù)添加,重置球的位置。
?? ?screen.fill((255, 166, 77)) ? ? if game_over: ? ? ? ? print_text(font1, 100, 200, "點(diǎn)擊新游戲") ? ? 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來(lái)顯示球,并繪制一個(gè)陰影
矩形一樣
顯示生命數(shù)和分?jǐn)?shù),更新屏幕,設(shè)置每秒的頻率為20
這里明顯有很大的問(wèn)題,因?yàn)閳A繪制的時(shí)候點(diǎn)是圓心,所以比較的時(shí)候就會(huì)出錯(cuò),如果你用矩形的右部分去接球心左少部分,顯示還是接不到,這里我們不深究,簡(jiǎn)單的游戲是實(shí)現(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() # 定時(shí)器 mainClock = pygame.time.Clock() # 設(shè)置屏幕和窗口標(biāo)題 screen = pygame.display.set_mode((600, 500)) pygame.display.set_caption("落球游戲") # 設(shè)置字體 font1 = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 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 # 游戲開(kāi)始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, "點(diǎn)擊新游戲") ? ? 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實(shí)現(xiàn)落球游戲詳解的文章就介紹到這了,更多相關(guān)Python Pygame落球游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python入門教程(十一)Python中的運(yùn)算符
這篇文章主要介紹了Python入門教程(十一)Python中的運(yùn)算符,Python是一門非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下2023-04-04python實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音的項(xiàng)目實(shí)踐
pyttsx3是一個(gè)Python庫(kù),用于文字轉(zhuǎn)語(yǔ)音的功能,它可以將文本轉(zhuǎn)換為語(yǔ)音,并使用不同的音頻引擎進(jìn)行輸出,本文就來(lái)詳細(xì)的介紹一下用法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08Win10下配置tensorflow-gpu的詳細(xì)教程(無(wú)VS2015/2017)
這篇文章主要介紹了Win10下配置tensorflow-gpu(無(wú)VS2015/2017),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07解決python 輸出到csv 出現(xiàn)多空行的情況
這篇文章主要介紹了解決python 輸出到csv 出現(xiàn)多空行的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03