写一个简单可运行的植物大战僵尸python代码
时间: 2024-05-18 14:18:13 浏览: 158
以下是一个简单的植物大战僵尸的Python代码,需要用到Pygame模块:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口大小和标题
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
pygame.display.set_caption('植物大战僵尸')
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# 加载图片
background_img = pygame.image.load('background.png')
sunflower_img = pygame.image.load('sunflower.png')
peashooter_img = pygame.image.load('peashooter.png')
zombie_img = pygame.image.load('zombie.png')
bullet_img = pygame.image.load('bullet.png')
# 设置字体
font = pygame.font.SysFont('Arial', 24)
# 设置游戏状态
GAME_STATE_READY = 0
GAME_STATE_PLAYING = 1
GAME_STATE_GAME_OVER = 2
game_state = GAME_STATE_READY
# 设置游戏对象
SUNFLOWER = 0
PEASHOOTER = 1
ZOMBIE = 2
BULLET = 3
# 设置游戏对象属性
object_properties = [
{'name': 'Sunflower', 'cost': 50, 'health': 100, 'cooldown': 5, 'damage': 0},
{'name': 'Peashooter', 'cost': 100, 'health': 100, 'cooldown': 2, 'damage': 10},
{'name': 'Zombie', 'cost': 0, 'health': 100, 'cooldown': 1, 'damage': 20},
{'name': 'Bullet', 'cost': 0, 'health': 1, 'cooldown': 1, 'damage': 10}
]
# 设置游戏对象列表
objects = []
# 设置游戏对象生成的位置
sunflower_spawn_points = [(100, 100), (200, 100), (300, 100), (400, 100), (500, 100)]
peashooter_spawn_points = [(100, 200), (200, 200), (300, 200), (400, 200), (500, 200)]
zombie_spawn_points = [(700, 100), (700, 200), (700, 300), (700, 400), (700, 500)]
# 设置游戏对象生成的计时器
sunflower_timer = 0
peashooter_timer = 0
zombie_timer = 0
# 设置游戏得分
score = 0
# 设置太阳值
sun_value = 50
# 设置游戏主循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and game_state == GAME_STATE_PLAYING:
# 点击生成植物
pos = pygame.mouse.get_pos()
for i in range(len(sunflower_spawn_points)):
if abs(sunflower_spawn_points[i][0] - pos[0]) < 50 and abs(sunflower_spawn_points[i][1] - pos[1]) < 50:
if sun_value >= object_properties[SUNFLOWER]['cost']:
objects.append({'type': SUNFLOWER, 'x': sunflower_spawn_points[i][0], 'y': sunflower_spawn_points[i][1], 'health': object_properties[SUNFLOWER]['health'], 'cooldown': object_properties[SUNFLOWER]['cooldown'], 'damage': object_properties[SUNFLOWER]['damage']})
sun_value -= object_properties[SUNFLOWER]['cost']
for i in range(len(peashooter_spawn_points)):
if abs(peashooter_spawn_points[i][0] - pos[0]) < 50 and abs(peashooter_spawn_points[i][1] - pos[1]) < 50:
if sun_value >= object_properties[PEASHOOTER]['cost']:
objects.append({'type': PEASHOOTER, 'x': peashooter_spawn_points[i][0], 'y': peashooter_spawn_points[i][1], 'health': object_properties[PEASHOOTER]['health'], 'cooldown': object_properties[PEASHOOTER]['cooldown'], 'damage': object_properties[PEASHOOTER]['damage']})
sun_value -= object_properties[PEASHOOTER]['cost']
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and game_state == GAME_STATE_PLAYING:
# 空格键发射子弹
for i in range(len(objects)):
if objects[i]['type'] == PEASHOOTER and objects[i]['cooldown'] <= 0:
objects.append({'type': BULLET, 'x': objects[i]['x'], 'y': objects[i]['y'], 'health': object_properties[BULLET]['health'], 'cooldown': object_properties[BULLET]['cooldown'], 'damage': object_properties[BULLET]['damage']})
objects[i]['cooldown'] = object_properties[PEASHOOTER]['cooldown']
# 更新游戏状态
if game_state == GAME_STATE_READY:
# 显示游戏开始画面
screen.blit(background_img, (0, 0))
text = font.render('按任意键开始游戏', True, (255, 255, 255))
screen.blit(text, (WINDOW_WIDTH / 2 - text.get_width() / 2, WINDOW_HEIGHT / 2 - text.get_height() / 2))
pygame.display.update()
# 等待玩家按下任意键
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
game_state = GAME_STATE_PLAYING
break
if game_state == GAME_STATE_PLAYING:
break
elif game_state == GAME_STATE_PLAYING:
# 更新游戏对象
for i in range(len(objects)):
if objects[i]['type'] == SUNFLOWER:
if sunflower_timer <= 0:
sun_value += 25
sunflower_timer = object_properties[SUNFLOWER]['cooldown']
else:
sunflower_timer -= 1
elif objects[i]['type'] == PEASHOOTER:
if objects[i]['cooldown'] > 0:
objects[i]['cooldown'] -= 1
for j in range(len(objects)):
if objects[j]['type'] == ZOMBIE and abs(objects[i]['x'] - objects[j]['x']) < 50:
objects[j]['health'] -= objects[i]['damage']
if objects[j]['health'] <= 0:
objects.pop(j)
score += 10
break
elif objects[i]['type'] == ZOMBIE:
if zombie_timer <= 0:
objects.append({'type': ZOMBIE, 'x': zombie_spawn_points[random.randint(0, len(zombie_spawn_points) - 1)][0], 'y': zombie_spawn_points[random.randint(0, len(zombie_spawn_points) - 1)][1], 'health': object_properties[ZOMBIE]['health'], 'cooldown': object_properties[ZOMBIE]['cooldown'], 'damage': object_properties[ZOMBIE]['damage']})
zombie_timer = object_properties[ZOMBIE]['cooldown']
else:
zombie_timer -= 1
for j in range(len(objects)):
if objects[j]['type'] == SUNFLOWER and abs(objects[i]['x'] - objects[j]['x']) < 50:
objects[i]['cooldown'] -= 1
if objects[i]['cooldown'] <= 0:
objects[j]['health'] -= objects[i]['damage']
if objects[j]['health'] <= 0:
objects.pop(j)
score += 10
break
elif objects[i]['type'] == BULLET:
objects[i]['x'] += 5
if objects[i]['x'] >= WINDOW_WIDTH:
objects.pop(i)
break
for j in range(len(objects)):
if objects[j]['type'] == ZOMBIE and abs(objects[i]['x'] - objects[j]['x']) < 50:
objects[j]['health'] -= objects[i]['damage']
if objects[j]['health'] <= 0:
objects.pop(j)
score += 10
break
if objects[i]['health'] <= 0:
objects.pop(i)
break
# 显示游戏界面
screen.blit(background_img, (0, 0))
for i in range(len(objects)):
if objects[i]['type'] == SUNFLOWER:
screen.blit(sunflower_img, (objects[i]['x'], objects[i]['y']))
elif objects[i]['type'] == PEASHOOTER:
screen.blit(peashooter_img, (objects[i]['x'], objects[i]['y']))
elif objects[i]['type'] == ZOMBIE:
screen.blit(zombie_img, (objects[i]['x'], objects[i]['y']))
elif objects[i]['type'] == BULLET:
screen.blit(bullet_img, (objects[i]['x'], objects[i]['y']))
text = font.render('太阳值:' + str(sun_value), True, (255, 255, 255))
screen.blit(text, (10, 10))
text = font.render('得分:' + str(score), True, (255, 255, 255))
screen.blit(text, (10, 40))
pygame.display.update()
# 判断游戏结束
game_over = True
for i in range(len(objects)):
if objects[i]['type'] == ZOMBIE:
game_over = False
break
if game_over:
game_state = GAME_STATE_GAME_OVER
elif game_state == GAME_STATE_GAME_OVER:
# 显示游戏结束画面
screen.blit(background_img, (0, 0))
text = font.render('游戏结束', True, (255, 255, 255))
screen.blit(text, (WINDOW_WIDTH / 2 - text.get_width() / 2, WINDOW_HEIGHT / 2 - text.get_height() / 2))
text = font.render('得分:' + str(score), True, (255, 255, 255))
screen.blit(text, (WINDOW_WIDTH / 2 - text.get_width() / 2, WINDOW_HEIGHT / 2 - text.get_height() / 2 + 50))
pygame.display.update()
# 等待玩家按下任意键
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
game_state = GAME_STATE_READY
score = 0
sun_value = 50
objects = []
break
if game_state == GAME_STATE_READY:
break
```
注意:该代码仅用于学习和参考,不能用于商业用途。
阅读全文