<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      python之植物大戰(zhàn)僵尸

      `import pygame
      import random
      import sys
      import os

      全局常量定義素材路徑(確保有 imgs 文件夾存放對應(yīng)圖片)

      MAP_IMAGE = "E:/python代碼/imgs/map1.png"
      SUNFLOWER_IMAGE = "E:/python代碼/imgs/map2.png"
      PEASHOOTER_IMAGE = "E:/python代碼/imgs/map3.png"
      ZOMBIE_IMAGE = "E:/python代碼/imgs/map4.png"
      BULLET_IMAGE = "E:/python代碼/imgs/map5.png"
      SUN_IMAGE = "E:/python代碼/imgs/map6.png"

      初始化 Pygame

      pygame.init()
      screen = pygame.display.set_mode((800, 600))
      pygame.display.set_caption("植物大戰(zhàn)僵尸")
      clock = pygame.time.Clock()

      全局變量

      sunshine = 200 # 初始陽光數(shù)量
      zombie_list = []
      plant_list = []
      bullet_list = []
      sun_list = []
      zombie_spawn_timer = 0 # 僵尸生成計時器,提前初始化

      地圖類

      class Map:
      def init(self, image_path):
      try:
      self.image = pygame.image.load(image_path).convert()
      except:
      self.image = pygame.Surface((800, 600))
      self.image.fill((0, 128, 0)) # 綠色背景替代
      self.rect = self.image.get_rect()

      def display(self):
          screen.blit(self.image, self.rect)
      

      植物基類

      class Plant(pygame.sprite.Sprite):
      def init(self, x, y, image_path, cost, health):
      super().init()
      try:
      self.image = pygame.image.load(image_path).convert_alpha()
      except:
      self.image = pygame.Surface((50, 50))
      self.image.fill((255, 0, 0))
      self.rect = self.image.get_rect(topleft=(x, y))
      self.cost = cost
      self.health = health
      self.is_alive = True

      def update(self):
          if self.health <= 0:
              self.is_alive = False
              self.kill()
      

      向日葵類

      class Sunflower(Plant):
      def init(self, x, y):
      super().init(x, y, SUNFLOWER_IMAGE, 50, 100)
      self.production_timer = 0

      def produce_sunshine(self):
          self.production_timer += 1
          if self.production_timer >= 100:
              new_sun = Sun(self.rect.centerx, self.rect.centery)
              sun_list.append(new_sun)
              self.production_timer = 0
      
      def display(self):
          if self.is_alive:
              screen.blit(self.image, self.rect)
      

      豌豆射手類

      class PeaShooter(Plant):
      def init(self, x, y):
      super().init(x, y, PEASHOOTER_IMAGE, 50, 100)
      self.shoot_timer = 0

      def shoot(self):
          self.shoot_timer += 1
          if self.shoot_timer >= 100:
              new_bullet = PeaBullet(self.rect.centerx + 30, self.rect.centery)
              bullet_list.append(new_bullet)
              self.shoot_timer = 0
      
      def display(self):
          if self.is_alive:
              screen.blit(self.image, self.rect)
      

      僵尸類

      class Zombie(pygame.sprite.Sprite):
      def init(self, x, y):
      super().init()
      try:
      self.image = pygame.image.load(ZOMBIE_IMAGE).convert_alpha()
      except:
      self.image = pygame.Surface((60, 100))
      self.image.fill((128, 128, 128))
      self.rect = self.image.get_rect(topleft=(x, y))
      self.health = 100
      self.speed = 1
      self.is_alive = True

      def move(self):
          if self.is_alive:
              self.rect.x -= self.speed
              if self.rect.x < -self.rect.width:
                  self.is_alive = False
                  self.kill()
      
      def display(self):
          if self.is_alive:
              screen.blit(self.image, self.rect)
      

      子彈類

      class PeaBullet(pygame.sprite.Sprite):
      def init(self, x, y):
      super().init()
      try:
      self.image = pygame.image.load(BULLET_IMAGE).convert_alpha()
      except:
      self.image = pygame.Surface((10, 5))
      self.image.fill((255, 255, 0))
      self.rect = self.image.get_rect(topleft=(x, y))
      self.speed = 5
      self.is_alive = True

      def move(self):
          if self.is_alive:
              self.rect.x += self.speed
              if self.rect.x > 800:
                  self.is_alive = False
                  self.kill()
      
      def hit(self):
          for zombie in zombie_list:
              if pygame.sprite.collide_rect(self, zombie):
                  zombie.health -= 50
                  self.is_alive = False
                  self.kill()
      
      def display(self):
          if self.is_alive:
              screen.blit(self.image, self.rect)
      

      陽光類

      class Sun(pygame.sprite.Sprite):
      def init(self, x, y):
      super().init()
      try:
      self.image = pygame.image.load(SUN_IMAGE).convert_alpha()
      except:
      self.image = pygame.Surface((30, 30), pygame.SRCALPHA)
      pygame.draw.circle(self.image, (255, 215, 0), (15, 15), 15)
      self.rect = self.image.get_rect(center=(x, y))
      self.speed = 1
      self.is_alive = True

      def fall(self):
          if self.is_alive:
              self.rect.y += self.speed
              if self.rect.y > 600:
                  self.is_alive = False
                  self.kill()
      
      def display(self):
          if self.is_alive:
              screen.blit(self.image, self.rect)
      

      主游戲邏輯函數(shù)

      def main():
      global sunshine, zombie_spawn_timer
      game_map = Map(MAP_IMAGE)
      running = True
      while running:
      # 處理事件
      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      running = False
      elif event.type == pygame.MOUSEBUTTONDOWN:
      x, y = pygame.mouse.get_pos()
      # 判斷種植向日葵
      if sunshine >= 50 and y > 100:
      clicked_plant = Sunflower(x - 25, y - 25)
      plant_list.append(clicked_plant)
      sunshine -= 50
      # 判斷種植豌豆射手
      elif sunshine >= 100 and y > 100:
      clicked_plant = PeaShooter(x - 25, y - 25)
      plant_list.append(clicked_plant)
      sunshine -= 100
      # 收集陽光
      for sun in sun_list:
      if sun.rect.collidepoint(x, y):
      sunshine += 25
      sun.is_alive = False
      sun.kill()

          # 生成僵尸
          zombie_spawn_timer += 1
          if zombie_spawn_timer >= 300:
              zombie_y = random.randint(100, 400)
              new_zombie = Zombie(800, zombie_y)
              zombie_list.append(new_zombie)
              zombie_spawn_timer = 0
      
          # 更新植物
          for plant in plant_list:
              if isinstance(plant, Sunflower):
                  plant.produce_sunshine()
              elif isinstance(plant, PeaShooter):
                  plant.shoot()
              plant.update()
      
          # 更新僵尸
          for zombie in zombie_list:
              zombie.move()
      
          # 更新子彈
          for bullet in bullet_list:
              bullet.move()
              bullet.hit()
      
          # 更新陽光
          for sun in sun_list:
              sun.fall()
      
          # 繪制地圖
          game_map.display()
          # 繪制植物
          for plant in plant_list:
              plant.display()
          # 繪制僵尸
          for zombie in zombie_list:
              zombie.display()
          # 繪制子彈
          for bullet in bullet_list:
              bullet.display()
          # 繪制陽光
          for sun in sun_list:
              sun.display()
      
          # 顯示陽光數(shù)量
          font = pygame.font.SysFont(None, 36)
          sunshine_text = font.render(f"陽光: {sunshine}", True, (255, 255, 0))
          screen.blit(sunshine_text, (10, 10))
      
          # 控制幀率
          clock.tick(30)
          pygame.display.flip()
      
      pygame.quit()
      sys.exit()
      

      if name == "main":
      main()`

      posted @ 2025-06-23 13:29  何定霓  閱讀(19)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 和田县| 兴安县| 日韩精品亚洲精品第一页| 精品无码一区二区三区在线| 免费十八禁一区二区三区| 日韩乱码人妻无码中文字幕视频| 日韩精品中文字幕有码| A级毛片100部免费看| 狠狠色噜噜狠狠亚洲AV| 视频一区视频二区卡通动漫| 国产亚洲999精品AA片在线爽| 狠狠色婷婷久久综合频道日韩 | 久久精品一区二区东京热| 国产精品国色综合久久| 亚洲欧洲色图片网站| 中文字幕精品无码一区二区三区| 久久国产精品第一区二区| 少妇被无套内谢免费看| 欧美成人一区二区三区不卡| 亚洲日韩欧美一区二区三区在线| 色综合AV综合无码综合网站| 国产精品无码不卡在线播放 | 久久精品熟女亚洲av麻| 滦平县| 麻豆成人精品国产免费| 亚洲国产精品久久久天堂麻豆宅男 | 精品无码久久久久久尤物| 亚洲欧美日韩国产手机在线| 国产亚洲一区二区三区成人| 午夜成人性爽爽免费视频| 天堂V亚洲国产V第一次| 成人毛片100免费观看| 五月天丁香婷婷亚洲欧洲国产| 看免费的无码区特aa毛片| 南汇区| 色猫咪av在线网址| 尤物yw193无码点击进入| 久久亚洲精品日本波多野结衣| 亚洲av午夜福利精品一区二区 | 国产成人综合久久亚洲av| 亚洲人成小说网站色在线|