From 246326991a1fe6daf5f5374a1adb8a282b3b99e5 Mon Sep 17 00:00:00 2001 From: marblexu <314299052@qq.com> Date: Sun, 15 Sep 2019 19:38:35 +0800 Subject: [PATCH] add Lawn mower --- resources/graphics/Screen/car.png | Bin 0 -> 1386 bytes source/component/plant.py | 30 ++++++++++++++++++++++++++++++ source/constants.py | 1 + source/state/level.py | 26 +++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 resources/graphics/Screen/car.png diff --git a/resources/graphics/Screen/car.png b/resources/graphics/Screen/car.png new file mode 100644 index 0000000000000000000000000000000000000000..037e641535082cfae249f0063240b305a2618c62 GIT binary patch literal 1386 zcmV-w1(o`VP) z46dcFBm@XhOi)k=2t+(YFbohA009^X2(G*Wf&c&j0d!JMQvg8b*k%9#1jk84K~z}7 zrI*=y+At7>tC1#_WLcXnFMa<9?HL&gq=kNkbCHmY_T%)eXOMWr|CHg3{NCkG7wEX-4 zlw3h=ax@OV+6Pr`NQ;pkq1u$+Gwr$hR$7= z$)31TGT9pPlbM2Zte~prG41QRYgd{BC#Y!(a$bSx`C-!xOTbj}=7$yI6XkKt36)|u z&UhI`O$Vq4a}g3+@$G&ex}ypE)oL(c#<4MM9NU(F9IefrF z6fD!X+to&C)az~d~hmKB@_JCI-2;WT@wsoghg|61CLTDVNzk?A800j0%> zE+s)HXFm`C2vPGmJ?q!Lh|lduW+R}^IYGznbg*+*44t!HIfJneJ>S6)I{s@3Sydvc za1SkM9AlYnAE81F431toN51EvHeYzxk~@^n+lyn`!Pcc=mgiA0xo}Audx~9MPmX;Y zLS(fb#0|_uP$kBFikG2|bFADtM^<4wui`YV!}q+Pw)e7W^d1Q9(OY5%(9e0!=Flg{ zwx5Q&Tq*82EAgnPmu1MGK7v?rYC<)Dta zIh^zp9Xjblx-1&%iSg$T@-;qxC|Amj<2>8aSFNIIU47TI+_h8AO^airs(r%g_ny2P(2gWyaU}sn3F!bMcEf89+q;?|)s?4KgR>Lz6NeSCHLt_xm5|U z;=L&0tfbCL;QXnfVq;s5{u07*qoM6N<$g25hv$^ZZW literal 0 HcmV?d00001 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)