def __init__(self): pygame.init() self.settings = Settings() self.bullets = pygame.sprite.Group() self.enemies = pygame.sprite.Group() self.evil_bullets = pygame.sprite.Group() self.a_map = World_map() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption("Invasion") self.level = 0 self.stats = Stats(self) temp_x, temp_y = 200, 50 temp_color = (0, 255, 0) temp_text_color = (255, 255, 255) self.play_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "PLAY") temp_x, temp_y = 350, 50 temp_color = (100, 100, 100) temp_text_color = (255, 0, 0) self.win_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "YOU ARE A WINNER!") self.myTank = Ship(self) self.myTank.live = 0 self.image = pygame.image.load('images/brick_wall3.png') self.rect = self.image.get_rect()
def __init__(self, game, direction, level): super().__init__() self.a_map = World_map() if level == 2: self.a_map.level_2_world_map() elif level == 3: self.a_map.level_3_world_map() elif level == 4: self.a_map.level_4_world_map() elif level == 5: self.a_map.level_5_world_map() self.screen = game.screen self.color = BULLET_COLOR if direction == 'up': self.rect = pygame.Rect(0, 0, BULLET_WIGHT, BULLET_HEIGHT) self.rect.midtop = game.myTank.rect.midtop elif direction == 'down': self.rect = pygame.Rect(0, 0, BULLET_WIGHT, BULLET_HEIGHT) self.rect.midbottom = game.myTank.rect.midbottom elif direction == 'left': self.rect = pygame.Rect(0, 0, BULLET_HEIGHT, BULLET_WIGHT) self.rect.midleft = game.myTank.rect.midleft elif direction == 'right': self.rect = pygame.Rect(0, 0, BULLET_HEIGHT, BULLET_WIGHT) self.rect.midright = game.myTank.rect.midright self.direction = direction self.y = float(self.rect.y) self.x = float(self.rect.x)
def __init__(self): pygame.init() self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Future") self.clock = pygame.time.Clock() self.a_map = World_map() self.bender = Robot(self) self.angle = 0 self.angle_minus = False self.angle_plus = False self.mouse_pos_x, self.mouse_pos_y = 0, 0 #self.mouse_sin = 0 #self.mouse_cos = 0 self.bullets = pygame.sprite.Group() self.bots1 = pygame.sprite.Group() bot1 = Bot1(self) bot1.rect.x = 904 bot1.rect.y = 282 bot1.real_x = 904 bot1.real_y = 282 bot1.x_on_a_map = 904 bot1.y_on_a_map = 282 self.bots1.add(bot1) bot1 = Bot1(self) bot1.rect.x = 104 bot1.rect.y = 292 bot1.real_x = 104 bot1.real_y = 292 bot1.x_on_a_map = 104 bot1.y_on_a_map = 292 self.bots1.add(bot1) #self.bot2 = Bot2(self) self.gun1 = Guns(self) self.fire = False self.gun1_status = False self.gap = 0 self.gap_y = 0 self.gap_left = False self.gap_right = False
def __init__(self): pygame.init() self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Future") self.clock = pygame.time.Clock() self.a_map = World_map() self.bender = Robot(self) self.mouse_pos_x, self.mouse_pos_y = 0, 0 self.bullets = pygame.sprite.Group() self.bots1 = pygame.sprite.Group() bot1 = Bot1(self) bot1.rect.x = 904 bot1.rect.y = 282 bot1.real_x = 904 bot1.real_y = 282 bot1.x_on_a_map = 904 bot1.y_on_a_map = 282 self.bots1.add(bot1) bot1 = Bot1(self) bot1.rect.x = 104 bot1.rect.y = 292 bot1.real_x = 104 bot1.real_y = 292 bot1.x_on_a_map = 104 bot1.y_on_a_map = 292 self.bots1.add(bot1) self.bots2 = pygame.sprite.Group() self.bot2 = Bot2(self) self.bot2.rect.x = 600 self.bot2.rect.y = 92 self.bots2.add(self.bot2) self.gun1 = Guns(self) self.fire = False self.gun1_status = False self.move_left = False self.move_right = False self.camera = Camera(self.camera_configure, self)
def __init__(self, game, enemy_x, enemy_y, direction, level): #Bullet.__init__(self, enemy, direction) Sprite.__init__(self) self.screen = game.screen self.color = BULLET_COLOR self.a_map = World_map() if level == 2: self.a_map.level_2_world_map() elif level == 3: self.a_map.level_3_world_map() elif level == 4: self.a_map.level_4_world_map() elif level == 5: self.a_map.level_5_world_map() if direction == 'up': self.rect = pygame.Rect(0, 0, BULLET_WIGHT, BULLET_HEIGHT) self.rect.x = enemy_x + 15 self.rect.y = enemy_y #self.rect.midtop = game.myTank.rect.midtop elif direction == 'down': self.rect = pygame.Rect(0, 0, BULLET_WIGHT, BULLET_HEIGHT) self.rect.x = enemy_x + 15 self.rect.y = enemy_y + 30 #self.rect.midbottom = game.myTank.rect.midbottom elif direction == 'left': self.rect = pygame.Rect(0, 0, BULLET_HEIGHT, BULLET_WIGHT) #self.rect.midleft = game.myTank.rect.midleft self.rect.x = enemy_x self.rect.y = enemy_y + 15 elif direction == 'right': self.rect = pygame.Rect(0, 0, BULLET_HEIGHT, BULLET_WIGHT) #self.rect.midright = game.myTank.rect.midright self.rect.x = enemy_x + 30 self.rect.y = enemy_y + 15 self.direction = direction #self.rect.y = enemy_y + 16 #self.rect.x = enemy_x + 16 self.y = float(self.rect.y) self.x = float(self.rect.x)
def __init__(self, game): self.settings = Settings() self.screen = game.screen self.screen_rect = game.screen.get_rect() self.a_map = World_map() self.image = pygame.image.load('images/greenBattle32.png') self.imageLeft = pygame.image.load('images/greenBattleLeft32.png') self.imageRight = pygame.image.load('images/greenBattleRight32.png') self.imageDown = pygame.image.load('images/greenBattleDown32.png') self.imageExplode = pygame.image.load('images/shotRed32.png') # for images self.direction = 'up' #self.live = True self.rect = self.image.get_rect() self.SIZE = 32 #start coordinates self.rect.topleft = self.screen_rect.topleft self.rect.x += 52 self.rect.y += 52 #temporary self.rightTop = [self.rect.x + self.SIZE, self.rect.y] self.rightBottom = [self.rect.x + self.SIZE, self.rect.y + self.SIZE] self.lefttBottom = [self.rect.x, self.rect.y + self.SIZE] self.moving_right = False self.moving_left = False self.moving_up = False self.moving_down = False self.ship_speed = 1.5 self.live = 3 self.out_coordinates = [] self.coordinates_others = []
class Ship(): def __init__(self, game): self.settings = Settings() self.screen = game.screen self.screen_rect = game.screen.get_rect() self.a_map = World_map() self.image = pygame.image.load('images/greenBattle32.png') self.imageLeft = pygame.image.load('images/greenBattleLeft32.png') self.imageRight = pygame.image.load('images/greenBattleRight32.png') self.imageDown = pygame.image.load('images/greenBattleDown32.png') self.imageExplode = pygame.image.load('images/shotRed32.png') # for images self.direction = 'up' #self.live = True self.rect = self.image.get_rect() self.SIZE = 32 #start coordinates self.rect.topleft = self.screen_rect.topleft self.rect.x += 52 self.rect.y += 52 #temporary self.rightTop = [self.rect.x + self.SIZE, self.rect.y] self.rightBottom = [self.rect.x + self.SIZE, self.rect.y + self.SIZE] self.lefttBottom = [self.rect.x, self.rect.y + self.SIZE] self.moving_right = False self.moving_left = False self.moving_up = False self.moving_down = False self.ship_speed = 1.5 self.live = 3 self.out_coordinates = [] self.coordinates_others = [] def create_map(self): self.out_coordinates = [] self.coordinates_others = [] self.a_map.create_map() def change_map(self): self.a_map.level_2_world_map() #print ('IT ITTTTTTTTTTTTTTTTTTTTTTTTTTTTsdsdsds') def change_map_3(self): self.a_map.level_3_world_map() def change_map_4(self): self.a_map.level_4_world_map() def change_map_5(self): self.a_map.level_5_world_map() def empty_coordinates_others(self): self.coordinates_others = [] def add_coordinates(self, x, y): self.coordinates_others.append([x, y]) def random_coordinates(self): list_x = [x for x in range(0, self.settings.screen_width, self.SIZE)] list_y = [y for y in range(0, self.settings.screen_height, self.SIZE)] self.rect.x = random.choice(list_x) self.rect.y = random.choice(list_y) if len(self.out_coordinates) < self.a_map.space: for x, y in self.out_coordinates: # checked space not to do if self.rect.x == x and self.rect.y == y: self.random_coordinates() # creating a tank into empy place def right_coordinates(self): for i in range(500): #while does not needed if i < self.a_map.space: # chack every space in a map if self.apear_coordinates(): self.out_coordinates.append( (self.rect.x, self.rect.y)) # checked space #self.rect.x = 0 #test #self.rect.y = 0 #test pass else: print('ELSE') return True else: print('limit is out') return False return False # important tanks have to be into a map def apear_coordinates(self): self.random_coordinates() #print(f'x = {self.rect.x} y = {self.rect.y}') self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE # walls for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE # left top if (rightTopX >= self.rect.x >= x) and (lefttBottomY >= self.rect.y >= y): return True # right top elif (rightTopX >= self.rightTopX >= x) and (lefttBottomY >= self.rect.y >= y): return True # left bottom elif (rightTopX >= self.rect.x >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True # right bottom elif (rightTopX >= self.rightTopX >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True #coordinates_others for x, y in self.coordinates_others: #I need to add my Tank into enemy if self.rect.x == x and self.rect.y == y: return True # the same thing But a little harder #rightTopX = x + self.SIZE #lefttBottomY = y + self.SIZE ## left top #if (rightTopX >= self.rect.x >= x) and (lefttBottomY >= self.rect.y >= y): #return True ## right top #elif (rightTopX >= self.rightTopX >= x) and (lefttBottomY >= self.rect.y >= y): #return True ## left bottom #elif (rightTopX >= self.rect.x >= x) and (lefttBottomY >= self.lefttBottomY >= y): #return True ## right bottom #elif (rightTopX >= self.rightTopX >= x) and (lefttBottomY >= self.lefttBottomY >= y): #return True return False def check_coordinates_right(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.coordinates_others: rightTopX = x + self.SIZE lefttBottomY = y + self.SIZE if (rightTopX >= self.rightTopX + self.ship_speed >= x) and (lefttBottomY >= self.rect.y >= y): return True elif (rightTopX >= self.rightTopX + self.ship_speed >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True return False def check_coordinates_top(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.coordinates_others: rightTopX = x + self.SIZE lefttBottomY = y + self.SIZE if (rightTopX >= self.rect.x >= x) and ( lefttBottomY >= self.rect.y - self.ship_speed >= y): return True elif (rightTopX >= self.rightTopX >= x) and (lefttBottomY >= self.rect.y - self.ship_speed >= y): return True return False def check_coordinates_left(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.coordinates_others: rightTopX = x + self.SIZE lefttBottomY = y + self.SIZE if (rightTopX >= self.rect.x - self.ship_speed >= x) and (lefttBottomY >= self.rect.y >= y): return True elif (rightTopX >= self.rect.x - self.ship_speed >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True return False def check_coordinates_bottom(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.coordinates_others: rightTopX = x + self.SIZE lefttBottomY = y + self.SIZE if (rightTopX >= self.rightTopX >= x) and ( lefttBottomY >= self.lefttBottomY + self.ship_speed >= y): return True elif (rightTopX >= self.rect.x >= x) and ( lefttBottomY >= self.lefttBottomY + self.ship_speed >= y): return True return False def check_top_wall(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rect.x >= x) and ( lefttBottomY >= self.rect.y - self.ship_speed >= y): return True elif (rightTopX >= self.rightTopX >= x) and (lefttBottomY >= self.rect.y - self.ship_speed >= y): return True return False def check_left_wall(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rect.x - self.ship_speed >= x) and (lefttBottomY >= self.rect.y >= y): return True elif (rightTopX >= self.rect.x - self.ship_speed >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True return False def check_right_wall(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rightTopX + self.ship_speed >= x) and (lefttBottomY >= self.rect.y >= y): return True elif (rightTopX >= self.rightTopX + self.ship_speed >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True return False def check_bottom_wall(self): self.rightTopX = self.rect.x + self.SIZE self.lefttBottomY = self.rect.y + self.SIZE for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rightTopX >= x) and ( lefttBottomY >= self.lefttBottomY + self.ship_speed >= y): return True elif (rightTopX >= self.rect.x >= x) and ( lefttBottomY >= self.lefttBottomY + self.ship_speed >= y): return True return False def blitme(self): if self.direction == 'up': self.screen.blit(self.image, self.rect) elif self.direction == 'left': self.screen.blit(self.imageLeft, self.rect) elif self.direction == 'right': self.screen.blit(self.imageRight, self.rect) elif self.direction == 'down': self.screen.blit(self.imageDown, self.rect) if self.live == False: self.screen.blit(self.imageExplode, self.rect) def update(self): if self.moving_down: if self.check_bottom_wall( ) == False and self.check_coordinates_bottom() == False: self.rect.y += self.ship_speed self.direction = 'down' #print(self.rect.x, self.rect.y) else: self.direction = 'down' elif self.moving_up: if self.check_top_wall() == False and self.check_coordinates_top( ) == False: self.rect.y -= self.ship_speed self.direction = 'up' #print(self.rect.x, self.rect.y) else: self.direction = 'up' elif self.moving_left: if self.check_left_wall() == False and self.check_coordinates_left( ) == False: self.rect.x -= self.ship_speed self.direction = 'left' #print(self.rect.x, self.rect.y) else: self.direction = 'left' elif self.moving_right: if self.check_right_wall( ) == False and self.check_coordinates_right() == False: self.rect.x += self.ship_speed self.direction = 'right' #print(self.rect.x, self.rect.y) else: self.direction = 'right'
class Bullet(Sprite): def __init__(self, game, direction, level): super().__init__() self.a_map = World_map() if level == 2: self.a_map.level_2_world_map() elif level == 3: self.a_map.level_3_world_map() elif level == 4: self.a_map.level_4_world_map() elif level == 5: self.a_map.level_5_world_map() self.screen = game.screen self.color = BULLET_COLOR if direction == 'up': self.rect = pygame.Rect(0, 0, BULLET_WIGHT, BULLET_HEIGHT) self.rect.midtop = game.myTank.rect.midtop elif direction == 'down': self.rect = pygame.Rect(0, 0, BULLET_WIGHT, BULLET_HEIGHT) self.rect.midbottom = game.myTank.rect.midbottom elif direction == 'left': self.rect = pygame.Rect(0, 0, BULLET_HEIGHT, BULLET_WIGHT) self.rect.midleft = game.myTank.rect.midleft elif direction == 'right': self.rect = pygame.Rect(0, 0, BULLET_HEIGHT, BULLET_WIGHT) self.rect.midright = game.myTank.rect.midright self.direction = direction self.y = float(self.rect.y) self.x = float(self.rect.x) def check_top_wall(self): self.rightTopX = self.rect.x + BULLET_WIGHT self.lefttBottomY = self.rect.y + BULLET_HEIGHT for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rect.x >= x) and (lefttBottomY >= self.rect.y - BULLET_SPEED >= y): return True elif (rightTopX >= self.rightTopX >= x) and (lefttBottomY >= self.rect.y - BULLET_SPEED >= y): return True return False def check_left_wall(self): self.rightTopX = self.rect.x + BULLET_HEIGHT self.lefttBottomY = self.rect.y + BULLET_WIGHT for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rect.x - BULLET_SPEED >= x) and (lefttBottomY >= self.rect.y >= y): return True elif (rightTopX >= self.rect.x - BULLET_SPEED >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True return False def check_right_wall(self): self.rightTopX = self.rect.x + BULLET_HEIGHT self.lefttBottomY = self.rect.y + BULLET_WIGHT for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rightTopX + BULLET_SPEED >= x) and (lefttBottomY >= self.rect.y >= y): return True elif (rightTopX >= self.rightTopX + BULLET_SPEED >= x) and (lefttBottomY >= self.lefttBottomY >= y): return True return False def check_bottom_wall(self): self.rightTopX = self.rect.x + BULLET_WIGHT self.lefttBottomY = self.rect.y + BULLET_HEIGHT for x, y in self.a_map.world_map: rightTopX = x + TILE lefttBottomY = y + TILE if (rightTopX >= self.rightTopX >= x) and ( lefttBottomY >= self.lefttBottomY + BULLET_SPEED >= y): return True elif (rightTopX >= self.rect.x >= x) and ( lefttBottomY >= self.lefttBottomY + BULLET_SPEED >= y): return True return False return False def update(self): if self.direction == 'up': if self.check_top_wall() == False: self.y -= BULLET_SPEED self.rect.y = self.y else: return True elif self.direction == 'down': if self.check_bottom_wall() == False: self.y += BULLET_SPEED self.rect.y = self.y else: return True elif self.direction == 'left': if self.check_left_wall() == False: self.x -= BULLET_SPEED self.rect.x = self.x else: return True elif self.direction == 'right': if self.check_right_wall() == False: self.x += BULLET_SPEED self.rect.x = self.x else: return True return False def draw_bullet(self): pygame.draw.rect(self.screen, self.color, self.rect)
class Game(): def __init__(self): pygame.init() self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Future") self.clock = pygame.time.Clock() self.a_map = World_map() self.bender = Robot(self) self.angle = 0 self.angle_minus = False self.angle_plus = False self.mouse_pos_x, self.mouse_pos_y = 0, 0 #self.mouse_sin = 0 #self.mouse_cos = 0 self.bullets = pygame.sprite.Group() self.bots1 = pygame.sprite.Group() bot1 = Bot1(self) bot1.rect.x = 904 bot1.rect.y = 282 bot1.real_x = 904 bot1.real_y = 282 bot1.x_on_a_map = 904 bot1.y_on_a_map = 282 self.bots1.add(bot1) bot1 = Bot1(self) bot1.rect.x = 104 bot1.rect.y = 292 bot1.real_x = 104 bot1.real_y = 292 bot1.x_on_a_map = 104 bot1.y_on_a_map = 292 self.bots1.add(bot1) #self.bot2 = Bot2(self) self.gun1 = Guns(self) self.fire = False self.gun1_status = False self.gap = 0 self.gap_y = 0 self.gap_left = False self.gap_right = False def _check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() #self.fire_direction(mouse_pos) self.fire_bullets(mouse_pos) #self.fire_direction(mouse_pos) if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit() if event.key == pygame.K_SPACE: self.bender.jump() if event.key == pygame.K_a: self.bender.direction = 'left' #self.bender.moving_left = True self.gap_left = True if event.key == pygame.K_d: #self.bender.moving_right = True self.bender.direction = 'right' self.gap_right = True if event.key == pygame.K_f: self.bender.shot_f = True print( f'self.bender.real_x = {self.bender.real_x} self.bender.real_y = {self.bender.real_y}' ) for bot1 in self.bots1: print( f'self.bot1.x_on_a_map = {bot1.x_on_a_map} self.bot1._on_a_map_y = {bot1.y_on_a_map}' ) print( f'self.bot1.rect.x = {bot1.rect.x} self.bot1.rect.y = {bot1.rect.y}' ) if event.key == pygame.K_k: pass if event.key == pygame.K_l: self.fire = True mouse_pos = pygame.mouse.get_pos() #self.fire_direction(mouse_pos) self.fire_bullets(mouse_pos) #self.fire_bullets() if event.key == pygame.K_1: if self.gun1_status == False: self.gun1_status = True elif self.gun1_status == True: self.gun1_status = False if event.key == pygame.K_LEFT: self.angle_minus = True if event.key == pygame.K_RIGHT: self.angle_plus = True if event.type == pygame.KEYUP: if event.key == pygame.K_a: #self.bender.moving_left = False self.gap_left = False if event.key == pygame.K_d: #self.bender.moving_right = False self.gap_right = False if event.key == pygame.K_f: self.bender.shot_f = False #if event.key == pygame.K_k: #self.fire = False if event.key == pygame.K_LEFT: self.angle_minus = False if event.key == pygame.K_RIGHT: self.angle_plus = False def fire_bullets(self, mouse_pos): if len(self.bullets) <= 999999: #BULLET_LIMIT: #my fire limit self.rect = pygame.draw.circle(self.screen, RED, (0, 0), 6) if self.bender.direction == 'right': #self.rect.midright = self.bender.rect.midright self.rect.x = self.bender.rect.x self.rect.y = self.bender.rect.y self.rect.x += 80 self.rect.y += 60 #self.rect.midleft = self.bender.rect.midleft self.mouse_pos_x, self.mouse_pos_y = mouse_pos self.mouse_pos_x = self.mouse_pos_x - self.rect.x self.mouse_pos_y = self.mouse_pos_y - self.rect.y print( f'self.mouse_pos_x = {self.mouse_pos_x}, self.mouse_pos_y = {self.mouse_pos_y}' ) xx = self.mouse_pos_x yy = self.mouse_pos_y line = (math.sqrt(xx**2 + yy**2)) print(f'line = {line} xx == {xx} yy== {yy}') print( f'self.bender.real_x = {self.bender.real_x} self.bender.real_y = {self.bender.real_y}' ) self.mouse_cos = xx / line self.mouse_sin = yy / line if -1 <= self.mouse_cos <= 1 and -1 <= self.mouse_sin <= 1 and self.gun1_status == True: new_bullet = Bullet(self, self.bender.direction) self.bullets.add(new_bullet) if self.bender.direction == 'left': #self.rect.midleft = self.bender.rect.midleft self.rect.x = self.bender.rect.x self.rect.y = self.bender.rect.y self.rect.x -= 25 self.rect.y += 60 #self.rect.midleft = self.bender.rect.midleft self.mouse_pos_x, self.mouse_pos_y = mouse_pos self.mouse_pos_x = self.mouse_pos_x - self.rect.x self.mouse_pos_y = self.mouse_pos_y - self.rect.y print( f'self.mouse_pos_x = {self.mouse_pos_x}, self.mouse_pos_y = {self.mouse_pos_y}' ) xx = self.mouse_pos_x yy = self.mouse_pos_y line = (math.sqrt(xx**2 + yy**2)) print(f'line = {line} xx == {xx} yy== {yy}') print( f'self.bender.real_x = {self.bender.real_x} self.bender.real_y = {self.bender.real_y}' ) self.mouse_cos = xx / line self.mouse_sin = yy / line if -1 <= self.mouse_cos <= 1 and -1 <= self.mouse_sin <= 1 and self.gun1_status == True: new_bullet = Bullet(self, self.bender.direction) self.bullets.add(new_bullet) #if -1 <= self.mouse_sin <= 1 and -1 <= self.mouse_cos <= 1 and self.gun1_status == True: #new_bullet = Bullet(self, self.bender.direction) #self.bullets.add(new_bullet) #def fire_direction(self, mouse_pos): #self.mouse_pos_x, self.mouse_pos_y = mouse_pos #self.mouse_pos_x = self.mouse_pos_x - 400#self.rect.x #self.mouse_pos_y = self.mouse_pos_y - 400#self.rect.y ##self.mouse_pos_x -= self.bender.real_x ##self.mouse_pos_y -= self.bender.real_y #print(f'self.mouse_pos_x = {self.mouse_pos_x}, self.mouse_pos_y = {self.mouse_pos_y}') #pass def run(self): #gap = 0 while True: for r in range(1000): self._check_events() self.screen.fill(BGR_COLOR) temp_bullet_x = 0 temp_bullet_y = 0 temp_bot1_x = 0 temp_bot1_y = 0 if self.angle_minus == True: self.angle -= 0.05 if self.angle_plus == True: self.angle += 0.05 if self.bender.jump_activate < self.bender.height_jump and self.bender.jump_up == True: self.gap_y -= self.bender.size_of_jump self.bender.real_y -= self.bender.size_of_jump # real self.bender.jump_activate += 1 #print(f'{self.bender.jump_activate} {self.bender.jump_up}') temp_bot1_y += self.bender.size_of_jump #self.bot2.rect.y += self.bender.size_of_jump temp_bullet_y += self.bender.size_of_jump elif self.bender.jump_activate == self.bender.height_jump and self.bender.jump_up == True: self.bender.jump_up = False self.bender.jump_activate -= 1 self.gap_y += self.bender.size_of_jump self.bender.real_y += self.bender.size_of_jump temp_bot1_y -= self.bender.size_of_jump #self.bot2.rect.y -= self.bender.size_of_jump temp_bullet_y -= self.bender.size_of_jump #print(f'{self.bender.jump_activate} {self.bender.jump_up}') #print(f'temp_bot1_y = {temp_bot1_y } self.bender.rect.y = {self.bender.rect.y}') elif self.bender.jump_activate > 0 and self.bender.jump_up == False: self.gap_y += self.bender.size_of_jump self.bender.real_y += self.bender.size_of_jump self.bender.jump_activate -= 1 temp_bot1_y -= self.bender.size_of_jump #self.bot2.rect.y -= self.bender.size_of_jump temp_bullet_y -= self.bender.size_of_jump #print(f'temp_bot1_y = {temp_bot1_y } self.bender.rect.y = {self.bender.rec7t.y}') elif self.bender.jump_activate == 0 and self.bender.jump_up == False: self.bender.jump_act = False if self.gap_right == True: self.gap += 0.25 self.bender.real_x += 0.25 * TILE # right temp_bullet_x -= 0.25 * TILE #for bullet in self.bullets: #bullet.real_x -= 0.25 * TILE temp_bot1_x -= 0.25 * TILE #print(f'self.bender.real_x= {self.bender.real_x} temp_bot1_x = {temp_bot1_x} xxx= {temp_bot1_x - TILE*self.gap}') #self.bot2.rect.x = self.bot2.rect.x - 0.25 * TILE #print(f' self.bender.real_x= {self.bender.real_x} self.bender.real_y = {self.bender.real_y}') if self.gap_left == True: self.gap -= 0.25 self.bender.real_x -= 0.25 * TILE # right temp_bot1_x += 0.25 * TILE #0.25 * TILE temp_bullet_x += 0.25 * TILE #for bullet in self.bullets: #bullet.real_x += 0.25 * TILE #self.bot2.rect.x = self.bot2.rect.x + 0.25 * TILE #print(f'self.bender.real_x= {self.bender.real_x} temp_bot1_x = {temp_bot1_x}') #print(f' self.bender.real_x= {self.bender.real_x} self.bender.real_y = {self.bender.real_y}') #falling fall = False for row in range(len(self.a_map.hard_wall)): for xy in range(len(self.a_map.hard_wall[row])): x = self.a_map.hard_wall[row][xy][1] # - self.gap y = self.a_map.hard_wall[row][xy][0] #- self.gap_y #print(f'x = {x} y= {y} self.bender.real_x= {self.bender.real_x} self.bender.real_y = {self.bender.real_y}') if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y + self.bender.Y_HEIGHT <= y + 5: # LEFT DOWN #print(f'x = {x} y= {y} self.bender.real_x= {self.bender.real_x } self.bender.real_y = {self.bender.real_y + self.bender.Y_HEIGHT}') fall = True self.bender.jump_activate = 0 self.bender.jump_up = False elif x <= self.bender.real_x + self.bender.X_WIGHT / 2 <= x + TILE and y <= self.bender.real_y + self.bender.Y_HEIGHT <= y + 5: # MIDLE DOWN #print(f'x = {x} y= {y} self.bender.real_x= {self.bender.real_x+ self.bender.X_WIGHT } self.bender.real_y = {self.bender.real_y + self.bender.Y_HEIGHT}') fall = True self.bender.jump_activate = 0 self.bender.jump_up = False elif x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y + self.bender.Y_HEIGHT <= y + 5: # RIGHT DOWN #print(f'x = {x} y= {y} self.bender.real_x= {self.bender.real_x+ self.bender.X_WIGHT } self.bender.real_y = {self.bender.real_y + self.bender.Y_HEIGHT}') fall = True self.bender.jump_activate = 0 self.bender.jump_up = False #hit in a celling if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y <= y + TILE: # LEFT TOP self.bender.jump_up = False if x <= self.bender.real_x + self.bender.X_WIGHT / 2 <= x + TILE and y <= self.bender.real_y <= y + TILE: # LEFT MIDLE self.bender.jump_up = False if x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y <= y + TILE: # RIGHT TOP self.bender.jump_up = False #hit left if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y <= y + TILE and self.gap_left == True: # LEFT TOP self.gap_left = False self.gap += 0.25 self.bender.real_x += 0.25 * TILE # right temp_bot1_x -= 0.25 * TILE temp_bullet_x -= 0.25 * TILE if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y + TILE <= y + TILE and self.gap_left == True: # LEFT MIDLE self.gap_left = False self.gap += 0.25 self.bender.real_x += 0.25 * TILE # right temp_bot1_x -= 0.25 * TILE temp_bullet_x -= 0.25 * TILE if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y + TILE * 2 <= y + TILE and self.gap_left == True: # LEFT MIDLE self.gap_left = False self.gap += 0.25 self.bender.real_x += 0.25 * TILE # right temp_bot1_x -= 0.25 * TILE temp_bullet_x -= 0.25 * TILE if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y + TILE * 3 <= y + TILE and self.gap_left == True: # LEFT MIDLE self.gap_left = False self.gap += 0.25 self.bender.real_x += 0.25 * TILE # right temp_bot1_x -= 0.25 * TILE temp_bullet_x -= 0.25 * TILE if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y + TILE * 4 <= y + TILE and self.gap_left == True: # LEFT MIDLE self.gap_left = False self.gap += 0.25 self.bender.real_x += 0.25 * TILE # right temp_bot1_x -= 0.25 * TILE temp_bullet_x -= 0.25 * TILE #hit right if x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y <= y + TILE and self.gap_right == True: # LEFT TOP self.gap_right = False self.gap -= 0.25 self.bender.real_x -= 0.25 * TILE # left temp_bot1_x += 0.25 * TILE temp_bullet_x += 0.25 * TILE if x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y + TILE <= y + TILE and self.gap_right == True: self.gap_right = False self.gap -= 0.25 self.bender.real_x -= 0.25 * TILE # left temp_bot1_x += 0.25 * TILE temp_bullet_x += 0.25 * TILE if x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y + TILE * 2 <= y + TILE and self.gap_right == True: self.gap_right = False self.gap -= 0.25 self.bender.real_x -= 0.25 * TILE # left temp_bot1_x += 0.25 * TILE temp_bullet_x += 0.25 * TILE if x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y + TILE * 3 <= y + TILE and self.gap_right == True: self.gap_right = False self.gap -= 0.25 self.bender.real_x -= 0.25 * TILE # left temp_bot1_x += 0.25 * TILE temp_bullet_x += 0.25 * TILE if x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y + TILE * 4 <= y + TILE and self.gap_right == True: self.gap_right = False self.gap -= 0.25 self.bender.real_x -= 0.25 * TILE # left temp_bot1_x += 0.25 * TILE temp_bullet_x += 0.25 * TILE #stairs for i in range(len(self.a_map.hard_wall[row - 1])): right_x = self.a_map.hard_wall[row - 1][i][1] right_y = self.a_map.hard_wall[row - 1][i][0] if right_x <= self.bender.real_x + self.bender.X_WIGHT + 10 <= right_x + TILE and right_y <= self.bender.real_y + self.bender.Y_HEIGHT - 5 <= right_y + TILE and self.gap_right == True: # RIGHT DOWN self.bender.real_x += 0.25 * TILE self.gap += 0.25 # self.bender.real_y += -25 self.gap_y += -25 temp_bot1_x -= 0.25 * TILE temp_bot1_y += 25 temp_bullet_x -= 0.25 * TILE temp_bullet_y += 25 if right_x <= self.bender.real_x - 10 <= right_x + TILE and right_y <= self.bender.real_y + self.bender.Y_HEIGHT - 5 <= right_y + TILE and self.gap_left == True: # LEFT DOWN self.bender.real_x -= 0.25 * TILE self.gap -= 0.25 # self.bender.real_y += -25 self.gap_y += -25 temp_bot1_x += 0.25 * TILE temp_bot1_y += 25 temp_bullet_x += 0.25 * TILE temp_bullet_y += 25 #print(fall) if not fall and self.bender.jump_act == False: self.bender.real_y += self.bender.size_of_jump #self.bender.speed self.gap_y += self.bender.size_of_jump #self.bender.speed temp_bot1_y -= self.bender.size_of_jump temp_bullet_y -= self.bender.size_of_jump self.a_map.draw(self, self.gap, self.gap_y) if r % 20 == 0: #print(f'i= {r}') #self.bot2.bore() pass if r % 6 == 0: self.bender.bore() for bot1 in self.bots1: bot1.real_x += temp_bot1_x #+ 904 #last change bot1.real_y += temp_bot1_y #+ 282 bot1.blitme() #self.bot2.rect.x = temp_bot1_x + 1024 #last change #self.bot2.rect.y = temp_bot1_y + 300 #self.bot2.blitme() self.bender.blitme() if self.gun1_status == True: self.gun1.draw(self.bender.rect.x, self.bender.rect.y, self.bender.direction, self.fire) if self.fire == True: self.fire = False #pygame.draw.rect(self.screen, DARKGRAY, (x - TILE*gap, y - gap_y, TILE, TILE),2) for bullet in self.bullets: bullet.real_x += temp_bullet_x bullet.real_y += temp_bullet_y for bot1 in self.bots1: if bot1.x_on_a_map <= bullet.x_on_a_map <= bot1.x_on_a_map + bot1.size_x and bot1.y_on_a_map <= bullet.y_on_a_map <= bot1.y_on_a_map + bot1.size_y: self.bots1.remove(bot1) self.bullets.remove(bullet) # If you wanna destroy black and white squares ##for row in range(len(self.a_map.world_map_left_to_right)): ##for xy in range(len(self.a_map.world_map_left_to_right[row])): ###print(self.a_map.world_map_left_to_right[row][xy]) ##if self.a_map.world_map_left_to_right[row][xy][0] <= bullet.x_on_a_map <= self.a_map.world_map_left_to_right[row][xy][0] +TILE and self.a_map.world_map_left_to_right[row][xy][1] <= bullet.y_on_a_map <= self.a_map.world_map_left_to_right[row][xy][1] +TILE: ##self.bullets.remove(bullet) ###print(dir(self.a_map.world_map_left_to_right)) ##self.a_map.world_map_left_to_right[row].pop(xy) ##break ###print(self.a_map.world_map_left_to_right) for row in range(len(self.a_map.hard_wall)): for xy in range(len(self.a_map.hard_wall[row])): #print(self.a_map.world_map_list[row][xy]) if self.a_map.hard_wall[row][xy][ 1] <= bullet.x_on_a_map <= self.a_map.hard_wall[ row][xy][1] + TILE and self.a_map.hard_wall[ row][xy][ 0] <= bullet.y_on_a_map <= self.a_map.hard_wall[ row][xy][0] + TILE: self.bullets.remove(bullet) ##print(dir(self.a_map.world_map_left_to_right)) self.a_map.hard_wall[row].pop(xy) break if bullet.need_to_delete() == True: self.bullets.remove(bullet) bullet.update() for bullet in self.bullets: bullet.draw_bullet() #pygame.draw.rect(self.screen, DARKGRAY, (self.bender.rect.x, self.bender.rect.y, TILE, TILE)) ##if self.bender.rect.x == self.bender.real_x: #self.rect = pygame.draw.line(self.screen, GREEN, (self.bender.rect.x, self.bender.rect.y), (self.mouse_pos_x, self.mouse_pos_y), 2) #pygame.draw.line(self.screen, GREEN, (self.bender.rect.x, self.bender.rect.y), (self.bender.rect.x + 300 * math.cos(self.angle), pygame.display.flip() self.clock.tick(FPS)
class Game(): def __init__(self): pygame.init() self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Future") self.clock = pygame.time.Clock() self.a_map = World_map() self.bender = Robot(self) self.mouse_pos_x, self.mouse_pos_y = 0, 0 self.bullets = pygame.sprite.Group() self.bots1 = pygame.sprite.Group() bot1 = Bot1(self) bot1.rect.x = 904 bot1.rect.y = 282 bot1.real_x = 904 bot1.real_y = 282 bot1.x_on_a_map = 904 bot1.y_on_a_map = 282 self.bots1.add(bot1) bot1 = Bot1(self) bot1.rect.x = 104 bot1.rect.y = 292 bot1.real_x = 104 bot1.real_y = 292 bot1.x_on_a_map = 104 bot1.y_on_a_map = 292 self.bots1.add(bot1) self.bots2 = pygame.sprite.Group() self.bot2 = Bot2(self) self.bot2.rect.x = 600 self.bot2.rect.y = 92 self.bots2.add(self.bot2) self.gun1 = Guns(self) self.fire = False self.gun1_status = False self.move_left = False self.move_right = False self.camera = Camera(self.camera_configure, self) def camera_configure(self, camera, target_rect): l, t, _, _ = target_rect _, _, w, h = camera l, t = -l + SCREEN_WIDTH / 2, -t + SCREEN_HEIGHT / 2 l = min(0, l) # Не движемся дальше левой границы l = max(-(self.a_map.level_wight * TILE - camera.width), l) # Не движемся дальше правой границы t = max(-(self.a_map.level_height * TILE - camera.height), t) # Не движемся дальше нижней границы t = min(0, t) # Не движемся дальше верхней границы return pygame.Rect(l, t, w, h) def _check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() #self.fire_direction(mouse_pos) self.fire_bullets(mouse_pos) #self.fire_direction(mouse_pos) if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit() if event.key == pygame.K_SPACE: self.bender.jump() if event.key == pygame.K_a: self.bender.direction = 'left' #self.bender.moving_left = True self.move_left = True if event.key == pygame.K_d: #self.bender.moving_right = True self.bender.direction = 'right' self.move_right = True if event.key == pygame.K_f: self.bender.shot_f = True print( f'self.bender.real_x = {self.bender.real_x} self.bender.real_y = {self.bender.real_y}' ) #for bot1 in self.bots1: #print(f'self.bot1.x_on_a_map = {bot1.x_on_a_map} self.bot1._on_a_map_y = {bot1.y_on_a_map}') #print(f'self.bot1.rect.x = {bot1.rect.x} self.bot1.rect.y = {bot1.rect.y}') if event.key == pygame.K_k: pass if event.key == pygame.K_l: self.fire = True mouse_pos = pygame.mouse.get_pos() self.fire_bullets(mouse_pos) if event.key == pygame.K_1: if self.gun1_status == False: self.gun1_status = True elif self.gun1_status == True: self.gun1_status = False if event.key == pygame.K_LEFT: self.angle_minus = True if event.key == pygame.K_RIGHT: self.angle_plus = True if event.type == pygame.KEYUP: if event.key == pygame.K_a: self.move_left = False if event.key == pygame.K_d: self.move_right = False if event.key == pygame.K_f: self.bender.shot_f = False if event.key == pygame.K_k: pass if event.key == pygame.K_LEFT: self.angle_minus = False if event.key == pygame.K_RIGHT: self.angle_plus = False def fire_bullets(self, mouse_pos): if len(self.bullets) <= 999999: #BULLET_LIMIT: #my fire limit self.rect = pygame.draw.circle(self.screen, RED, (0, 0), 6) if self.bender.direction == 'right': self.rect = self.camera.apply(self.bender) self.rect.x += 80 self.rect.y += 60 self.mouse_pos_x, self.mouse_pos_y = mouse_pos self.mouse_pos_x = self.mouse_pos_x - self.rect.x self.mouse_pos_y = self.mouse_pos_y - self.rect.y print( f'self.mouse_pos_x = {self.mouse_pos_x}, self.mouse_pos_y = {self.mouse_pos_y}' ) xx = self.mouse_pos_x yy = self.mouse_pos_y line = (math.sqrt(xx**2 + yy**2)) print(f'line = {line} xx == {xx} yy== {yy}') print( f'self.bender.real_x = {self.bender.real_x} self.bender.real_y = {self.bender.real_y}' ) self.mouse_cos = xx / line self.mouse_sin = yy / line if -1 <= self.mouse_cos <= 1 and -1 <= self.mouse_sin <= 1 and self.gun1_status == True: new_bullet = Bullet(self, self.bender.direction) self.bullets.add(new_bullet) if self.bender.direction == 'left': self.rect = self.camera.apply(self.bender) self.rect.x -= 25 self.rect.y += 60 self.mouse_pos_x, self.mouse_pos_y = mouse_pos self.mouse_pos_x = self.mouse_pos_x - self.rect.x self.mouse_pos_y = self.mouse_pos_y - self.rect.y print( f'self.mouse_pos_x = {self.mouse_pos_x}, self.mouse_pos_y = {self.mouse_pos_y}' ) xx = self.mouse_pos_x yy = self.mouse_pos_y line = (math.sqrt(xx**2 + yy**2)) print(f'line = {line} xx == {xx} yy== {yy}') print( f'self.bender.real_x = {self.bender.real_x} self.bender.real_y = {self.bender.real_y}' ) self.mouse_cos = xx / line self.mouse_sin = yy / line if -1 <= self.mouse_cos <= 1 and -1 <= self.mouse_sin <= 1 and self.gun1_status == True: new_bullet = Bullet(self, self.bender.direction) self.bullets.add(new_bullet) def jump_of_hero(self): ##hit in a celling during a jump self.checking_for_a_celling() if self.bender.jump_activate < self.bender.height_jump and self.bender.jump_up == True: self.bender.real_y -= self.bender.size_of_jump # real self.bender.jump_activate += 1 elif self.bender.jump_activate == self.bender.height_jump and self.bender.jump_up == True: self.bender.jump_up = False self.bender.jump_activate -= 1 self.bender.real_y += self.bender.size_of_jump elif self.bender.jump_activate > 0 and self.bender.jump_up == False: self.bender.real_y += self.bender.size_of_jump self.bender.jump_activate -= 1 elif self.bender.jump_activate == 0 and self.bender.jump_up == False: self.bender.jump_act = False def checking_for_a_celling(self): self.bender.celling = False for x, y in self.a_map.hard_wall_left_to_right[( (self.bender.real_y) // TILE) - 1]: if x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y - self.bender.size_of_jump <= y + TILE: # LEFT TOP self.bender.jump_up = False self.bender.celling = True if x <= self.bender.real_x + self.bender.X_WIGHT / 2 <= x + TILE and y <= self.bender.real_y - self.bender.size_of_jump <= y + TILE: # LEFT MIDLE self.bender.jump_up = False self.bender.celling = True if x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y - self.bender.size_of_jump <= y + TILE: # RIGHT TOP self.bender.jump_up = False self.bender.celling = True def run(self): while True: for r in range(1000): self._check_events() self.screen.fill(BGR_COLOR) self.jump_of_hero() #BOTTOM #looking for a surface fall = True for x, y in self.a_map.hard_wall_left_to_right[( (self.bender.real_y + self.bender.Y_HEIGHT) // TILE)]: if (x <= self.bender.real_x <= x + TILE and y <= self.bender.real_y + self.bender.Y_HEIGHT + self.bender.size_of_jump <= y + 5 # LEFT DOWN or x <= self.bender.real_x + self.bender.X_WIGHT / 2 <= x + TILE and y <= self.bender.real_y + self.bender.Y_HEIGHT <= y + 5 # MIDLE DOWN or x <= self.bender.real_x + self.bender.X_WIGHT <= x + TILE and y <= self.bender.real_y + self.bender.Y_HEIGHT <= y + 5): # RIGHT DOWN self.bender.jump_activate = 0 self.bender.jump_up = False fall = False # LEFT SIDE #checking_for_the_left_brick step_left = False step_right = False if self.move_left == True: if fall or self.bender.jump_activate != 0: #to avoid akward momens in the air for y, x in self.a_map.hard_wall[( (self.bender.real_x) // TILE) - 1]: if (x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y <= y + TILE # LEFT TOP or x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE <= y + TILE # LEFT MIDLE or x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 2 <= y + TILE # downer LEFT MIDLE or x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 3 <= y + TILE # mode downer LEFT MIDLE or x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 4 <= y + TILE): # bottom LEFT MIDLE self.move_left = False elif not fall or self.bender.jump_activate == 0: for y, x in self.a_map.hard_wall[( (self.bender.real_x) // TILE) - 1]: if (x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y <= y + TILE # LEFT TOP or x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE <= y + TILE # LEFT MIDLE or x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 2 <= y + TILE # downer LEFT MIDLE or x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 3 <= y + TILE): # mode downer LEFT MIDLE self.move_left = False #checking_for_a_step for y, x in self.a_map.hard_wall[( (self.bender.real_x) // TILE)]: if x <= self.bender.real_x - self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 4 <= y + TILE: #step step_left = True print(True) break self.checking_for_a_celling() #during climing if step_left == True and self.move_left != False and self.bender.celling != True: if r % 2 == 0: #it is for hard climing!!!! self.bender.real_x -= TILE self.bender.real_y += -TILE ##moving to left if step_left == False and self.move_left == True: self.bender.real_x -= self.bender.speed print('moving left') # RIGHT #checking_for_the_right_brick if self.move_right == True: if fall or self.bender.jump_activate != 0: #to avoid akward momens in the air for y, x in self.a_map.hard_wall[( (self.bender.real_x + self.bender.X_WIGHT) // TILE) + 1]: if (x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y <= y + TILE # right TOP or x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE <= y + TILE # right MIDLE or x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 2 <= y + TILE # downer right MIDLE or x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 3 <= y + TILE # mode downer right MIDLE or x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 4 <= y + TILE): # bottom right MIDLE self.move_right = False elif not fall or self.bender.jump_activate == 0: for y, x in self.a_map.hard_wall[( (self.bender.real_x + self.bender.X_WIGHT) // TILE) + 1]: if (x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y <= y + TILE # right TOP or x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE <= y + TILE # right MIDLE or x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 2 <= y + TILE # downer right MIDLE or x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 3 <= y + TILE): # mode downer right MIDLE self.move_right = False #checking_for_a_step for y, x in self.a_map.hard_wall[( (self.bender.real_x + self.bender.X_WIGHT) // TILE)]: if x <= self.bender.real_x + self.bender.X_WIGHT + self.bender.speed <= x + TILE and y <= self.bender.real_y + TILE * 4 <= y + TILE: #step step_right = True print(True) break self.checking_for_a_celling() #during climing if step_right == True and self.move_right != False and self.bender.celling != True: if r % 2 == 0: #it is for hard climing!!!! self.bender.real_x += TILE self.bender.real_y += -TILE ##moving to right if step_right == False and self.move_right == True: self.bender.real_x += self.bender.speed print('moving right') ### fallen if fall and self.bender.jump_act == False: self.bender.real_y += self.bender.size_of_jump self.bender.rect.x = self.bender.real_x self.bender.rect.y = self.bender.real_y self.camera.update(self.bender) self.a_map.draw(self) #TESTING I am watching neded row that way #for y,x in self.a_map.hard_wall[((self.bender.real_x + self.bender.X_WIGHT) // TILE)+1]:#self.bender.real_y // TILE]: for x,y in self.a_map.hard_wall_left_to_right[(self.bender.real_y // TILE)-1]: #for x,y in self.a_map.hard_wall_left_to_right[((self.bender.real_y) // TILE)]: #for y,x in self.a_map.hard_wall[((self.bender.real_x + self.bender.X_WIGHT) // TILE)+1]: for y, x in self.a_map.hard_wall[( (self.bender.real_x) // TILE) - 1]: square = pygame.Rect(x, y, TILE, TILE) pygame.draw.rect(self.screen, DARKGRAY, self.camera.apply_wall(square), 2) if r % 20 == 0: self.bot2.bore() if r % 6 == 0: self.bender.bore() self.bender.blitme(self.camera.apply(self.bender)) for bot1 in self.bots1: bot1.blitme(self.camera.apply(bot1)) for bot2 in self.bots2: bot2.blitme(self.camera.apply(bot2)) if self.gun1_status == True: self.gun1.draw(self.camera.apply(self.bender), self.bender.direction, self.fire) if self.fire == True: self.fire = False for bullet in self.bullets: for bot1 in self.bots1: if bot1.x_on_a_map <= bullet.x_on_a_map <= bot1.x_on_a_map + bot1.size_x and bot1.y_on_a_map <= bullet.y_on_a_map <= bot1.y_on_a_map + bot1.size_y: self.bots1.remove(bot1) self.bullets.remove(bullet) for bot2 in self.bots2: if bot2.rect.x <= bullet.x_on_a_map <= bot2.rect.x + bot2.size_x and bot2.rect.y <= bullet.y_on_a_map <= bot2.rect.y + bot2.size_y: self.bots2.remove(bot2) self.bullets.remove(bullet) ## If you wanna destroy black and white squares ##for row in range(len(self.a_map.world_map_left_to_right)): ##for xy in range(len(self.a_map.world_map_left_to_right[row])): ##if self.a_map.world_map_left_to_right[row][xy][0] <= bullet.x_on_a_map <= self.a_map.world_map_left_to_right[row][xy][0] +TILE and self.a_map.world_map_left_to_right[row][xy][1] <= bullet.y_on_a_map <= self.a_map.world_map_left_to_right[row][xy][1] +TILE: ##self.bullets.remove(bullet) ##self.a_map.world_map_left_to_right[row].pop(xy) ##break first_flag_break = False for row in range(len(self.a_map.hard_wall)): if first_flag_break == True: break for xy in range(len(self.a_map.hard_wall[row])): if self.a_map.hard_wall[row][xy][ 1] <= bullet.x_on_a_map <= self.a_map.hard_wall[ row][xy][1] + TILE and self.a_map.hard_wall[ row][xy][ 0] <= bullet.y_on_a_map <= self.a_map.hard_wall[ row][xy][0] + TILE: self.bullets.remove(bullet) #print(f'self.a_map.hard_wall[row][xy] {self.a_map.hard_wall[row][xy]}') flag_break = False for lr_row in range( len(self.a_map.hard_wall_left_to_right) ): if flag_break == True: break for xy_lr in range( len(self.a_map. hard_wall_left_to_right[lr_row] )): if (self.a_map.hard_wall[row][xy][1] == self.a_map. hard_wall_left_to_right[lr_row] [xy_lr][0] and self.a_map.hard_wall[row] [xy][0] == self.a_map. hard_wall_left_to_right[lr_row] [xy_lr][1]): self.a_map.hard_wall_left_to_right[ lr_row].pop(xy_lr) flag_break = True break self.a_map.hard_wall[row].pop(xy) first_flag_break = True break if bullet.need_to_delete() == True: self.bullets.remove(bullet) bullet.update() for bullet in self.bullets: bullet.draw_bullet(self.camera.apply(bullet)) pygame.display.flip() self.clock.tick(FPS)
def __init__(self): pygame.init() self.settings = Settings() self.bullets = pygame.sprite.Group() self.enemies = pygame.sprite.Group() self.evil_bullets = pygame.sprite.Group() self.a_map = World_map() self.screen = pygame.display.set_mode( (self.settings.screen_width,self.settings.screen_height)) pygame.display.set_caption("Invasion") self.level = 0 self.stats = Stats(self) temp_x, temp_y = 200, 50 temp_color = (0, 100, 0) temp_text_color =(255, 255, 255) self.play_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "PLAY") self.play_button.rect.y -= 65 self.play_button._prep_msg("NEW GAME") temp_x, temp_y = 200, 50 temp_color = (20, 40, 0) temp_text_color =(255, 255, 255) self.exit_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "EXIT") self.exit_button.rect.y += 185 self.exit_button._prep_msg("EXIT") temp_x, temp_y = 200, 50 temp_color = (80, 60, 0) temp_text_color =(255, 255, 255) self.title_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "TITLE") self.title_button.rect.y += 125 self.title_button._prep_msg("TITLE") self.title = Title(self) self.title_active = False temp_x, temp_y = 200, 50 temp_color = (150, 200, 0) temp_text_color =(255, 255, 255) self.help_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "HELP") self.help_button.rect.y += 65 self.help_button._prep_msg("HELP") self.help_ = Help(self) self.help_active = False temp_x, temp_y = 200, 50 temp_color = (150, 200, 0) temp_text_color =(255, 255, 255) self.back_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "BACK") self.back_button.rect.y += 185 self.back_button._prep_msg("BACK") temp_x, temp_y = 250, 50 temp_color = (0, 240, 0) temp_text_color =(255, 255, 255) self.restart_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "Restart level") temp_x, temp_y = 350, 50 temp_color = (100, 100, 100) temp_text_color =(255, 0, 0) self.win_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "YOU ARE A WINNER!") self.myTank = Ship(self) self.myTank.live = 0 #interface self.liveboard = Lives(self) self.score = 0 self.scoreboard = Score(self) self.image = pygame.image.load('images/brick_wall3.png') self.rect = self.image.get_rect() self.aid = pygame.image.load('images/aid.png')
class Invasion: def __init__(self): pygame.init() self.settings = Settings() self.bullets = pygame.sprite.Group() self.enemies = pygame.sprite.Group() self.evil_bullets = pygame.sprite.Group() self.a_map = World_map() self.screen = pygame.display.set_mode( (self.settings.screen_width,self.settings.screen_height)) pygame.display.set_caption("Invasion") self.level = 0 self.stats = Stats(self) temp_x, temp_y = 200, 50 temp_color = (0, 100, 0) temp_text_color =(255, 255, 255) self.play_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "PLAY") self.play_button.rect.y -= 65 self.play_button._prep_msg("NEW GAME") temp_x, temp_y = 200, 50 temp_color = (20, 40, 0) temp_text_color =(255, 255, 255) self.exit_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "EXIT") self.exit_button.rect.y += 185 self.exit_button._prep_msg("EXIT") temp_x, temp_y = 200, 50 temp_color = (80, 60, 0) temp_text_color =(255, 255, 255) self.title_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "TITLE") self.title_button.rect.y += 125 self.title_button._prep_msg("TITLE") self.title = Title(self) self.title_active = False temp_x, temp_y = 200, 50 temp_color = (150, 200, 0) temp_text_color =(255, 255, 255) self.help_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "HELP") self.help_button.rect.y += 65 self.help_button._prep_msg("HELP") self.help_ = Help(self) self.help_active = False temp_x, temp_y = 200, 50 temp_color = (150, 200, 0) temp_text_color =(255, 255, 255) self.back_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "BACK") self.back_button.rect.y += 185 self.back_button._prep_msg("BACK") temp_x, temp_y = 250, 50 temp_color = (0, 240, 0) temp_text_color =(255, 255, 255) self.restart_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "Restart level") temp_x, temp_y = 350, 50 temp_color = (100, 100, 100) temp_text_color =(255, 0, 0) self.win_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "YOU ARE A WINNER!") self.myTank = Ship(self) self.myTank.live = 0 #interface self.liveboard = Lives(self) self.score = 0 self.scoreboard = Score(self) self.image = pygame.image.load('images/brick_wall3.png') self.rect = self.image.get_rect() self.aid = pygame.image.load('images/aid.png') def delete_all_bullets(self): self.evil_bullets.empty() self.bullets.empty() def level_1(self): self.level = 1 #show lives self.liveboard.prepare(self) # destroyd enemies self.score = 0 self.scoreboard.prepare(self) self.a_map.create_map() #create a map 1 self.delete_all_bullets() #self.myTank.out_coordinates = [] self.myTank.create_map() #create a map self.myTank.right_coordinates() # set new coordinates #self.myEnemy = Enemy(self) #print('LEVEL 111111111111111111111111111111') doNE self.enemies_list_coordinate = [] for i in range(15): #20 #self.create_an_enemy() self.create_an_enemy2() #add coordianates self.settings.bgr_color = WHITE self.image = pygame.image.load('images/brick_wall3.png') #self.image = pygame.image.load('images/brick_wall.png') def level_2(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall2.png') self.enemies_list_coordinate = [] for i in range(6): # 10 9 #self.create_an_enemy() self.create_an_enemy2() def level_3(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall.png') self.enemies_list_coordinate = [] for i in range(10): #12 #self.create_an_enemy() self.create_an_enemy2() def level_4(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall2.png') self.enemies_list_coordinate = [] for i in range(8): #10 15 #self.create_an_enemy() self.create_an_enemy2() def level_5(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall.png') self.enemies_list_coordinate = [] for i in range(10): #10 17 #self.create_an_enemy() self.create_an_enemy2() def _check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() self._check_play_button(mouse_pos) self._check_exit_button(mouse_pos) self._check_title_button(mouse_pos) self._check_help_button(mouse_pos) self._check_restart_button(mouse_pos) self._check_back_button(mouse_pos) self._check_win_button(mouse_pos) if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit() if event.key == pygame.K_RIGHT: #self.myTank.rect.x += self.myTank.ship_speed self.myTank.moving_right = True if event.key == pygame.K_LEFT: #self.myTank.rect.x -= self.myTank.ship_speed self.myTank.moving_left = True if event.key == pygame.K_UP: #self.myTank.rect.y -= self.myTank.ship_speed self.myTank.moving_up = True if event.key == pygame.K_DOWN: #self.myTank.rect.y += self.myTank.ship_speed self.myTank.moving_down = True if event.key == pygame.K_SPACE: self.fire_bullets() if event.key == pygame.K_BACKSPACE: self._check_enter() if event.key == pygame.K_r: self._check_restart() if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT: self.myTank.moving_right = False if event.key == pygame.K_LEFT: self.myTank.moving_left = False if event.key == pygame.K_UP: self.myTank.moving_up = False if event.key == pygame.K_DOWN: self.myTank.moving_down = False def _check_exit_button(self,mouse_pos): if self.exit_button.rect.collidepoint(mouse_pos) and self.myTank.live == 0 and not self.stats.game_active and not self.help_active: # invisible button musnt be worked sys.exit() def _check_enter(self): self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK self.level_1() pygame.mouse.set_visible(False) def _check_restart(self): if self.level == 0: return 0 self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK #show lives self.liveboard.prepare(self) # destroyd enemies self.score = 0 self.scoreboard.prepare(self) if self.level == 1: self.level_1() elif self.level == 2: self.level_2() elif self.level == 3: self.level_3() elif self.level == 4: self.level_4() elif self.level == 5: self.level_5() pygame.mouse.set_visible(False) def _check_restart_button(self,mouse_pos): if self.restart_button.rect.collidepoint(mouse_pos) and self.myTank.live == 0 and not self.stats.game_active and not self.help_active: # invisible button musnt be worked if self.level == 0: return 0 self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK #show lives self.liveboard.prepare(self) # destroyd enemies self.score = 0 self.scoreboard.prepare(self) if self.level == 1: self.level_1() elif self.level == 2: self.level_2() elif self.level == 3: self.level_3() elif self.level == 4: self.level_4() elif self.level == 5: self.level_5() pygame.mouse.set_visible(False) def _check_title_button(self, mouse_pos): if self.title_button.rect.collidepoint(mouse_pos) and self.myTank.live == 0 and not self.stats.game_active and not self.help_active: # invisible button musnt be worked self.title_active = True pygame.mouse.set_visible(False) def _check_help_button(self, mouse_pos): if self.help_button.rect.collidepoint(mouse_pos) and self.myTank.live == 0 and not self.stats.game_active and not self.help_active: # invisible button musnt be worked self.help_active = True #pygame.mouse.set_visible(False) def _check_back_button(self, mouse_pos): if self.back_button.rect.collidepoint(mouse_pos) and self.help_active == True and not self.stats.game_active: # invisible button musnt be worked self.help_active = False #pygame.mouse.set_visible(False) def _check_play_button(self,mouse_pos): if self.play_button.rect.collidepoint(mouse_pos) and self.myTank.live == 0 and not self.stats.game_active and not self.help_active: # invisible button musnt be worked self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK self.level_1() pygame.mouse.set_visible(False) #print(dir(self.enemies)) def _check_win_button(self,mouse_pos): if self.win_button.rect.collidepoint(mouse_pos) and self.myTank.live > 0 and not self.stats.game_active and not self.help_active: # invisible button musnt be worked self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK self.level_1() pygame.mouse.set_visible(False) def fire_bullets(self): if len(self.bullets) <= BULLET_LIMIT: #my fire limit new_bullet = Bullet(self, self.myTank.direction,self.level) self.bullets.add(new_bullet) def fire_evil_bullets(self,enemy): new_bullet = Evil_bullet(self, enemy.rect.x, enemy.rect.y, enemy.direction,self.level) self.evil_bullets.add(new_bullet) def create_an_enemy2(self): new_enemy = Enemy(self) if self.level == 1: new_enemy.create_map() #CHANGE a map #new_enemy.ship_speed = 0.25 #CHANGE LEVEL DIFICULTY #print('ITTTTTTTTTTTTTTTTTTTTT') pass elif self.level == 2: new_enemy.change_map() elif self.level == 3: new_enemy.change_map_3() elif self.level == 4: new_enemy.change_map_4() elif self.level == 5: new_enemy.change_map_5() #new_enemy.ship_speed = 0.5 new_enemy.add_coordinates(self.myTank.rect.x, self.myTank.rect.y) #for temp in self.a_map.aid_list: #new_enemy.(temp[0],temp[1]) for x,y in self.enemies_list_coordinate: new_enemy.add_coordinates(x,y) #new_enemy.set_zero_coordinates() # return False if limit is out temp = new_enemy.right_coordinates() x = new_enemy.rect.x y= new_enemy.rect.y if temp != False: ###for enemy_update in self.enemies: ##enemy_update.add_coordinates(x, y)dd self.enemies_list_coordinate.append([x,y]) #self.add_coordinates(x,y) self.enemies.add(new_enemy) def levels(self): if len(self.enemies) == 0 and self.level == 1: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 2 self.myTank.change_map() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_2_world_map() self.level_2() elif len(self.enemies) == 0 and self.level == 2: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 3 self.myTank.change_map_3() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_3_world_map() self.level_3() elif len(self.enemies) == 0 and self.level == 3: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 4 self.myTank.change_map_4() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_4_world_map() self.level_4() elif len(self.enemies) == 0 and self.level == 4: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 5 self.myTank.change_map_5() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_5_world_map() self.level_5() elif len(self.enemies) == 0 and self.level == 5: pass #print(dir(self.evil_bullets)) #print('WIN WIN') #self.level_4() #restart game def lose_or_win(self): if self.myTank.live == 0: self.stats.game_active = False pygame.mouse.set_visible(True) pass #del self.myTank elif self.myTank.live > 0 and len(self.enemies) == 0 and self.level == 5: # IF WIN CHANGE LEVEL self.stats.game_active = False # IF WIN pygame.mouse.set_visible(True) # IF WIN def _update_screen(self): self.screen.fill(self.settings.bgr_color) self.myTank.blitme() for row in self.a_map.world_map_list: # CAN I update this one? for x,y in row: self.rect.x = x self.rect.y = y self.screen.blit(self.image, self.rect) #pygame.draw.rect(self.screen, DARKGRAY, (x, y, TILE, TILE),2) #for bullet in self.bullets.sprites(): #bullet.draw_bullet() #for bullet in self.evil_bullets.sprites(): #bullet.draw_bullet() #self.myEnemy.blitme() #for enemy in self.enemies.sprites(): #enemy.blitme() if not self.stats.game_active and self.myTank.live == 0 : self.play_button.draw_button() self.restart_button.draw_button() self.help_button.draw_button() self.title_button.draw_button() self.exit_button.draw_button() if self.title_active == True: self.title.draw_title() pygame.display.flip() #SHOW ALL update_screen time.sleep(2) self.title_active = False if self.help_active == True: self.help_.draw_title() self.back_button.draw_button() pygame.display.flip() #SHOW ALL update_screen #time.sleep(3) #self.help_active = False else: self.liveboard.draw() self.scoreboard.draw() if not self.stats.game_active and self.myTank.live > 0 and len(self.enemies) == 0 : self.win_button.draw_button() #pygame.display.flip() def run(self): #self.level = 1 while(True): for i in range(100): #if not self.stats.game_active: self.lose_or_win() self._check_events() self.myTank.empty_coordinates_others() #self.liveboard.prepare() #SHOW ALL update_screen self._update_screen() #animation of destroy and Delete if self.stats.game_active == True: #or self.stats.game_active == True: # self.myTank.live > 0 or not winner #for temp in self.a_map.aid_list: #print(temp[0],temp[1]) self.levels() for enemy in self.enemies: #update coordinates into enemies and myTank enemy.coordinates_others = [] self.myTank.coordinates_others = [] #for temp in self.a_map.aid_list: #self.myTank.add_coordinates(temp[0],temp[1]) #print(temp[0],temp[1]) for enemy_update in self.enemies: enemy.add_coordinates(enemy_update.rect.x, enemy_update.rect.y) enemy.add_coordinates(self.myTank.rect.x, self.myTank.rect.y) for temp in self.a_map.aid_list: enemy.add_coordinates(temp[0],temp[1]) self.myTank.add_coordinates(enemy_update.rect.x, enemy_update.rect.y) #live circle of enemy if enemy.live == True and i == 0: pass enemy.rand_direction() #enemy is shoting self.fire_evil_bullets(enemy) if i != 0: if enemy.live == True and i%2 == 0 or i%3 == 0: # speed enemies added or i%3 == 0 # print(f'i= {i}')# speed enemie3s enemy.update() if enemy.live == False and i == 0: self.score += 1 self.scoreboard.prepare(self) self.enemies.remove(enemy) #SHOW ALL update_screen enemy.blitme() #for in if len(self.a_map.aid_list) != 0: myTank_rightTopX = self.myTank.rect.x + self.myTank.SIZE myTank_lefttBottomY = self.myTank.rect.y + self.myTank.SIZE #temp = 0 for a in self.a_map.aid_list: x = a[0] y = a[1] self.rect.x = x self.rect.y = y rightTopX = x + 31 lefttBottomY = y + 31 if (rightTopX >= myTank_rightTopX >= x) and (lefttBottomY >= self.myTank.rect.y >= y) or (rightTopX >= myTank_rightTopX >= x) and (lefttBottomY >= myTank_lefttBottomY >= y) or (rightTopX >= self.myTank.rect.x >= x) and (lefttBottomY >= myTank_lefttBottomY >= y) or (rightTopX >= self.myTank.rect.x >= x) and (lefttBottomY >= self.myTank.rect.y >= y): #temp += 2 ################??????????????????????????? self.a_map.aid_list.remove(a) #temp += 2 self.myTank.live += 2 self.liveboard.prepare(self) #break else: self.screen.blit(self.aid, self.rect) self.myTank.update() #self.bullets.update() for bullet in self.evil_bullets.sprites(): if (self.myTank.rect.x + self.myTank.SIZE >= bullet.x >= self.myTank.rect.x) and (self.myTank.rect.y + self.myTank.SIZE >= bullet.y >= self.myTank.rect.y): #enemy.live = False #self.enemies.remove(enemy) print('You has been attacked') self.myTank.live -= 1 #show lives self.liveboard.prepare(self) self.evil_bullets.remove(bullet) #wall if bullet.update(): self.evil_bullets.remove(bullet) else: bullet.draw_bullet() #SHOW ALL update_screen # How to move and delete bullet for bullet in self.bullets.sprites(): #FIRE FIRE FIRE to enemies #step = True for enemy in self.enemies: if (enemy.rect.x + enemy.SIZE >= bullet.x >= enemy.rect.x) and (enemy.rect.y + enemy.SIZE >= bullet.y >= enemy.rect.y): #step = False enemy.live = False #self.enemies.remove(enemy) self.bullets.remove(bullet) #wall temp = bullet.update() if temp == True: self.bullets.remove(bullet) else: bullet.draw_bullet() #SHOW ALL update_screen pygame.display.flip() #SHOW ALL update_screen
class Invasion: def __init__(self): pygame.init() self.settings = Settings() self.bullets = pygame.sprite.Group() self.enemies = pygame.sprite.Group() self.evil_bullets = pygame.sprite.Group() self.a_map = World_map() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption("Invasion") self.level = 0 self.stats = Stats(self) temp_x, temp_y = 200, 50 temp_color = (0, 255, 0) temp_text_color = (255, 255, 255) self.play_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "PLAY") temp_x, temp_y = 350, 50 temp_color = (100, 100, 100) temp_text_color = (255, 0, 0) self.win_button = Button(self, temp_x, temp_y, temp_color, temp_text_color, "YOU ARE A WINNER!") self.myTank = Ship(self) self.myTank.live = 0 self.image = pygame.image.load('images/brick_wall3.png') self.rect = self.image.get_rect() def delete_all_bullets(self): self.evil_bullets.empty() self.bullets.empty() def level_1(self): self.level = 1 self.a_map.create_map() #create a map 1 self.delete_all_bullets() #self.myTank.out_coordinates = [] self.myTank.create_map() #create a map self.myTank.right_coordinates() # set new coordinates #self.myEnemy = Enemy(self) #print('LEVEL 111111111111111111111111111111') doNE self.enemies_list_coordinate = [] for i in range(6): #self.create_an_enemy() self.create_an_enemy2() #add coordianates self.settings.bgr_color = WHITE self.image = pygame.image.load('images/brick_wall3.png') def level_2(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall2.png') self.enemies_list_coordinate = [] for i in range(9): #self.create_an_enemy() self.create_an_enemy2() def level_3(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall.png') self.enemies_list_coordinate = [] for i in range(12): #self.create_an_enemy() self.create_an_enemy2() def level_4(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall2.png') self.enemies_list_coordinate = [] for i in range(15): #self.create_an_enemy() self.create_an_enemy2() def level_5(self): self.delete_all_bullets() self.settings.bgr_color = LIGHTGRAY self.image = pygame.image.load('images/brick_wall.png') self.enemies_list_coordinate = [] for i in range(17): #self.create_an_enemy() self.create_an_enemy2() def _check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() self._check_play_button(mouse_pos) self._check_win_button(mouse_pos) if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit() if event.key == pygame.K_RIGHT: #self.myTank.rect.x += self.myTank.ship_speed self.myTank.moving_right = True if event.key == pygame.K_LEFT: #self.myTank.rect.x -= self.myTank.ship_speed self.myTank.moving_left = True if event.key == pygame.K_UP: #self.myTank.rect.y -= self.myTank.ship_speed self.myTank.moving_up = True if event.key == pygame.K_DOWN: #self.myTank.rect.y += self.myTank.ship_speed self.myTank.moving_down = True if event.key == pygame.K_SPACE: self.fire_bullets() if event.key == pygame.K_BACKSPACE: self._check_enter() if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT: self.myTank.moving_right = False if event.key == pygame.K_LEFT: self.myTank.moving_left = False if event.key == pygame.K_UP: self.myTank.moving_up = False if event.key == pygame.K_DOWN: self.myTank.moving_down = False def _check_enter(self): self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK self.level_1() pygame.mouse.set_visible(False) def _check_play_button(self, mouse_pos): if self.play_button.rect.collidepoint( mouse_pos ) and self.myTank.live == 0 and not self.stats.game_active: # invisible button musnt be worked self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK self.level_1() pygame.mouse.set_visible(False) #print(dir(self.enemies)) def _check_win_button(self, mouse_pos): if self.win_button.rect.collidepoint( mouse_pos ) and self.myTank.live > 0 and not self.stats.game_active: # invisible button musnt be worked self.enemies.empty() self.stats.game_active = True self.myTank.live = LIVE_OF_TANK self.level_1() pygame.mouse.set_visible(False) def fire_bullets(self): if len(self.bullets) <= BULLET_LIMIT: #my fire limit new_bullet = Bullet(self, self.myTank.direction, self.level) #if self.level == 2: ##new_bullet.change_map() self.bullets.add(new_bullet) def fire_evil_bullets(self, enemy): new_bullet = Evil_bullet(self, enemy.rect.x, enemy.rect.y, enemy.direction, self.level) #if self.level == 2: ##new_bullet.change_map() self.evil_bullets.add(new_bullet) #def create_an_enemy(self): #new_enemy = Enemy(self) #self.enemies.add(new_enemy) def create_an_enemy2(self): new_enemy = Enemy(self) if self.level == 1: new_enemy.create_map() #CHANGE a map #new_enemy.ship_speed = 0.25 #CHANGE LEVEL DIFICULTY #print('ITTTTTTTTTTTTTTTTTTTTT') pass elif self.level == 2: new_enemy.change_map() elif self.level == 3: new_enemy.change_map_3() elif self.level == 4: new_enemy.change_map_4() elif self.level == 5: new_enemy.change_map_5() #new_enemy.ship_speed = 0.5 new_enemy.add_coordinates(self.myTank.rect.x, self.myTank.rect.y) for x, y in self.enemies_list_coordinate: new_enemy.add_coordinates(x, y) #new_enemy.get_direction() #new_enemy.update() #???????????????? #new_enemy.set_zero_coordinates() # return False if limit is out temp = new_enemy.right_coordinates() x = new_enemy.rect.x y = new_enemy.rect.y if temp != False: ###for enemy_update in self.enemies: ##enemy_update.add_coordinates(x, y)dd self.enemies_list_coordinate.append([x, y]) #self.add_coordinates(x,y) self.enemies.add(new_enemy) def levels(self): if len(self.enemies) == 0 and self.level == 1: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 2 self.myTank.change_map() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_2_world_map() self.level_2() elif len(self.enemies) == 0 and self.level == 2: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 3 self.myTank.change_map_3() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_3_world_map() self.level_3() elif len(self.enemies) == 0 and self.level == 3: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 4 self.myTank.change_map_4() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_4_world_map() self.level_4() elif len(self.enemies) == 0 and self.level == 4: #print(dir(self.evil_bullets))# == 0: #print('WIN WIN') self.level = 5 self.myTank.change_map_5() self.myTank.right_coordinates() # set new coordinates for my tank self.a_map.level_5_world_map() self.level_5() elif len(self.enemies) == 0 and self.level == 5: pass #print(dir(self.evil_bullets)) #print('WIN WIN') #self.level_4() #restart game def lose_or_win(self): if self.myTank.live == 0: self.stats.game_active = False pygame.mouse.set_visible(True) pass #del self.myTank elif self.myTank.live > 0 and len( self.enemies) == 0 and self.level == 5: # IF WIN CHANGE LEVEL self.stats.game_active = False # IF WIN pygame.mouse.set_visible(True) # IF WIN def _update_screen(self): self.screen.fill(self.settings.bgr_color) self.myTank.blitme() for x, y in self.a_map.world_map: self.rect.x = x self.rect.y = y self.screen.blit(self.image, self.rect) #pygame.draw.rect(self.screen, DARKGRAY, (x, y, TILE, TILE),2) for bullet in self.bullets.sprites(): bullet.draw_bullet() for bullet in self.evil_bullets.sprites(): bullet.draw_bullet() #self.myEnemy.blitme() for enemy in self.enemies.sprites(): enemy.blitme() if not self.stats.game_active and self.myTank.live == 0: self.play_button.draw_button() if not self.stats.game_active and self.myTank.live > 0 and len( self.enemies) == 0: self.win_button.draw_button() pygame.display.flip() def run(self): #self.level = 1 while (True): for i in range(100): #if not self.stats.game_active: self.lose_or_win() self._check_events() self.myTank.empty_coordinates_others() #animation of destroy and Delete if self.stats.game_active == True: # self.myTank.live > 0 or not winner self.levels() for enemy in self.enemies: #update coordinates into enemies and myTank enemy.coordinates_others = [] self.myTank.coordinates_others = [] for enemy_update in self.enemies: enemy.add_coordinates(enemy_update.rect.x, enemy_update.rect.y) enemy.add_coordinates(self.myTank.rect.x, self.myTank.rect.y) self.myTank.add_coordinates( enemy_update.rect.x, enemy_update.rect.y) #live circle of enemy if enemy.live == True and i == 0: pass enemy.rand_direction() #enemy is shoting self.fire_evil_bullets(enemy) if i != 0: if enemy.live == True and i % 2 == 0 or i % 3 == 0: # speed enemies added or i%3 == 0 # print(f'i= {i}')# speed enemie3s enemy.update() if enemy.live == False and i == 0: self.enemies.remove(enemy) self.myTank.update() #self.bullets.update() for bullet in self.evil_bullets: if (self.myTank.rect.x + self.myTank.SIZE >= bullet.x >= self.myTank.rect.x) and ( self.myTank.rect.y + self.myTank.SIZE >= bullet.y >= self.myTank.rect.y): #enemy.live = False #self.enemies.remove(enemy) print('You has been attacked') self.myTank.live -= 1 self.evil_bullets.remove(bullet) #wall if bullet.update(): self.evil_bullets.remove(bullet) pass # How to move and delete bullet for bullet in self.bullets: #FIRE FIRE FIRE to enemies #step = True for enemy in self.enemies: if (enemy.rect.x + enemy.SIZE >= bullet.x >= enemy.rect.x) and ( enemy.rect.y + enemy.SIZE >= bullet.y >= enemy.rect.y): #step = False enemy.live = False #self.enemies.remove(enemy) self.bullets.remove(bullet) #wall temp = bullet.update() if temp == True: self.bullets.remove(bullet) self._update_screen()