1.游戏简介
zombie游戏规则, 玩家必须从僵尸中逃脱, 并寻找食物生存下去, 获得食物增加生命值, 被僵尸咬到减少生命值
2.游戏素材
3.游戏MyLibrary.py库源码
import sys
import pygame
import random
import math
import time
import datetime
from pygame.locals import *
def print_text(font, x, y, text, color=(255,255,255)):
imageText = font.render(text, True, color)
screen = pygame.display.get_surface()
screen.blit(imageText, (x,y))
class MySprite(pygame.sprite.Sprite):
def __init__(self, target):
pygame.sprite.Sprite.__init__(self)
self.master_image = None
self.old_frame = -1
self.frame = 0
self.frame_width = 1
self.frame_height = 1
self.first_frame = 0
self.last_frame = 0
self.columns = 1
self.last_time = 0
self.direction = 0
self.velocity = Point(0,0)
def get_x(self):
return self.rect.x
def set_x(self, value):
self.rect.x = value
#X = property(_getx,_setx)
def get_y(self):
return self.rect.y
def set_y(self, value):
self.rect.y = value
#Y = property(_gety,_sety)
def add_x(self, value):
self.rect.x += value
def add_y(self, value):
self.rect.y += value
def get_pos(self):
return self.rect.topleft
def set_pos(self, pos):
self.rect.topleft = pos
def load(self, filename, width, height, columns):
self.master_image = pygame.image.load(filename).convert_alpha()
self.image = self.master_image
self.frame_width = width
self.frame_height = height
self.rect = Rect(0,0,width,height)
self.columns = columns
rect = self.master_image.get_rect()
self.last_frame = (rect.width//width) * (rect.height//height) - 1
def update(self, current_time, rate=30):
if current_time > self.last_time + rate:
self.frame += 1
if self.frame > self.last_frame:
self.frame = self.first_frame
self.last_time = current_time
if self.frame != self.old_frame:
frame_x = (self.frame % self.columns) * self.frame_width
frame_y = (self.frame // self.columns) * self.frame_height
rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
self.image = self.master_image.subsurface(rect)
self.old_frame = self.frame
def __str__(self):
return str(self.frame)+","+str(self.first_frame)+","+ \
str(self.last_frame)+","+str(self.frame_width) + "," + \
str(self.frame_height)+","+str(self.columns)+","+ str(self.rect)
class Point:
def __init__(self, x, y):
self.move(x, y)
def move(self,x, y):
self.x = x
self.y = y
def reset(self):
self.move(0, 0)
4.游戏主程序源码
import itertools
import sys
import time
import datetime
import random
import math
import pygame
from pygame.locals import *
from MyLibrary import *
def calc_velocity(direction, vel=1.0):
velocity = Point(0,0)
if direction == 0:
velocity.y = vel
elif direction == 2:
velocity.x = -vel
elif direction == 4:
velocity.x = vel
elif direction == 6:
velocity.y = -vel
return velocity
def reverse_direction(sprite):
if sprite.direction == 0:
sprite.direction = 6
elif sprite.direction == 2:
sprite.direction = 4
elif sprite.direction == 4:
sprite.direction = 2
elif sprite.direction == 6:
sprite.direction = 0
pygame.init()
width = 600
height = 500
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Collision Demo")
font = pygame.font.Font(None,36)
timer = pygame.time.Clock()
player_group = pygame.sprite.Group()
zombie_group = pygame.sprite.Group()
health_group = pygame.sprite.Group()
player = MySprite(screen)
player.load("player.png", 32, 48, 4)
player.set_pos((80,80))
player.direction = 4
player_group.add(player)
#zombie_image = pygame.image.load("zombie_walk.png").convert_alpha()
for n in range(0, 10):
zombie = MySprite(screen)
zombie.load("zombie.png", 36, 120, 4)
zombie.set_pos((random.randint(0,width-zombie.frame_width), random.randint(0,height-zombie.frame_height)))
zombie.direction = random.randint(0, 3) * 2
zombie_group.add(zombie)
health = MySprite(screen)
health.load("food.png", 100, 100, 1)
health.set_pos((400,300))
health_group.add(health)
game_over = False
player_moving = False
player_health = 100
while True:
timer.tick(30)
ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
elif keys[K_UP] or keys[K_w]:
player.direction = 6
player_moving = True
elif keys[K_RIGHT] or keys[K_d]:
player.direction = 4
player_moving = True
elif keys[K_DOWN] or keys[K_s]:
player.direction = 0
player_moving = True
elif keys[K_LEFT] or keys[K_a]:
player.direction = 2
player_moving = True
else:
player_moving = False
if not game_over:
#玩家逻辑
player.first_frame = player.direction * player.columns / 2
player.last_frame = player.first_frame + player.columns - 1
if player.frame < player.first_frame:
player.frame = player.first_frame
if not player_moving:
player.frame = player.first_frame = player.last_frame
else:
player.velocity = calc_velocity(player.direction, 1.5)
player.velocity.x *= 1.5
player.velocity.y *= 1.5
player_group.update(ticks, 50)
if player_moving:
player.add_x(player.velocity.x)
player.add_y(player.velocity.y)
if player.get_x() < 0:
player.set_x(0)
elif player.get_x() > width-player.frame_width:
player.set_x(width-player.frame_width)
if player.get_y() < 0:
player.set_y(0)
elif player.get_y() > height-player.frame_height:
player.set_y(height-player.frame_height)
#僵尸逻辑
zombie_group.update(ticks, 50)
for z in zombie_group:
z.first_frame = z.direction * z.columns / 2
z.last_frame = z.first_frame + z.columns - 1
if z.frame < z.first_frame:
z.frame = z.first_frame
z.velocity = calc_velocity(z.direction)
z.add_x(z.velocity.x)
z.add_y(z.velocity.y)
if z.get_x() < 0 or z.get_x() > width-z.frame_width or z.get_y() < 0 or z.get_y() > height-z.frame_height:
reverse_direction(z)
#检查僵尸与玩家的冲突
attacker = None
attacker = pygame.sprite.spritecollideany(player, zombie_group)
if attacker != None:
if pygame.sprite.collide_rect_ratio(0.5)(player, attacker):
player_health -= 10
if attacker.get_x() < player.get_x():
attacker.add_x(-10)
elif attacker.get_x() > player.get_x():
attacker.add_x(10)
else:
attacker = None
#食物逻辑
health_group.update(ticks, 50)
#检查玩家与食物的冲突
if pygame.sprite.collide_rect_ratio(0.5)(player, health):
player_health += 10
if player_health > 100:
player_health = 100
if player_health <= 0:
player_health = 0
game_over = True
health.set_pos((random.randint(0,width-health.frame_width),random.randint(0,height-health.frame_height)))
screen.fill((50, 50, 100))
health_group.draw(screen)
zombie_group.draw(screen)
player_group.draw(screen)
pygame.draw.rect(screen, (50,150,50,180), Rect(200,470,player_health*2,25))
pygame.draw.rect(screen, (100,200,100,180), Rect(200,470,200,25), 2)
if game_over:
print_text(font, 200, 100, "GAME OVER")
pygame.display.update()
time.sleep(1.0/60)
5.游戏展示