def __init__(self): self.hp_bar = self.Bar(30, 100, 1, 10, 10, (250, 0, 0), GOL.get_go(Player), "hp", icon=r"images\ui\hp_icon.png") self.shield_bar = self.Bar(30, 100, 1, 50, 10, (0, 100, 250), GOL.get_go(Player), "shield_hp", icon=r"images\ui\shield_icon.png") self.shield_charge_bar = self.Bar(10, 100, 1, 80, 10, (0, 200, 250), GOL.get_go(Player), "shield_charge", icon=r"images\ui\energy_icon.png") self.shield_charge_bar.var_max_value = 60 * 5 self.base_hp = self.Bar(20, 100, 4, 100, 10, (250, 250, 0), GOL.get_go(Base), "hp", icon=r"images\ui\hq_icon.png")
def explode(self): """when destroyed""" self.explosion_time -= 1 if self.explosion_time > 0: self.screen.blit(self.explosion_image, self.rect) else: GOL.del_go(self)
def update(self): # while a solid bullet if not self.destroyed: # Move if self.dir == "UP": self.y -= self.speed self.rect.y = int(self.y) elif self.dir == "DOWN": self.y += self.speed self.rect.y = int(self.y) elif isinstance(self.dir, tuple): self.y += self.dir[0] * self.speed self.rect.y = int(self.y) self.x += self.dir[1] * self.speed self.rect.x = int(self.x) # Check if reach bottom or top if self.rect.top >= self.screen.get_rect().bottom: GOL.del_go(self) elif self.rect.bottom <= self.screen.get_rect().top: GOL.del_go(self) # Check if hit anything if isinstance(self.ship, Player): for alien in GOL.get_golist(Aliens): if self.rect.colliderect(alien.rect): if alien.spawned and not alien.destroyed: alien.take_dmg(self.dmg) self.destroyed = True elif isinstance(self.ship, Aliens): if GOL.get_go(Player): if self.rect.colliderect(GOL.get_go(Player).rect): GOL.get_go(Player).take_dmg(self.dmg) self.destroyed = True
def fire(self): if self.dmg_timer < 0: GOL.add_go( Projectile(self, "DOWN", self.dmg_speed, self.dmg, sprite=self.dmg_sprite, sprite_expl=r"images\alien\plasmaball_expl.png")) self.dmg_timer = self.dmg_rate else: self.dmg_timer -= 1
def take_dmg(self, dmg): if self.shield_hp > 0: self.shield_hp -= dmg self.shield_charge = 0 if self.shield_hp < 0: self.hp += self.shield_hp self.shield_hp = 0 else: self.hp -= dmg self.shield_charge = 0 if self.hp <= 0: self.destroyed = True GOL.add_go( Title("GAME OVER!", 60, self.expl_time * 3, color=(250, 50, 100)))
def update(self): if self.spawned: Ships.update(self) if not self.destroyed: # Movement self.y += self.speed self.rect.y = int(self.y) # Fire self.fire() # Check if reached bottom if self.rect.top == self.screen.get_rect().bottom: GOL.get_go(Base).take_dmg(self.hp) GOL.del_go(self) else: if self.spawn_timer <= 0: self.spawned = True else: self.spawn_timer -= 1
def update(self): # Sprite to draw selection. #1 -IF Destroyed check if self.destroyed == True: if self.expl_timer >= len(self.sprites_expl) * self.expl_time: self.sprite_index = 0 self.sprite = self.sprites_expl[self.sprite_index] elif self.expl_timer < (len(self.sprites_expl) - 1 - self.sprite_index) * self.expl_time: self.sprite_index += 1 self.sprite = self.sprites_expl[self.sprite_index] elif self.expl_timer <= 0: GOL.del_go(self) self.expl_timer -= 1 #2 -If Alive dmg check elif self.hp <= self.max_hp * (len(self.sprites) - 1 - self.sprite_index) / len(self.sprites): self.sprite_index += 1 self.sprite = self.sprites[self.sprite_index]
def draw(screen): # Reset screen screen.blit(space, (0, 0)) # Object drawing for object in GOL.get_golist(): object.draw() # Update and FPS font = pygame.font.SysFont("Arial MS", size=16) text = font.render(str(round(clock.get_fps(), 2)), False, (250, 250, 250)) screen.blit(text, (0, 0)) pygame.display.flip() clock.tick(60)
def test_list(struclist, gridsize, rounds, failisblank=True, failnochange=False): ''' Takes a list of structures, places each of them on the gameboard in the lower right quadrent, and tests it for x number of rounds. Returns a list of strucures that have passed ''' results = [] for structure in struclist: game = GOL(gridsize, gridsize) game.add_structure(structure, xoffset=int(gridsize / 2), yoffset=int(gridsize / 2)) result = game.test(rounds, failisblank=failisblank, failnochange=failnochange) if result == True: results.append(structure) return results
def test_iterations(structuresize, gridsize, rounds, failisblank=True, failnochange=False): ''' Generates a list of all possible structures based on structuresize (a sqare grid) tests them for rounds, and returns the ones that pass ''' teststructures = generate_binary_grid(structuresize) results = [] for structure in teststructures: game = GOL(gridsize, gridsize) game.add_structure(structure, xoffset=int(gridsize / 2), yoffset=int(gridsize / 2)) result = game.test(rounds, failisblank=failisblank, failnochange=failnochange) if result == True: results.append(structure) return results
def play_lvl(cls, lvl_num): lvl = cls.levels[str(lvl_num)] GOL.add_go(Title(lvl["title"], 100, 60 * lvl["delay"])) for spawn in lvl["spawns"]: if spawn["type"] == "alien1": GOL.add_go( Alien1(spawn["x"], timer=spawn["timer"] * 60 + lvl["delay"] * 60)) if spawn["type"] == "alien2": GOL.add_go( Alien2(spawn["x"], timer=spawn["timer"] * 60 + lvl["delay"] * 60))
def setup(): from main import screen # Delete old game objects GOL.del_golist() # Add new player, base, hp bars and reset levels GOL.add_go(Player()) GOL.add_go(Base()) GOL.add_go(PlayerInteface()) LM.reset() gamestart = False while not gamestart: # Start screen, fill and text screen.fill((0, 250, 0)) screen.blit(space, (0, 0)) pygame.font.init() font = pygame.font.SysFont("Tahoma MS", size=60) title = font.render("KILL ALL THE ALIENS!", True, (255, 215, 0)) rect = title.get_rect() rect.centerx = screen.get_rect().centerx rect.centery = screen.get_rect().top + 100 screen.blit(title, rect) font = pygame.font.SysFont("Arial MS", size=18) title = font.render("PRESS ANY KEY TO START", True, (250, 250, 0)) rect = title.get_rect() rect.centerx = screen.get_rect().centerx rect.centery = screen.get_rect().top + 400 screen.blit(title, rect) pygame.display.update() clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: gamestart = True
def update(self): # Check collision self.transparent = False for object in GOL.get_golist(): rect = getattr(object, "rect", "") if rect: if self.rect.colliderect(rect): self.transparent = True # Check variable # ! since a 0 height surface cant exist there is an IF statement if self.object and getattr(self.object, self.variable_name) > 0: self.var_height = getattr( self.object, self.variable_name) / self.var_max_value * ( self.height - self.thick * 2) self.s = pygame.Surface( (self.width - self.thick * 2, self.var_height)) self.s.fill(self.color) if self.transparent: self.s.set_alpha(100) self.bs.set_alpha(100) else: self.s.set_alpha(250) self.bs.set_alpha(250)
def lvl_ended(cls): for x in GOL.get_golist(): if isinstance(x, Aliens): return False return True
def gol_hash(bits): gol = GOL(WIDTH, HEIGHT, LIFE) i = 0 s = gol.getSize() w = gol.getWidth() for bit in bits: si = i % s gol.set(si % w, si / w, bit) i += 1 #PROCESS gol.printBoard() for i in xrange(STEPS): gol.step() gol.printBoard() #PULL DATA bits = [] w = gol.getWidth() for i in xrange(gol.getSize()): bits.append(gol.get(i % w, i / w)) return bits
# Python Glider # Glider simulating Conway's Game of Life or A New Kind of Science "computational universe" thesis import sys from gol import GOL if __name__ == "__main__": g = GOL() #setup glider g.fill([[0, 1], [1, 2], [2, 0], [2, 1], [2, 2]]) for e in range(int(sys.argv[1])): g.next() g.show()
# Python Glider # Glider simulating Conway's Game of Life or A New Kind of Science "computational universe" thesis import sys from gol import GOL if __name__ == "__main__": g = GOL() #setup glider g.fill([[0,1],[1,2],[2,0],[2,1],[2,2]]) for e in range(int(sys.argv[1])): g.next() g.show()
def update(self): self.time -= 1 if self.time <= 0: GOL.del_go(self)
def check_event(): for event in pygame.event.get(): # Exit check if event.type == pygame.QUIT: sys.exit() # Ship movement if GOL.get_go(Player): if event.type == pygame.KEYDOWN: if event.key == pygame.K_d or event.key == pygame.K_RIGHT: GOL.get_go(Player).moving_right = True if event.key == pygame.K_a or event.key == pygame.K_LEFT: GOL.get_go(Player).moving_left = True if event.key == pygame.K_w or event.key == pygame.K_UP: GOL.get_go(Player).moving_up = True if event.key == pygame.K_s or event.key == pygame.K_DOWN: GOL.get_go(Player).moving_down = True if event.key == pygame.K_SPACE: GOL.get_go(Player).firing = True elif event.type == pygame.KEYUP: if event.key == pygame.K_d or event.key == pygame.K_RIGHT: GOL.get_go(Player).moving_right = False if event.key == pygame.K_a or event.key == pygame.K_LEFT: GOL.get_go(Player).moving_left = False if event.key == pygame.K_w or event.key == pygame.K_UP: GOL.get_go(Player).moving_up = False if event.key == pygame.K_s or event.key == pygame.K_DOWN: GOL.get_go(Player).moving_down = False if event.key == pygame.K_SPACE: GOL.get_go(Player).firing = False
def update(): for object in GOL.get_golist(): object.update()
def check_collision(self): for alien in GOL.get_golist(Aliens): if self.rect.colliderect(alien.rect) and alien.destroyed == False: alien.destroyed = True self.take_dmg(alien.hp)
def fire(self): GOL.add_go( Projectile(self, "UP", self.dmg_speed, self.dmg, self.dmg_sprite))