# 主方法 import sys from wg import * screen = pygame.display.set_mode((800, 800)) pygame.display.set_caption("虫吃草") clock = pygame.time.Clock() worms = [] grasses = [] c = 0 grass_grow_period = 5 def game_init(): for i in range(20): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) warm = Worm(r, g, b) worms.append(warm) for n in range(10): grass = Grass() grasses.append(grass) def world_test(worm): for grass in grasses: if worm.head_hit_test(grass.xg, grass.yg, grass.thick1, grass.thick1): return grass return None def game_frame(): # process my world for worm in worms: if worm.is_death(): worms.remove(worm) continue worm.move(800, 800) grass = world_test(worm) if grass is not None: worm.eat(grass) global c if c > grass_grow_period: for a in range(10): grass = Grass() grasses.append(grass) c = 0 c = c + 1 for grass in grasses: if grass.is_death(): grasses.remove(grass) continue # draw objects for worm in worms: worm.draw(screen) for grass in grasses: grass.draw(screen) game_init() while True: clock.tick(10) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.fill([255, 255, 255]) game_frame() pygame.display.update()
# worm 和 grass 类
import pygame import random class Worm: def __init__(self, r, g, b): self.energy = 100 self.thick = 10 self.x0 = 0 self.y0 = 0 self.x1 = self.x0 + self.thick self.y1 = self.y0 self.x2 = self.x0 + self.thick * 2 self.y2 = self.y0 self.color0 = [r, g, b, 255] self.color1 = [r, g, b, 200] self.color2 = [r, g, b, 100] self.last_direction = -100 def move(self, w, h): # process energy if self.energy <= 0: pass # process bound self.x2 = self.x1 self.y2 = self.y1 self.x1 = self.x0 self.y1 = self.y0 direction = 0 while True: # 让小虫不能回头 direction = random.randint(-2, 2) if direction != 0 and self.last_direction + direction != 0: break if direction == 1: self.x0 = self.x0 + self.thick elif direction == -1: self.x0 = self.x0 - self.thick elif direction == 2: self.y0 = self.y0 + self.thick elif direction == -2: self.y0 = self.y0 - self.thick self.last_direction = direction if self.x0 < 0: self.x0 = 0 elif self.y0 < 0: self.y0 = 0 elif self.x0 > 800: self.x0 = 800 elif self.y0 > 800: self.y0 = 800 self.energy = self.energy - 1 def is_death(self): return self.energy <= 0 # 碰撞检测,判断小虫的四个顶点是否在草内部,如果在 energy plus 10 def head_hit_test(self, x, y, w, h): o1_x = self.x0 + (self.thick / 2) o2_x = x + w / 2 len_x = abs(o1_x - o2_x) o1_y = self.y0 + (self.thick / 2) o2_y = y + h / 2 len_y = abs(o1_y - o2_y) centre_x = abs((self.thick + w) / 2) centre_y = abs((self.thick + h) / 2) if len_x < centre_x and len_y < centre_y: return True return False def eat(self, grass): grass_energy = grass.reduce() self.energy = self.energy + grass_energy def draw(self, screen): pygame.draw.rect(screen, self.color0, [self.x0, self.y0, self.thick, self.thick], 0) pygame.draw.rect(screen, self.color1, [self.x1, self.y1, self.thick, self.thick], 0) pygame.draw.rect(screen, self.color2, [self.x2, self.y2, self.thick, self.thick], 0) class Grass: def __init__(self): self.color = [0, 0, 0] self.thick1 = 10 self.xg = random.randint(0, 800) self.yg = random.randint(0, 800) self.energy = 40 def reduce(self): old_energy = self.energy self.energy = 0 return old_energy def is_death(self): return self.energy <= 0 def draw(self, screen): pygame.draw.rect(screen, self.color, [self.xg, self.yg, self.thick1, self.thick1], 0)