def collision_ammo(self, ammo): if self.rect.x <= a.getX( ammo ) <= self.rect.x + self.rect.width and self.rect.y <= a.getY( ammo) <= self.rect.y + self.rect.height: return True return False
def save_load(file): # Ouvre le fichier .json with open(file) as f: data = json.load(f) # Récupère et applique les données aux attributs statiques du joueur et des ennemis for d in data["game"]: Player.set_score(d["player"]["score"]) Player.set_attack(d["player"]["attack"]) Player.set_max_health(d["player"]["health"]) Player.set_velocity(d["player"]["velocity"]) Player.set_NumSkin(d["player"]["skin"]) for n in d["player"]["OwnedSkin"]: Player.appendOwnedSkin(n) Player.set_bonus_lvl(d["upgrade"]["bonus"]) Player.set_degat_level(d["upgrade"]["degats"]) Player.set_vie_level(d["upgrade"]["vie"]) Player.set_vitesse_level(d["upgrade"]["vitesse"]) Player.set_bonus_lvl_left(d["upgrade"]["bonus_level_left"]) Enemy.set_attack(d["enemy"]["attack"]) Enemy.set_health(d["enemy"]["health"]) Enemy.set_velocity(d["enemy"]["velocity"]) Enemy.set_freq_tir(d["enemy"]["frequence_tir"]) AmmoEnemy.set_velocity(d["ammo_enemy"]["velocity"]) Ammo.set_velocity(d["ammo"]["velocity"])
def make_bullet(self): if GameState.game_time - self.player.time > self.player.speed: bullet = Ammo(WHITE, BULLET_SIZE) bullet.vector = -1 bullet.speed = 56 bullet.rect.x = self.player.rect.x + 28 bullet.rect.y = self.player.rect.y self.bullet_group.add(bullet) self.all_sprite_list.add(bullet) self.player.time = GameState.game_time GameState.shoot_bullet = False
def make_missile(self): if len(self.enemy_group): shoot = random.random() if shoot <= 0.05: shooter = random.choice([ enemy for enemy in self.enemy_group]) missile = Ammo(RED, MISSILE_SIZE) missile.vector = 1 missile.rect.x = shooter.rect.x + 15 missile.rect.y = shooter.rect.y + 40 missile.speed = 10 self.missile_group.add(missile) self.all_sprite_list.add(missile)
def __init__(self, posx, posy, direction, vertical_shot, ammo_type, scene): Ammo.__init__(self) self.scene = scene self.ammo_type = ammo_type if self.ammo_type == 'normal': self.image = graphics.load_image(SHOT, True) elif self.ammo_type == 'plasma': self.image = graphics.load_image(SHOT_PLASMA, True) self.rect = self.image.get_rect() self.rect.center = (posx, posy) self.radius = self.rect.width / 2 self.pos = vec(posx, posy) self.direction = direction self.vertical_shot = vertical_shot self.speed = 20
def save_json(file): # Ouvre le fichier .json with open(file, 'r') as f: data = json.load(f) # Change les valeurs des clés des données for d in data['game']: d["player"]["score"] = Player.get_score() d["player"]["health"] = Player.get_max_health() d["player"]["attack"] = Player.get_attack() d["player"]["velocity"] = Player.get_velocity() d["player"]["skin"] = Player.get_NumSkin() for n in Player.OwnedSkin: if n not in d["player"]["OwnedSkin"]: d["player"]["OwnedSkin"].append(n) d["upgrade"]["bonus"] = Player.get_bonus_lvl() d["upgrade"]["degats"] = Player.get_degat_level() d["upgrade"]["vie"] = Player.get_vie_level() d["upgrade"]["vitesse"] = Player.get_vitesse_level() d["upgrade"]["bonus_level_left"] = Player.get_bonus_lvl_left() d["enemy"]["health"] = Enemy.G_health() d["enemy"]["attack"] = Enemy.G_attack() d["enemy"]["velocity"] = Enemy.G_velocity() d["enemy"]["frequence_tir"] = Enemy.G_fre_tir() d["ammo_enemy"]["velocity"] = AmmoEnemy.get_velocity() d["ammo"]["velocity"] = Ammo.get_velocity() # Modifie le fichier .json avec les données ci-dessus with open(file, 'w') as f: json.dump(data, f, indent=2) print('Save Complete')
def __init__(self): #Initialize the game and create game resources pygame.init() pygame.display.set_caption("Alien Invaders") self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((1600, 900), pygame.FULLSCREEN) self.screen_rect = self.screen.get_rect() self.settings = Settings(self) self.bg_choice = 0 self.bg = Background(self.settings.background_choices[self.bg_choice], [0, 0]) self.stats = GameStats(self) self.ship = Ship(self) self.bullet = Bullet(self) self.ammo = Ammo(self) self.sb = Scoreboard(self) self.play_button = Button(self) self.fps = Get_FPS(self) self.cur_time = time.time() self.dt = time.time() - self.cur_time self.dt = 0.01666667 self.accum = 0.0 self.bullets = pygame.sprite.Group() self.aliens1 = pygame.sprite.Group() self.aliens2 = pygame.sprite.Group() self.cur_ammo = self.settings.bullet_limit self.spawn_alien2_count = self.settings.spawn_alien2 self.a1_kill_amount = 0
def createAmmoList(rows): ammos = [] lines = [] name = '' for r in rows: line = '' if len(r) < 28: if len(r) == 27: name = str(r[0]) r=r[1:] aType = r[0] damage = r[1] pValue = r[2] aDamage = r[3] fChance = r[4] c1 = r[5] c2 = r[6] c3 = r[7] c4 = r[8] c5 = r[9] c6 = r[10] for c in range(11): line += r[c] + ' | ' if len(line) > 1: ammos.append(Ammo(name, aType, damage, pValue, aDamage, fChance, c1, c2, c3, c4, c5, c6)) return ammos
def test_init(self): ammo = Ammo(3, 5) self.assertEqual(ammo.pos_x, 3) self.assertEqual(ammo.pos_y, 5) self.assertEqual(ammo.color, 'red') self.assertFalse(ammo.used) ammo = Ammo(-335, -596) self.assertEqual(ammo.pos_x, -335) self.assertEqual(ammo.pos_y, -596) self.assertEqual(ammo.color, 'red') self.assertFalse(ammo.used) ammo = Ammo(0, 0) self.assertEqual(ammo.pos_x, 0) self.assertEqual(ammo.pos_y, 0) self.assertEqual(ammo.color, 'red') self.assertFalse(ammo.used)
def check_keydown_events(event, board_settings, screen, ship, ammo): """handles event - key press""" if event.key == pygame.K_RIGHT: ship.moving_right = True elif event.key == pygame.K_LEFT: ship.moving_left = True elif event.key == pygame.K_SPACE: if len(ammo) < board_settings.bullets_onscreen_limit: new_ammo = Ammo(board_settings, screen, ship) ammo.add(new_ammo)
def __init__(self): #Initialize the game and create game resources pygame.init() pygame.display.set_caption("Alien Invaders") self.settings = Settings() self.screen = pygame.display.set_mode((1600, 900), pygame.FULLSCREEN) self.stats = GameStats(self) self.settings.screen_width = self.screen.get_rect().width self.settings.screen_height = self.screen.get_rect().height self.ship = Ship(self) self.bullet = Bullet(self) self.bullets = pygame.sprite.Group() self.aliens1 = pygame.sprite.Group() self.aliens2 = pygame.sprite.Group() self.scores = [] self.ammo = Ammo(self) self.cur_ammo = self.settings.bullet_limit self.spawn_alien2_count = 4 self.a1_kill_amount = 0 self.sb = Scoreboard(self) self.play_button = Button(self)
def super_pickup(self, indian, cowboy): if self.rect.colliderect(cowboy.rect): self.rect.x = 2200 self.kill() cowboy.ammo = [] cowboy.reload_img = False cowboy.reloaded = True for ammo in range(0, 1): ammo = Ammo("images/bullet.png", "bullet") ammo.surf = pygame.image.load("images/bullet.png") ammo.surf = pygame.transform.scale( pygame.image.load("images/bullet.png"), (BULLET_X * BULLET_MULTIPLIER, BULLET_Y * BULLET_MULTIPLIER)) ammo.surf.set_colorkey(GREEN_SCREEN) ammo.rect = ammo.surf.get_rect() ammo.super = True cowboy.ammo.append(ammo) if self.rect.colliderect(indian.rect): self.rect.x = 2200 self.kill() indian.ammo = [] indian.reload_img = False indian.reloaded = True for ammo in range(0, 1): ammo = Ammo("images/arrow.png", "arrow") ammo.surf = pygame.image.load("images/arrow.png") ammo.surf = pygame.transform.scale( pygame.image.load("images/arrow.png"), (ARROW_X * ARROW_MULTIPLIER, ARROW_Y * ARROW_MULTIPLIER)) ammo.surf.set_colorkey(GREEN_SCREEN) ammo.rect = ammo.surf.get_rect() ammo.super = True indian.ammo.append(ammo)
def __init__(self,gamestate,score,ammo): # Konfuguracja random.seed() #inicjalizaja pygame.init() self.SCREEN_SIZE = (1280, 720) # grafiki dopasowane do tego self.screen = pygame.display.set_mode(self.SCREEN_SIZE) self.tps_clock = pygame.time.Clock() self.tps_delta = 0.0 self.shot = 0.0 self.supertime = -2 self.player = Player(self) self.aliens = [] self.opp = 10 self.bullets = [] self.alienbulets = [] self.lives = Lives(self) self.lives_number = 3 self.score = Score(self) self.score_number = 0 + score self.ammo = Ammo(self) self.walls = [] self.gameover = GameOver(self) self.gamestate = gamestate self.ammo_number = ammo + 5 self.nextlevel = NextLevel(self) self.tps_max = 300.0 self.superalien = [] self.tooClose = False self.pauseSign = Pause(self) self.pause = 1 for i in range(0,self.opp): self.aliens.append(Alien(self, i * 100 + 100, 100,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 150,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 200,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 250,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 300,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 350,self.gamestate-1)) self.rand_opp = 6*self.opp for i in range(0,5): self.walls.append(Wall(self,80+i*340)) channel_game = pygame.mixer.Channel(1) channel_game2 = pygame.mixer.Channel(2) channel_game3 = pygame.mixer.Channel(3) self.background = pygame.image.load("tlo3.jpg") self.text = open("score").readline() while self.gamestate !=0: if self.rand_opp != 0: los = random.randrange(self.rand_opp) else: los = 0 # obsługa zdarzen for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN and event.key == pygame.K_p: self.pause *= -1 elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and self.ammo_number != 0: self.bullets.append(Bullet(self,self.player.pos[0]+23,self.player.pos[1])) channel_game3.play(pygame.mixer.Sound("mygun.wav")) channel_game3.set_volume(0.5) elif (self.lives_number == 0 or self.ammo_number <= 0 or self.tooClose == True) and event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: self.gamestate = 0 elif len(self.aliens) == 0 and event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: self.gamestate += 1 Game(self.gamestate,self.score_number,self.ammo_number) self.text = open("score").readline() self.gamestate = 0 elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE and self.pause == -1: self.gamestate = 0 elif event.type == pygame.KEYDOWN and event.key == pygame.K_p and self.pause == -1: self.pause *= -1 #ticking self.tps_delta += self.tps_clock.tick() / 1000.0 self.shot += self.tps_clock.tick()+0.000000003*(self.gamestate-1) / 1.0 self.supertime += self.tps_clock.tick() / 1.0 while self.tps_delta > 1 / self.tps_max: self.tick() self.tps_delta -= 1 / self.tps_max while(self.shot >= 0.001 / self.tps_max and len(self.aliens)!=0 and (self.lives_number != 0 and self.ammo_number > 0) and self.tooClose == False and self.pause == 1): self.shot = 0 channel_game.play(pygame.mixer.Sound("shot.wav")) channel_game.set_volume(0.5) self.alienbulets.append(AlienBullet(self,self.aliens[los].x,self.aliens[los].y)) while self.supertime >= 0.001 / self.tps_max: self.supertime = -2 if(len(self.superalien)==0 and self.tooClose == False and self.lives_number !=0 and self.ammo_number > 0 and self.pause == 1): self.superalien.append(SuperAlien(self)) channel_game2.play(pygame.mixer.Sound("supersound.wav")) channel_game2.set_volume(0.3) #rendering self.screen.fill((0, 0, 0)) self.screen.blit(self.background, (0, 0)) self.draw() pygame.display.flip()
class Game(object): def __init__(self,gamestate,score,ammo): # Konfuguracja random.seed() #inicjalizaja pygame.init() self.SCREEN_SIZE = (1280, 720) # grafiki dopasowane do tego self.screen = pygame.display.set_mode(self.SCREEN_SIZE) self.tps_clock = pygame.time.Clock() self.tps_delta = 0.0 self.shot = 0.0 self.supertime = -2 self.player = Player(self) self.aliens = [] self.opp = 10 self.bullets = [] self.alienbulets = [] self.lives = Lives(self) self.lives_number = 3 self.score = Score(self) self.score_number = 0 + score self.ammo = Ammo(self) self.walls = [] self.gameover = GameOver(self) self.gamestate = gamestate self.ammo_number = ammo + 5 self.nextlevel = NextLevel(self) self.tps_max = 300.0 self.superalien = [] self.tooClose = False self.pauseSign = Pause(self) self.pause = 1 for i in range(0,self.opp): self.aliens.append(Alien(self, i * 100 + 100, 100,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 150,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 200,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 250,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 300,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 350,self.gamestate-1)) self.rand_opp = 6*self.opp for i in range(0,5): self.walls.append(Wall(self,80+i*340)) channel_game = pygame.mixer.Channel(1) channel_game2 = pygame.mixer.Channel(2) channel_game3 = pygame.mixer.Channel(3) self.background = pygame.image.load("tlo3.jpg") self.text = open("score").readline() while self.gamestate !=0: if self.rand_opp != 0: los = random.randrange(self.rand_opp) else: los = 0 # obsługa zdarzen for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN and event.key == pygame.K_p: self.pause *= -1 elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and self.ammo_number != 0: self.bullets.append(Bullet(self,self.player.pos[0]+23,self.player.pos[1])) channel_game3.play(pygame.mixer.Sound("mygun.wav")) channel_game3.set_volume(0.5) elif (self.lives_number == 0 or self.ammo_number <= 0 or self.tooClose == True) and event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: self.gamestate = 0 elif len(self.aliens) == 0 and event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: self.gamestate += 1 Game(self.gamestate,self.score_number,self.ammo_number) self.text = open("score").readline() self.gamestate = 0 elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE and self.pause == -1: self.gamestate = 0 elif event.type == pygame.KEYDOWN and event.key == pygame.K_p and self.pause == -1: self.pause *= -1 #ticking self.tps_delta += self.tps_clock.tick() / 1000.0 self.shot += self.tps_clock.tick()+0.000000003*(self.gamestate-1) / 1.0 self.supertime += self.tps_clock.tick() / 1.0 while self.tps_delta > 1 / self.tps_max: self.tick() self.tps_delta -= 1 / self.tps_max while(self.shot >= 0.001 / self.tps_max and len(self.aliens)!=0 and (self.lives_number != 0 and self.ammo_number > 0) and self.tooClose == False and self.pause == 1): self.shot = 0 channel_game.play(pygame.mixer.Sound("shot.wav")) channel_game.set_volume(0.5) self.alienbulets.append(AlienBullet(self,self.aliens[los].x,self.aliens[los].y)) while self.supertime >= 0.001 / self.tps_max: self.supertime = -2 if(len(self.superalien)==0 and self.tooClose == False and self.lives_number !=0 and self.ammo_number > 0 and self.pause == 1): self.superalien.append(SuperAlien(self)) channel_game2.play(pygame.mixer.Sound("supersound.wav")) channel_game2.set_volume(0.3) #rendering self.screen.fill((0, 0, 0)) self.screen.blit(self.background, (0, 0)) self.draw() pygame.display.flip() def tick(self): if (self.lives_number != 0 and self.ammo_number > 0 and len(self.aliens) !=0 and self.tooClose == False and self.pause == 1): self.edge = False self.player.tick() for i in range(0,len(self.aliens)): self.aliens[i].move() if (self.aliens[i].x >= self.SCREEN_SIZE[0]-50) or (self.aliens[i].x <= 0): self.edge = True if self.edge: for i in range(0, len(self.aliens)): if self.aliens[i].x >= self.SCREEN_SIZE[0]-100: for j in range(0, len(self.aliens)): self.aliens[j].x -= 3 elif self.aliens[i].x < 50: for j in range(0, len(self.aliens)): self.aliens[j].x += 3 self.aliens[i].shiftDown() self.edge = False if (self.aliens[i].y > 600 ): self.tooClose = True for i in range(0,len(self.bullets)): self.bullets[i].tick() for i in range(0,len(self.alienbulets)): self.alienbulets[i].tick() for i in range(0,len(self.superalien)): self.superalien[i].move() def draw(self): self.player.draw() self.lives.draw(self.lives_number) self.score.draw(self.score_number) self.ammo.draw(self.ammo_number) for i in range(0,len(self.walls)-1): self.walls[i].draw() for i in range(0,len(self.bullets)): self.bullets[i].draw() for j in range(0, len(self.aliens)): if(self.bullets[i].hits(self.aliens[j])): self.aliens[j].destroy() self.bullets[i].destroy() self.score_number +=10 #self.ammo_number += 1 for j in range(0, len(self.walls)): if (self.bullets[i].hits_wall(self.walls[j])): self.bullets[i].destroy() self.ammo_number -=1 if (self.walls[j].state > 0): self.walls[j].state -= 1 else: self.walls[j].destroy() for j in range(0,len(self.superalien)): if(self.bullets[i].hits_super(self.superalien[j])): self.superalien[j].destroy() self.bullets[i].destroy() self.score_number +=100 self.ammo_number += 5 if(self.bullets[i].y <= 0): self.bullets[i].destroy() self.ammo_number -= 1 for i in range(0,len(self.aliens)): self.aliens[i].draw() for i in range(len(self.bullets)-1,-1,-1): if(self.bullets[i].toDestroy): self.bullets.pop(i) for i in range(len(self.aliens)-1,-1,-1): if(self.aliens[i].toDestroy): self.aliens.pop(i) self.rand_opp -= 1 for i in range(len(self.walls)-1,-1,-1): if(self.walls[i].toDestroy): self.walls.pop(i) for i in range(len(self.alienbulets)-1,-1,-1): if(self.alienbulets[i].toDestroy): self.alienbulets.pop(i) for i in range(len(self.superalien)-1,-1,-1): if(self.superalien[i].toDestroy): self.superalien.pop(i) for i in range(0,len(self.superalien)): self.superalien[i].draw() if self.superalien[i].x < -65: self.superalien[i].destroy() for i in range(0,len(self.alienbulets)): self.alienbulets[i].draw() if(self.alienbulets[i].hits(self.player) and len(self.aliens) != 0): self.alienbulets[i].destroy() self.lives_number -= 1 self.ammo_number -= 4*(self.gamestate) self.score_number -= 100 for j in range(0,len(self.walls)): if(self.alienbulets[i].hits_wall(self.walls[j])): self.alienbulets[i].destroy() if(self.walls[j].state>0): self.walls[j].state -=1 else: self.walls[j].destroy() if (self.alienbulets[i].y >= 720): self.alienbulets[i].destroy() if self.lives_number == 0 or self.ammo_number <= 0 or self.tooClose: self.gameover.draw() if len(self.aliens) == 0: self.nextlevel.draw() if self.pause == -1: self.pauseSign.draw() if(self.score_number > int(self.text)): self.cel = open("score", "w") self.cel.write(self.text.replace(self.text,str(self.score_number))) self.cel.close()
def load_ammos(self): ammo = Ammo(self.pos_x, self.pos_y) ammo.render() self.fired_ammos.append(ammo)
def fire_ammo(self): self.all_ammo.add(Ammo(self))