def __init__(self, bgimg): BgState.__init__(self, bgimg) txt = TextLine("Press any key to start", fontsize=15) pos = (utils.center(txt.get_size(), (800, 600))[0], 590 - txt.get_size()[1]) self.background.blit(txt, pos) self.upd = 1
def __init__(self, bgimg=None): BgState.__init__(self, bgimg) pygame.mouse.set_visible(1) #background of message size = (250, 100) msg_bg = StaticWidget(size) draw_border(msg_bg) pos = utils.center(size, (800, 600)) msg_bg.set_pos(pos) self.msg_bg = msg_bg msg_pos = pos txt_img = TextLine("Paused", fontsize=30) pos = (utils.center(txt_img.get_size(), size)) msg_bg.blit(txt_img, pos) msg_bg.draw(self.background) #continue/cancel buttons btn = FixedTextButton((100, 24), "Abort Game", callback=self.to_menu) btn.set_pos((msg_pos[0], msg_pos[1] + size[1] + 4)) btn.draw_border() self.add_button("cancel", btn) btn.draw(self.background) btn = FixedTextButton((100, 24), "Continue", callback=self.quit) btn.set_pos((msg_pos[0] + size[0] - 100, msg_pos[1] + size[1] + 4)) btn.draw_border() self.add_button("start", btn) btn.draw(self.background) self.upd = 1
def __init__(self, bgimg=None): BgState.__init__(self, bgimg) #background of message size = (300, 200) msg_bg = StaticWidget(size) #msg_bg.fill((0,0,128)) draw_border(msg_bg) pos = utils.center(size, (800, 600)) msg_bg.set_pos(pos) self.msg_bg = msg_bg msg_pos = pos txt = "New game" txt_img = TextLine(txt, fontsize=30) pos = (utils.center(txt_img.get_size(), size)[0], 10) msg_bg.blit(txt_img, pos) #message txt = "Enter name:" txt_img = TextLine(txt, fontsize=15) pos = (10, 80) msg_bg.blit(txt_img, pos) self.entry = TextEntry(size=(130, 22)) self.entry.max = 10 self.entry.set_pos( (msg_pos[0] + txt_img.get_size()[0] + 20, msg_pos[1] + 75)) txt = "Note: Any previous savegame will be " txt_img = TextLine(txt, fontsize=12) pos = (10, 150) msg_bg.blit(txt_img, pos) txt = "overwritten!" txt_img = TextLine(txt, fontsize=12) pos = (10, pos[1] + 18) msg_bg.blit(txt_img, pos) #start/cancel buttons btn = FixedTextButton((100, 24), "Cancel", callback=self.quit) btn.set_pos((msg_pos[0], msg_pos[1] + size[1] + 2)) btn.draw_border() self.add_button("cancel", btn) btn = FixedTextButton((100, 24), "Start!", callback=self.submit) btn.set_pos((msg_pos[0] + size[0] - 100, msg_pos[1] + size[1] + 2)) btn.draw_border() self.add_button("start", btn) self.add_textentry_events(self.entry) self.add_kbevent(KEYDOWN, K_ESCAPE, self.quit) self.redraw()
def restart(self): Game.restart(self) self.lives = 0 self.score = 0 self.lives_img.blit(pygame.Surface((32, 32)), (0, 0)) self.lives_img.blit(GFX["pacdog-img"], (0, 0)) self.lives_img.blit(TextLine("x" + str(self.lives)), (32, 10)) self.set_active() for c in self.catlist: c.ai = CatAi(c) c.ai.setup(self.map, self.doglist) self.upd = 1
def __init__(self, img=None, lost=False): img = pygame.Surface((800, 600)) BgState.__init__(self, img) self.game_bg = img.copy() pygame.mouse.set_visible(1) #background of message size = (300, 300) msg_bg = StaticWidget(size) #draw_border(msg_bg) pos = utils.center(size, (800, 600)) msg_bg.set_pos(pos) self.msg_bg = msg_bg msg_pos = pos #message img = GFX["win"] if lost: img = GFX["lose"] pos = (utils.center(img.get_size(), size)[0], 1) msg_bg.blit(img, pos) score = SAVE["cur"]["score"] + 100 * SAVE["cur"]["lives"] txt = "Score: " + str(score) txt_img = TextLine(txt, fontsize=20) pos = (utils.center(txt_img.get_size(), size)[0], size[1] - 60) msg_bg.blit(txt_img, pos) scr_list = ScoreList() scr_list.max = 10 scr_list.set_list(SAVE["overhigh"]) #add highscore entry if scr_list.check(score): txt = "New Highscore!" txt_img = TextLine(txt, fontsize=20) pos = (utils.center(txt_img.get_size(), size)[0], size[1] - 30) msg_bg.blit(txt_img, pos) scr_list.max = 10 scr_list.add(SAVE["cur"]["name"], score, SAVE["cur"]["lives"]) SAVE["overhigh"] = scr_list.get_list() save_data() btn = FixedTextButton((120, 25), "Exit", callback=self.quit) btn.draw_border() btn.set_pos((msg_pos[0], msg_pos[1] + size[1] + 4)) self.add_button("continue", btn) btn = FixedTextButton((120, 25), "Highscore", callback=self.to_highscore) btn.draw_border() btn.set_pos((msg_pos[0] + size[0] - 120, msg_pos[1] + size[1] + 4)) self.add_button("highscore", btn) SAVE["cur"] = {} #delete savegame save_data() self.redraw()
def __init__(self, bgimg=None, mapname="", folder="", pacmode=False): BgState.__init__(self, bgimg) pygame.mouse.set_visible(1) self.pacmode = pacmode self.mapname = mapname self.folder = folder #background of message size = (250, 100) msg_bg = StaticWidget(size) draw_border(msg_bg) pos = utils.center(size, (800, 600)) msg_bg.set_pos(pos) self.msg_bg = msg_bg msg_pos = pos txt_img = TextLine("You lose.", fontsize=30) pos = (utils.center(txt_img.get_size(), size)) msg_bg.blit(txt_img, pos) msg_bg.draw(self.background) #continue/cancel buttons btn = FixedTextButton((100, 24), "Abort Game", callback=self.quit) btn.set_pos((msg_pos[0], msg_pos[1] + size[1] + 4)) btn.draw_border() self.add_button("cancel", btn) btn.draw(self.background) btn = FixedTextButton((100, 24), "Try Again", callback=self.tryagain) btn.set_pos((msg_pos[0] + size[0] - 100, msg_pos[1] + size[1] + 4)) btn.draw_border() self.add_button("start", btn) btn.draw(self.background) self.add_kbevent(KEYDOWN, K_ESCAPE, self.quit) self.upd = 1
def __init__(self, bgimg=None): BgState.__init__(self, bgimg) bgsize = (300, 400) bgimg = pygame.Surface(bgsize) draw_border(bgimg, width=4) self.background.blit(bgimg, (250, 100)) txt = TextLine("Highscore", fontsize=30) pos = (utils.center(txt.get_size(), (800, 600))[0], 120) self.background.blit(txt, pos) x, y = 275, pos[1] + txt.get_size()[1] + 40 tmp = SAVE["overhigh"][:] tmp.reverse() for i in tmp: name, score, lives = i self.background.blit(TextLine(name), (x, y)) self.background.blit(TextLine(str(score)), (x + 150, y)) self.background.blit(TextLine(str(lives)), (x + 240, y)) y += 25 img = pygame.transform.scale(GFX["ghost"], (19, 19)) icon_pos = (x + 235, pos[1] + 55) self.background.blit(img, icon_pos) img = pygame.transform.scale(GFX["catnip"], (19, 19)) icon_pos = (x + 168, pos[1] + 55) self.background.blit(img, icon_pos) btn = FixedTextButton((200, 25), "Back to Menu", callback=self.quit) btn.draw_border() x = utils.center(btn.get_size(), (800, 600))[0] btn.set_pos((x, 510)) btn.draw(self.background) self.add_button("back", btn) self.add_kbevent(KEYDOWN, K_ESCAPE, self.quit) self.upd = 1
def __init__(self, img, mapname, score, custom=False, name="", pacmode=False, win=True): BgState.__init__(self, img) score_str = str(score) #background of message size = (300, 200) msg_bg = StaticWidget(size) #msg_bg.fill((0,0,128)) draw_border(msg_bg) pos = utils.center(size, (800, 600)) msg_bg.set_pos(pos) self.msg_bg = msg_bg msg_pos = pos #message if win: txt = "Map completed!" else: txt = "You were caught!" txt_img = TextLine(txt, fontsize=30) pos = (utils.center(txt_img.get_size(), size)[0], 10) msg_bg.blit(txt_img, pos) txt = "Score: " + score_str txt_img = TextLine(txt, fontsize=20) pos = (utils.center(txt_img.get_size(), size)[0], 60) msg_bg.blit(txt_img, pos) #check if highscore type = "camphigh" if custom: type = "custhigh" if pacmode: type = "pachighca" if custom: type = "pachighcu" scr_list = [] if SAVE[type].has_key(mapname): scr_list = SAVE[type][mapname] else: SAVE[type][mapname] = [] self.type = type self.mapname = mapname self.score = score self.scr_list = ScoreList() self.scr_list.set_list(scr_list) self.entry = None #add highscore entry if self.scr_list.check(score): txt = "New Highscore!" txt_img = TextLine(txt, fontsize=20) pos = (utils.center(txt_img.get_size(), size)[0], 120) msg_bg.blit(txt_img, pos) txt = "Enter name:" txt_img = TextLine(txt, fontsize=20) pos = (10, 150) msg_bg.blit(txt_img, pos) self.entry = TextEntry(text=name, size=(130, 22)) self.entry.max = 10 self.entry.set_pos( (msg_pos[0] + txt_img.get_size()[0] + 20, msg_pos[1] + 150)) self.add_textentry_events(self.entry) btn = FixedTextButton((120, 25), "Continue", callback=self.submit) btn.draw_border() btn.set_pos((msg_pos[0] + size[0] - 120, msg_pos[1] + size[1] + 4)) self.add_button("continue", btn) self.add_kbevent(KEYDOWN, K_RETURN, self.quit) self.redraw() #keys self.add_kbevent(KEYDOWN, K_ESCAPE, self.quit)
def restart(self): mapname = self.mapname folder = self.folder test = self.test self.upd = 1 self.fps = 50 self.win = False self.lost = False self.win_shown = False self.started = False self.custom = not folder == "lvl" self.screen = pygame.display.get_surface() self.background = pygame.Surface(self.screen.get_size()) pygame.mouse.set_visible(False) self.cataway_timer = utils.Timer(100) self.cataway_timer.deactivate() #load map self.map = Map(mapname, edit=False, folder=folder) self.map_pos = utils.center(self.map.get_size(), self.screen.get_size()) self.background.blit(self.map, self.map_pos) pygame.display.flip() #setup blocking spritegroup self.walls = pygame.sprite.Group() block = self.map.get_blocking() size = (32, 32) for b in block: pos = (b[0] * 32 + self.map_pos[0], b[1] * 32 + self.map_pos[1]) s = pygame.sprite.Sprite() s.rect = pygame.Rect(pos, size) self.walls.add(s) #catnip pellets catnip = [] self.catnip_rects = [] for p in self.map.pellets: c = CatNip(p) c.set_pos( (p[0] * 32 + self.map_pos[0], p[1] * 32 + self.map_pos[1])) catnip.append(c) self.catnip_rects.append(c.rect) self.catnip = pygame.sprite.Group(catnip) #cataway cataway = [] for c in self.map.cataway: x = CatAway(c) x.set_pos( (c[0] * 32 + self.map_pos[0], c[1] * 32 + self.map_pos[1])) cataway.append(x) self.cataway = pygame.sprite.Group(cataway) #catghosts cats = [] for g in self.map.ghost_start: c = Cat(len(cats)) c.set_pos( (g[0] * 32 + self.map_pos[0], g[1] * 32 + self.map_pos[1])) cats.append(c) self.catlist = cats self.cats = pygame.sprite.Group(cats) self.active_cat = 0 self.set_active(0) #pacdogs dogs = [] self.doglist = [] for p in self.map.pac_start: d = PacDog() d.set_pos( (p[0] * 32 + self.map_pos[0], p[1] * 32 + self.map_pos[1])) d.ai.setup(self.map, self.catlist) self.doglist.append(d) dogs.append(d) self.dogs = pygame.sprite.Group(dogs) #player stats self.score = self.map.get_score() #score self.score_img = Score(self.score) self.lives_img = StaticWidget((60, 32)) self.lives_img.blit(GFX["ghost"], (0, 0)) self.lives_img.blit(TextLine("x" + str(self.lives)), (32, 10)) self.lives_img.set_pos((800 - self.lives_img.get_size()[0], 0)) self.level_img = TextLine(self.mapname[:-4].upper()) x = utils.center(self.level_img.get_size(), (800, 600))[0] self.level_img.set_pos((x, 2)) #setup spritegroups self.sprites = pygame.sprite.RenderUpdates() self.sprites.add(self.cats) self.sprites.add(self.dogs) self.static_sprites = pygame.sprite.Group() self.static_sprites.add(self.catnip) self.static_sprites.add(self.cataway) self.start_paused = False
class Game(State): def __init__(self, mapname="level01.map", folder="lvl", test=False, camp=False, pacmode=False): State.__init__(self) self.mapname = mapname self.fps = 60 self.folder = folder self.test = test self.lives = 3 self.camp = camp self.start_paused = False if self.camp: self.lives = SAVE["cur"]["lives"] #lives self.restart() #add keyboard events self.add_kbevent(KEYDOWN, K_ESCAPE, self.to_pause) if not pacmode: #movement self.add_kbevent(KEYDOWN, K_UP, self.cat_go, vel=(0, -1)) self.add_kbevent(KEYDOWN, K_DOWN, self.cat_go, vel=(0, 1)) self.add_kbevent(KEYDOWN, K_LEFT, self.cat_go, vel=(-1, 0)) self.add_kbevent(KEYDOWN, K_RIGHT, self.cat_go, vel=(1, 0)) #ghost selection self.add_kbevent(KEYDOWN, K_1, self.set_active, nr=0) self.add_kbevent(KEYDOWN, K_2, self.set_active, nr=1) self.add_kbevent(KEYDOWN, K_3, self.set_active, nr=2) self.add_kbevent(KEYDOWN, K_4, self.set_active, nr=3) self.add_kbevent(KEYDOWN, K_5, self.set_active, nr=4) def to_pause(self): if self.test: self.quit() else: self.pause() self.next = Pause(self.screen) self.quit() def main_start(self): pygame.mouse.set_visible(0) self.screen.blit(self.background, (0, 0)) pygame.display.flip() self.started = True if not self.start_paused: self.start_pause() k = TMP["last_key"] if k and k in TMP["play_keys"]: ev = pygame.event.Event(KEYDOWN, { "key": TMP["last_key"], "mod": 0 }) pygame.event.post(ev) TMP["last_key"] = None def cat_go(self, vel): if self.active_cat < len(self.catlist) and \ self.catlist[self.active_cat] is not None: self.ent_go(self.catlist[self.active_cat], vel) def ent_go(self, ent, vel): old_pos = ent.pos old_vel = ent.vel success = False ent.go(vel) ent.update() off_tile = (old_pos[0] - self.map_pos[0]) % 32 or \ (old_pos[1] - self.map_pos[1]) % 32 if off_tile or pygame.sprite.spritecollideany(ent, self.walls): ent.next_vel = vel ent.go((old_vel[0] / ent.speed, old_vel[1] / ent.speed)) else: ent.next_vel = None ent.set_movedir(ent.vel2dir(vel)) success = True ent.set_pos(old_pos) return success def update_other(self): #move cat into last requested dir if possible if not self.start_paused: self.start_pause() for c in self.cats: self.update_ent(c) for d in self.dogs: self.update_ent(d) #CATAWAY-mode if self.cataway_timer.update(): self.cataway_timer.deactivate() for d in self.dogs: d.set_scary(0) for c in self.cats: c.set_scary(1) #TODO: Stop whatever else is going on when cataway mode is running if len(self.dogs.sprites()) == 0: self.show_win() if len(self.cats.sprites())==0 or \ (len(self.catnip.sprites())==0 and \ len(self.cataway.sprites())==0): self.show_lose() def show_lose(self, pacmode=False): if self.test or self.lost: self.quit() return if self.lives > 0: self.lives -= 1 if self.camp: SAVE["cur"]["lives"] = self.lives self.restart() elif self.camp: self.lost = True self.next = GameOver(self.screen, lost=True) self.pause() self.quit() elif not self.camp: self.next = TryAgain(self.screen, self.mapname, self.folder, pacmode=pacmode) self.quit() def add_score(self, val): self.score = self.score - val self.upd = 1 def show_win(self, pacmode=False, win=True): if self.test: self.quit() return if self.win_shown: self.quit() return self.win = True self.win_shown = True name = "" if self.camp: name = SAVE["cur"]["name"] self.pause() self.next = ShowWin(self.screen, self.mapname, self.score, self.custom, name, pacmode, win) self.quit() def update_ai(self, ent): #dog-AI dir = None epos = (ent.pos[0] - self.map_pos[0], ent.pos[1] - self.map_pos[1]) if epos[0] % 32 or epos[1] % 32: return cx = epos[0] / 32 cy = epos[1] / 32 vel = ent.ai.get_vel() if vel is None: vel = random.choice([(0, 1), (1, 0), (0, -1), (-1, 0)]) self.ent_go(ent, vel) def update_ent(self, ent): epos = (ent.pos[0] - self.map_pos[0], ent.pos[1] - self.map_pos[1]) if not epos[0] % 32 and not epos[1] % 32: cx = epos[0] / 32 cy = epos[1] / 32 ent.coord = (cx, cy) if ent.ai: self.update_ai(ent) if ent.next_vel: self.ent_go(ent, ent.next_vel) def set_active(self, nr=None): if nr is None: for i in xrange(len(self.catlist)): if self.catlist[i] is not None: nr = i if nr is not None and nr < len( self.catlist) and self.catlist[nr] is not None: if self.catlist[self.active_cat] is not None: self.catlist[self.active_cat].deselect() self.active_cat = nr self.catlist[nr].select() def check_collisions(self): self.sprites.update() #check if cats or dogs collide with wall coll = pygame.sprite.groupcollide(self.sprites, self.walls, False, False) for s in coll.keys(): x, y = s.pos n = s.speed v = s.vel if v[1] < 0: #moving up s.set_pos((x, y + n)) if v[1] > 0: #moving down s.set_pos((x, y - n)) if v[0] < 0: #moving left s.set_pos((x + n, y)) if v[0] > 0: #moving right s.set_pos((x - n, y)) #check if dogs collide with cats coll = pygame.sprite.groupcollide(self.dogs, self.cats, False, False) if self.cataway_timer.active: for k in coll.keys(): #in CATAWAY-mode dog kills cat cats = coll[k] for c in cats: c.kill() self.catlist[c.nr] = None self.set_active() else: for dog in coll.keys(): #in NORMAL mode, cat kills dog if dog in self.doglist: self.doglist.remove(dog) dog.kill() #check if dogs collide with catnip coll = pygame.sprite.groupcollide(self.dogs, self.catnip, False, True) if coll: self.add_score(len(coll) * CATNIP) for c in coll.values(): for nip in c: if nip.coord in self.map.pellets: self.map.pellets.remove(nip.coord) #check if dogs collide with cataway coll = pygame.sprite.groupcollide(self.dogs, self.cataway, False, True) if coll: self.add_score(len(coll) * CATAWAY) self.cataway_timer.activate() for d in self.dogs: d.set_scary(1) for c in self.cats: c.set_scary(0) for c in coll.values(): for nip in c: if nip.coord in self.map.cataway: self.map.cataway.remove(nip.coord) def update_screen(self): self.screen.blit(self.background, (0, 0)) self.static_sprites.draw(self.screen) changed = self.sprites.draw(self.screen) if self.upd: self.upd = 0 self.score_img.set_text(self.score) self.lives_img.draw(self.background) self.level_img.draw(self.background) self.score_img.draw(self.background) pygame.display.flip() def restart(self): mapname = self.mapname folder = self.folder test = self.test self.upd = 1 self.fps = 50 self.win = False self.lost = False self.win_shown = False self.started = False self.custom = not folder == "lvl" self.screen = pygame.display.get_surface() self.background = pygame.Surface(self.screen.get_size()) pygame.mouse.set_visible(False) self.cataway_timer = utils.Timer(100) self.cataway_timer.deactivate() #load map self.map = Map(mapname, edit=False, folder=folder) self.map_pos = utils.center(self.map.get_size(), self.screen.get_size()) self.background.blit(self.map, self.map_pos) pygame.display.flip() #setup blocking spritegroup self.walls = pygame.sprite.Group() block = self.map.get_blocking() size = (32, 32) for b in block: pos = (b[0] * 32 + self.map_pos[0], b[1] * 32 + self.map_pos[1]) s = pygame.sprite.Sprite() s.rect = pygame.Rect(pos, size) self.walls.add(s) #catnip pellets catnip = [] self.catnip_rects = [] for p in self.map.pellets: c = CatNip(p) c.set_pos( (p[0] * 32 + self.map_pos[0], p[1] * 32 + self.map_pos[1])) catnip.append(c) self.catnip_rects.append(c.rect) self.catnip = pygame.sprite.Group(catnip) #cataway cataway = [] for c in self.map.cataway: x = CatAway(c) x.set_pos( (c[0] * 32 + self.map_pos[0], c[1] * 32 + self.map_pos[1])) cataway.append(x) self.cataway = pygame.sprite.Group(cataway) #catghosts cats = [] for g in self.map.ghost_start: c = Cat(len(cats)) c.set_pos( (g[0] * 32 + self.map_pos[0], g[1] * 32 + self.map_pos[1])) cats.append(c) self.catlist = cats self.cats = pygame.sprite.Group(cats) self.active_cat = 0 self.set_active(0) #pacdogs dogs = [] self.doglist = [] for p in self.map.pac_start: d = PacDog() d.set_pos( (p[0] * 32 + self.map_pos[0], p[1] * 32 + self.map_pos[1])) d.ai.setup(self.map, self.catlist) self.doglist.append(d) dogs.append(d) self.dogs = pygame.sprite.Group(dogs) #player stats self.score = self.map.get_score() #score self.score_img = Score(self.score) self.lives_img = StaticWidget((60, 32)) self.lives_img.blit(GFX["ghost"], (0, 0)) self.lives_img.blit(TextLine("x" + str(self.lives)), (32, 10)) self.lives_img.set_pos((800 - self.lives_img.get_size()[0], 0)) self.level_img = TextLine(self.mapname[:-4].upper()) x = utils.center(self.level_img.get_size(), (800, 600))[0] self.level_img.set_pos((x, 2)) #setup spritegroups self.sprites = pygame.sprite.RenderUpdates() self.sprites.add(self.cats) self.sprites.add(self.dogs) self.static_sprites = pygame.sprite.Group() self.static_sprites.add(self.catnip) self.static_sprites.add(self.cataway) self.start_paused = False def start_pause(self): self.start_paused = True self.update_screen() self.pause() self.next = StartPause(self.screen) self.quit()