python植物大战僵尸代码例_python实现植物大战僵尸游戏实例代码
開發思路
完整項目地址:https://github.com/371854496/…
覺得還OK的話,點下Star,作者不易,thank you!
實現方法
1.引入需要的模塊,配置圖片路徑,設置界面寬高背景顏色,創建游戲主入口。
#1引入需要的模塊
import pygame
import random
#1配置圖片地址
IMAGE_PATH = 'imgs/'
#1設置頁面寬高
scrrr_width=800
scrrr_height =560
#1創建控制游戲結束的狀態
GAMEOVER = False
#1主程序
class MainGame():
#1加載游戲窗口
def init_window(self):
#1調用顯示模塊的初始化
pygame.display.init()
#1創建窗口
MainGame.window = pygame.display.set_mode([scrrr_width,scrrr_height]) #
#1開始游戲
def start_game(self):
#1初始化窗口
self.init_window()
#1只要游戲沒結束,就一直循環
while not GAMEOVER:
#1渲染白色背景
MainGame.window.fill((255, 255, 255))
#1實時更新
pygame.display.update()
#1啟動主程序
if __name__ == '__main__':
game = MainGame()
game.start_game()
2.文本繪制,創建要動態改變的屬性,渲染的位置
#2 創建關數,得分,剩余分數,錢數
shaoguan = 1
score = 0
remnant_score = 100
money = 200
#2 文本繪制
def draw_text(self, content, size, color):
pygame.font.init()
font = pygame.font.SysFont('kaiti', size)
text = font.render(content, True, color)
return text
#2 加載幫助提示
def load_help_text(self):
text1 = self.draw_text('1.按左鍵創建向日葵 2.按右鍵創建豌豆射手', 26, (255, 0, 0))
MainGame.window.blit(text1, (5, 5))
#2 渲染的文字和坐標位置
MainGame.window.blit(self.draw_text('當前錢數$: {}'.format(MainGame.money), 26, (255, 0, 0)), (500, 40))
MainGame.window.blit(self.draw_text(
'當前關數{},得分{},距離下關還差{}分'.format(MainGame.shaoguan, MainGame.score, MainGame.remnant_score), 26,
(255, 0, 0)), (5, 40))
self.load_help_text()
3.創建地圖類,初始化地圖和坐標
#3 創建地圖類
class Map():
#3 存儲兩張不同顏色的圖片名稱
map_names_list = [IMAGE_PATH + 'map1.png', IMAGE_PATH + 'map2.png'] #3 初始化地圖
def __init__(self, x, y, img_index):
self.image = pygame.image.load(Map.map_names_list[img_index])
self.position = (x, y)
# 是否能夠種植
self.can_grow = True
#3 加載地圖
def load_map(self):
MainGame.window.blit(self.image,self.position)
總結
以上是生活随笔為你收集整理的python植物大战僵尸代码例_python实现植物大战僵尸游戏实例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分销平台使用手册
- 下一篇: python植物大战僵尸代码