diff --git a/resources/graphics/Screen/car.png b/resources/graphics/Screen/car.png new file mode 100644 index 0000000..037e641 Binary files /dev/null and b/resources/graphics/Screen/car.png differ diff --git a/source/component/plant.py b/source/component/plant.py index 8a3e099..a1d1e31 100644 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -4,6 +4,36 @@ import pygame as pg from .. import tool from .. import constants as c +class Car(pg.sprite.Sprite): + def __init__(self, x, y, map_y): + pg.sprite.Sprite.__init__(self) + + rect = tool.GFX[c.CAR].get_rect() + width, height = rect.w, rect.h + self.image = tool.get_image(tool.GFX[c.CAR], 0, 0, width, height) + self.rect = self.image.get_rect() + self.rect.x = x + self.rect.bottom = y + self.map_y = map_y + self.state = c.IDLE + self.dead = False + + def update(self, game_info): + self.current_time = game_info[c.CURRENT_TIME] + if self.state == c.IDLE: + pass + elif self.state == c.WALK: + self.rect.x += 4 + if self.rect.x > c.SCREEN_WIDTH: + self.dead = True + + def setWalk(self): + if self.state == c.IDLE: + self.state = c.WALK + + def draw(self, surface): + surface.blit(self.image, self.rect) + class Bullet(pg.sprite.Sprite): def __init__(self, x, start_y, dest_y, name, damage, ice): pg.sprite.Sprite.__init__(self) diff --git a/source/constants.py b/source/constants.py index 7c689eb..ee239c9 100644 --- a/source/constants.py +++ b/source/constants.py @@ -58,6 +58,7 @@ MAP_OFFSET_Y = 100 MENUBAR_BACKGROUND = 'ChooserBackground' #PLANT INFO +CAR = 'car' SUN = 'Sun' SUNFLOWER = 'SunFlower' PEASHOOTER = 'Peashooter' diff --git a/source/state/level.py b/source/state/level.py index 100d603..fd87966 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -32,6 +32,7 @@ class Level(tool.State): self.setupBackground() self.setupGroups() self.setupZombies() + self.setupCars() def loadMap(self): map_file = 'level_' + str(self.game_info[c.LEVEL_NUM]) + '.json' @@ -71,6 +72,12 @@ class Level(tool.State): self.zombie_start_time = 0 self.zombie_list.sort(key=takeTime) + def setupCars(self): + self.cars = [] + for i in range(self.map_y_len): + _, y = self.map.getMapGridPos(0, i) + self.cars.append(plant.Car(-35, y, i)) + def update(self, surface, current_time, mouse_pos, mouse_click): self.current_time = self.game_info[c.CURRENT_TIME] = current_time @@ -115,9 +122,13 @@ class Level(tool.State): if sun.checkCollision(mouse_pos[0], mouse_pos[1]): self.menubar.increaseSunValue(c.SUN_VALUE) + for car in self.cars: + car.update(self.game_info) + self.checkBulletCollisions() self.checkZombieCollisions() self.checkPlants() + self.checkCarCollisions() self.checkGameState() self.draw(surface) @@ -221,7 +232,18 @@ class Level(tool.State): if plant and zombie.state == c.WALK: zombie.setAttack(plant) plant.setAttacked() - + + def checkCarCollisions(self): + collided_func = pg.sprite.collide_circle_ratio(0.7) + for car in self.cars: + zombies = pg.sprite.spritecollide(car, self.zombie_groups[car.map_y], False, collided_func) + for zombie in zombies: + if zombie and zombie.state != c.DIE: + car.setWalk() + zombie.setDie() + if car.dead: + self.cars.remove(car) + def boomZombies(self, x, map_y): for i in range(self.map_y_len): if abs(i - map_y) > 1: @@ -308,6 +330,8 @@ class Level(tool.State): self.plant_groups[i].draw(surface) self.zombie_groups[i].draw(surface) self.bullet_groups[i].draw(surface) + for car in self.cars: + car.draw(surface) self.head_group.draw(surface) self.sun_group.draw(surface)