您好,登錄后才能下訂單哦!
這篇文章主要介紹了基于Python+Pygame如何實現變異狗大戰游戲的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇基于Python+Pygame如何實現變異狗大戰游戲文章都會有所收獲,下面我們一起來看看吧。
1)環境安裝
本文用到的環境如下——
Python3、Pycharm社區版,pygame其他自帶的庫只要安裝完 Python就可以直接使用了
一般安裝:pip install +模塊名
鏡像源安裝:pip install -i pypi.douban.com/simple/+模塊名…
1)導入庫
import pygame, sys from pygame.locals import *
2)主程序
def pygame_run(): pygame.init() _display_surf = pygame.display.set_mode((480, 320)) pygame.display.set_caption('py夢') _font_type = pygame.font.match_font('Microsoft YaHei') # 敵方精靈狀態,文字顯示 _ord_pym_rect = pygame.Rect(-260, 0, 220, 50) # 敵方精靈名字,文字顯示設置 _ord_pym_name = pygame.font.Font(_font_type, 16) _ord_pym_name_surf_obj = _ord_pym_name.render("lv10:它狗", True, BLACK, None) _ord_pym_name_rect = _ord_pym_name_surf_obj.get_rect() _ord_pym_name_rect.left = -200 _ord_pym_name_rect.top = 0 # 敵方精靈血量,文字顯示設置 _ord_pym_blood = pygame.font.Font(_font_type, 16) _ord_pym_blood_surf_obj = _ord_pym_blood.render("血量:----------[69/69]", True, BLACK, None) _ord_pym_blood_rect = _ord_pym_blood_surf_obj.get_rect() _ord_pym_blood_rect.left = -200 _ord_pym_blood_rect.top = 20 # 敵方精靈貼圖顯示設置 _ord_pym_img = pygame.image.load('dog1.png') _ord_pym_img_top = 20 _ord_pym_img_left = 320+220 # 我方精靈狀態,文字顯示設置 _my_pym_rect = pygame.Rect(260, 170, 220, 50) # 我方精靈名字,文字顯示設置 _my_pym_name = pygame.font.Font(_font_type, 16) _my_pym_name_surf_obj = _my_pym_name.render("lv18:我狗", True, BLACK, None) _my_pym_name_rect = _my_pym_name_surf_obj.get_rect() _my_pym_name_rect.left = 480 _my_pym_name_rect.top = 170 # 我方精靈血量,文字顯示設置 _my_pym_blood = pygame.font.Font(_font_type, 16) _my_pym_blood_surf_obj = _my_pym_blood.render("血量:----------[99/99]", True, BLACK, None) _my_pym_blood_rect = _my_pym_blood_surf_obj.get_rect() _my_pym_blood_rect.left = 480 _my_pym_blood_rect.top = 190 # 我方精靈貼圖顯示設置 _my_pym_img = pygame.image.load('dog2.png') _my_pym_img_top = 80 _my_pym_img_left = 20-220 # 對戰面板,顯示設置 _select_rect = pygame.Rect(480, 220, 220, 95) # 戰斗,文字顯示設置 _select_font_1 = pygame.font.Font(_font_type, 30) _select_font_1_surf_obj = _select_font_1.render("戰斗", True, BLACK, None) _select_font_1_rect = _select_font_1_surf_obj.get_rect() _select_font_1_rect.left = 480 _select_font_1_rect.top = 220 # 道具,文字顯示設置 _select_font_2 = pygame.font.Font(_font_type, 30) _select_font_2_surf_obj = _select_font_2.render("道具", True, BLACK, None) _select_font_2_rect = _select_font_2_surf_obj.get_rect() _select_font_2_rect.left = 580 _select_font_2_rect.top = 220 # 精靈,文字顯示設置 _select_font_3 = pygame.font.Font(_font_type, 30) _select_font_3_surf_obj = _select_font_3.render("精靈", True, BLACK, None) _select_font_3_rect = _select_font_3_surf_obj.get_rect() _select_font_3_rect.left = 480 _select_font_3_rect.top = 270 # 逃跑,文字顯示設置 _select_font_4 = pygame.font.Font(_font_type, 30) _select_font_4_surf_obj = _select_font_4.render("逃跑", True, BLACK, None) _select_font_4_rect = _select_font_4_surf_obj.get_rect() _select_font_4_rect.left = 580 _select_font_4_rect.top = 270 while True: _display_surf.fill(WHITE) pygame.draw.rect(_display_surf, BLACK, _select_rect, 1) pygame.draw.rect(_display_surf, WHITE, _my_pym_rect, 0) _display_surf.blit(_ord_pym_img, (_ord_pym_img_left, _ord_pym_img_top)) _display_surf.blit(_my_pym_img, (_my_pym_img_left, _my_pym_img_top)) _display_surf.blit(_ord_pym_name_surf_obj, _ord_pym_name_rect) _display_surf.blit(_ord_pym_blood_surf_obj, _ord_pym_blood_rect) _display_surf.blit(_my_pym_name_surf_obj, _my_pym_name_rect) _display_surf.blit(_my_pym_blood_surf_obj, _my_pym_blood_rect) _display_surf.blit(_select_font_1_surf_obj, _select_font_1_rect) _display_surf.blit(_select_font_2_surf_obj, _select_font_2_rect) _display_surf.blit(_select_font_3_surf_obj, _select_font_3_rect) _display_surf.blit(_select_font_4_surf_obj, _select_font_4_rect) if _select_rect.left != 260: _select_rect.left = _select_rect.left - 5 _select_font_1_rect.left = _select_font_1_rect.left - 5 _select_font_2_rect.left = _select_font_2_rect.left - 5 _select_font_3_rect.left = _select_font_3_rect.left - 5 _select_font_4_rect.left = _select_font_4_rect.left - 5 _my_pym_name_rect.left = _my_pym_name_rect.left - 5 _my_pym_blood_rect.left = _my_pym_blood_rect.left - 5 _ord_pym_name_rect.left = _ord_pym_name_rect.left + 5 _ord_pym_blood_rect.left = _ord_pym_blood_rect.left + 5 _ord_pym_img_left = _ord_pym_img_left - 5 _my_pym_img_left = _my_pym_img_left + 5 for _event in pygame.event.get(): if _event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() _fps_Clock.tick(FPS) if __name__ == '__main__': pygame_run()
關于“基于Python+Pygame如何實現變異狗大戰游戲”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“基于Python+Pygame如何實現變異狗大戰游戲”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。