当前位置:   article > 正文

【Python游戏】Python实现一个植物大战僵尸小游戏,非常简单,可以用于做毕业设计哟 | 附源码_微信代码小游戏pvz

微信代码小游戏pvz

前言

halo,包子们上午好
今天给打击整一个植物大战僵尸 无广告版本 哈哈
说实话,现在的小游戏很多都是有广告,多少有点难受
今天给大家直接安排

相关文件

关注小编,私信小编领取哟!
当然别忘了一件三连哟~~

Python零基础入门到精通视频合集

【整整800集】Python爬虫项目零基础入门合集,细狗都学会了,你还不会?

开发工具

Python版本:3.7.8
相关模块:
pygame模块;
random模块;
以及一些python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

效果展示

在这里插入图片描述
在这里插入图片描述

代码展示

导入模块

#1 引入需要的模块
import pygame
import random
  • 1
  • 2
  • 3

配置图片地址

IMAGE_PATH = 'imgs/'
  • 1

设置页面宽高

scrrr_width=800
scrrr_height =560
  • 1
  • 2

创建控制游戏结束的状态

GAMEOVER = False
  • 1

图片加载报错处理

LOG = '文件:{}中的方法:{}出错'.format(__file__,__name__)
  • 1

创建地图类

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

植物类

class Plant(pygame.sprite.Sprite):
    def __init__(self):
        super(Plant, self).__init__()
        self.live=True

    # 加载图片
    def load_image(self):
        if hasattr(self, 'image') and hasattr(self, 'rect'):
            MainGame.window.blit(self.image, self.rect)
        else:
            print(LOG)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

向日葵类

class Sunflower(Plant):
    def __init__(self,x,y):
        super(Sunflower, self).__init__()
        self.image = pygame.image.load('imgs/sunflower.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 100
        #5 时间计数器
        self.time_count = 0

    #5 新增功能:生成阳光
    def produce_money(self):
        self.time_count += 1
        if self.time_count == 25:
            MainGame.money += 5
            self.time_count = 0
    #5 向日葵加入到窗口中
    def display_sunflower(self):
        MainGame.window.blit(self.image,self.rect)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

豌豆射手类

class PeaShooter(Plant):
    def __init__(self,x,y):
        super(PeaShooter, self).__init__()
        # self.image 为一个 surface
        self.image = pygame.image.load('imgs/peashooter.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 200
        #6 发射计数器
        self.shot_count = 0

    #6 增加射击方法
    def shot
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/760538
推荐阅读
相关标签
  

闽ICP备14008679号