用Python开发植物大战僵尸游戏
时间: 2023-11-16 19:36:35 浏览: 224
植物大战僵尸python复刻 python3.9 pygame 2.0
《植物大战僵尸》是一款非常经典的塔防游戏,下面是一个简单的 Python 实现:
1. 导入必要的库
```python
import pygame
import random
```
2. 初始化 Pygame
```python
pygame.init()
```
3. 设置游戏窗口大小和标题
```python
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Plants vs. Zombies")
```
4. 加载游戏素材
```python
background = pygame.image.load("background.png")
sunflower_image = pygame.image.load("sunflower.png")
peashooter_image = pygame.image.load("peashooter.png")
zombie_image = pygame.image.load("zombie.png")
bullet_image = pygame.image.load("bullet.png")
sun_image = pygame.image.load("sun.png")
```
5. 定义太阳花类、豌豆射手类、子弹类、僵尸类和太阳类
```python
class Sunflower:
cost = 50
cooldown = 100
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 50
self.height = 50
self.image = sunflower_image
self.cooldown_timer = 0
self.sun_timer = 0
def update(self):
self.cooldown_timer += 1
self.sun_timer += 1
if self.sun_timer >= 1000:
self.sun_timer = 0
return Sun(self.x + self.width // 2, self.y + self.height // 2)
def draw(self):
screen.blit(self.image, (self.x, self.y))
class Peashooter:
cost = 100
cooldown = 30
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 50
self.height = 50
self.image = peashooter_image
self.cooldown_timer = 0
def update(self):
self.cooldown_timer += 1
if self.cooldown_timer >= Peashooter.cooldown:
self.cooldown_timer = 0
return Bullet(self.x + self.width, self.y + self.height // 2)
def draw(self):
screen.blit(self.image, (self.x, self.y))
class Bullet:
speed = 10
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 20
self.height = 20
self.image = bullet_image
def update(self):
self.x += Bullet.speed
def off_screen(self):
return self.x > width
def collision(self, zombie):
if (self.x + self.width > zombie.x and self.x < zombie.x + zombie.width) and (self.y + self.height > zombie.y and self.y < zombie.y + zombie.height):
return True
else:
return False
def draw(self):
screen.blit(self.image, (self.x, self.y))
class Zombie:
speed = 3
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 50
self.height = 100
self.image = zombie_image
def update(self):
self.x -= Zombie.speed
def off_screen(self):
return self.x < -self.width
def collision(self, plants):
for plant in plants:
if (self.x + self.width > plant.x and self.x < plant.x + plant.width) and (self.y + self.height > plant.y and self.y < plant.y + plant.height):
return True
return False
def draw(self):
screen.blit(self.image, (self.x, self.y))
class Sun:
speed = 1
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 50
self.height = 50
self.image = sun_image
self.timer = 0
def update(self):
self.timer += 1
self.y += Sun.speed
if self.timer >= 600:
return True
def collision(self, x, y):
if (x + self.width > self.x and x < self.x + self.width) and (y + self.height > self.y and y < self.y + self.height):
return True
else:
return False
def draw(self):
screen.blit(self.image, (self.x, self.y))
```
6. 创建游戏对象,并设置游戏循环
```python
sunflowers = []
peashooters = []
bullets = []
zombies = []
suns = []
sun_timer = 0
score = 0
clock = pygame.time.Clock()
while True:
clock.tick(30)
sun_timer += 1
if sun_timer >= 500:
sun_timer = 0
sunflowers.append(Sunflower(random.randint(0, width - 50), random.randint(0, height - 50)))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if y < 100:
if x < 100 and score >= Sunflower.cost:
sunflowers.append(Sunflower(random.randint(0, width - 50), random.randint(0, height - 50)))
score -= Sunflower.cost
elif x >= 100 and x < 200 and score >= Peashooter.cost:
peashooters.append(Peashooter(random.randint(0, width - 50), random.randint(100, height - 50)))
score -= Peashooter.cost
for sunflower in sunflowers:
sun = sunflower.update()
if sun:
suns.append(sun)
sunflower.draw()
for peashooter in peashooters:
bullet = peashooter.update()
if bullet:
bullets.append(bullet)
peashooter.draw()
for bullet in bullets:
bullet.update()
bullet.draw()
if bullet.off_screen():
bullets.remove(bullet)
for zombie in zombies:
if bullet.collision(zombie):
zombies.remove(zombie)
bullets.remove(bullet)
score += 10
for zombie in zombies:
zombie.update()
zombie.draw()
if zombie.off_screen():
zombies.remove(zombie)
score -= 10
if zombie.collision(sunflowers + peashooters):
zombies.remove(zombie)
for sun in suns:
if sun.update():
suns.remove(sun)
sun.draw()
if sun.collision(*pygame.mouse.get_pos()):
suns.remove(sun)
score += 25
screen.blit(background, (0, 0))
pygame.draw.rect(screen, (0, 0, 0), (0, 0, width, 100))
pygame.draw.rect(screen, (0, 255, 0), (0, 0, 100, 100))
pygame.draw.rect(screen, (0, 255, 0), (100, 0, 100, 100))
screen.blit(sunflower_image, (25, 25))
screen.blit(peashooter_image, (125, 25))
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), True, (255, 255, 255))
screen.blit(text, (10, 110))
if random.randint(0, 100) < 2:
zombies.append(Zombie(width, random.randint(100, height - 100)))
pygame.display.update()
```
7. 运行程序,开始游戏!
这是一个简单的《植物大战僵尸》游戏的 Python 实现,可以根据需要进行扩展和优化。
阅读全文