def test_linsys_rref2(): p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='2') s = LinearSystem([p1, p2]) r = s.compute_rref() assert r[0] == p1 assert r[1] == Plane(constant_term='1')
def test_linsys_swap_rows(): p0 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1') p1 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2') p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3') p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2') s = LinearSystem([p0, p1, p2, p3]) s.swap_rows(0, 1) assert s[0] == p1 assert s[1] == p0 assert s[2] == p2 assert s[3] == p3 s.swap_rows(1, 3) assert s[0] == p1 assert s[1] == p3 assert s[2] == p2 assert s[3] == p0 s.swap_rows(3, 1) assert s[0] == p1 assert s[1] == p0 assert s[2] == p2 assert s[3] == p3
def test_gaussian_elimination_init(): p1 = Plane(normal_vector=Vector(['0', '0', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['0', '-1', '0']), constant_term='-2') p3 = Plane(normal_vector=Vector(['5', '0', '0']), constant_term='15') s = LinearSystem([p1, p2, p3]) r = s.gaussian_elimination() assert r == Vector(['3', '2', '1'])
def test_gaussian_elimination_no_solution(): p1 = Plane(normal_vector=Vector(['1', '2', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['0', '-1', '0']), constant_term='-2') p3 = Plane(normal_vector=Vector(['2', '4', '2']), constant_term='-4') s = LinearSystem([p1, p2, p3]) r = s.gaussian_elimination() assert r == 'No solutions'
def test_when_seat_taken_is_correct_seat_correct_placements_increases( self): fake_passenger_assignments = [2, 1, 3, 5, 4] size_of_plane = 5 subject = Plane(fake_passenger_assignments, size_of_plane) subject.take_seat(3, 3) assert subject.correct_placements == 1
def test_linsys_triangular_form2(): p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='2') s = LinearSystem([p1, p2]) t = s.compute_triangular_form() assert t[0] == p1 assert t[1] == Plane(constant_term='1')
def test_when_pick_random_seat_a_seat_is_chosen_from_the_available_seats( self): fake_passenger_assignments = [2, 1, 3, 5, 4] size_of_plane = 5 subject = Plane(fake_passenger_assignments, size_of_plane) actual = subject.pick_random_seat() assert actual in subject.available_seats
def test_when_take_seat_and_seat_is_incorrectly_taken_already_a_different_seat_is_chosen( self): fake_passenger_assignments = [2, 1, 3, 5, 4] size_of_plane = 5 subject = Plane(fake_passenger_assignments, size_of_plane) subject.take_seat(3, 4) subject.take_seat(3, 3) assert subject.correct_placements == 0
def test_board_given_first_person_picks_correct_board_was_successful(self): fake_passenger_assignments = [2, 1, 3, 5, 4] size_of_plane = 5 mock_random = MagicMock() mock_random.random_number = MagicMock(return_value=1) subject = Plane(fake_passenger_assignments, size_of_plane, mock_random) subject.board() result = subject.successful_board assert result is True
def run(): number_of_succesful_boards = 0 plane_tickets = Plane.create_list_of_all_seats(100) for i in range(0, 1000): plane = Plane(plane_tickets, 100) plane.board() if plane.successful_board: number_of_succesful_boards += 1 probability = (number_of_succesful_boards / 1000) * 100 print( "\nThe probability over the course of 1000 boardings of the last passenger getting their correct seat is {:0.2f}%.\n" .format(probability))
def test_linsys_multiply_coefficient(): p0 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2') p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3') p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2') s = LinearSystem([p0, p1, p2, p3]) assert s[0] == p0 assert s[1] == p1 assert s[2] == p2 assert s[3] == p3 s.multiply_coefficient_and_row(1, 0) assert s[0] == p0 assert s[1] == p1 assert s[2] == p2 assert s[3] == p3 s.multiply_coefficient_and_row(-1, 2) assert s[0] == p0 assert s[1] == p1 assert s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') assert s[3] == p3 s.multiply_coefficient_and_row(10, 1) assert s[0] == p0 assert s[1] == Plane(normal_vector=Vector(['10', '10', '10']), constant_term='10') assert s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') assert s[3] == p3
def test_linsys_init(): p0 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1') p1 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2') p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3') p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2') s = LinearSystem([p0, p1, p2, p3]) assert s.planes[0] == Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1') assert s.planes[1] == Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2') assert s.planes[2] == Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3') assert s.planes[3] == Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
def test_linsys_rref3(): p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2') p3 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3') p4 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2') s = LinearSystem([p1, p2, p3, p4]) r = s.compute_rref() assert r[0] == Plane(normal_vector=Vector(['1', '0', '0']), constant_term='0') assert r[1] == p2 assert r[2] == Plane(normal_vector=Vector(['0', '0', '-2']), constant_term='2') assert r[3] == Plane()
def test_linsys_rref4(): p1 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['1', '-1', '1']), constant_term='2') p3 = Plane(normal_vector=Vector(['1', '2', '-5']), constant_term='3') s = LinearSystem([p1, p2, p3]) r = s.compute_rref() assert r[0] == Plane(normal_vector=Vector(['1', '0', '0']), constant_term=Decimal('23') / Decimal('9')) assert r[1] == Plane(normal_vector=Vector(['0', '1', '0']), constant_term=Decimal('7') / Decimal('9')) assert r[2] == Plane(normal_vector=Vector(['0', '0', '1']), constant_term=Decimal('2') / Decimal('9'))
def test_linsys_triangular_form4(): p1 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['1', '-1', '1']), constant_term='2') p3 = Plane(normal_vector=Vector(['1', '2', '-5']), constant_term='3') s = LinearSystem([p1, p2, p3]) t = s.compute_triangular_form() assert t[0] == Plane(normal_vector=Vector(['1', '-1', '1']), constant_term='2') assert t[1] == Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1') assert t[2] == Plane(normal_vector=Vector(['0', '0', '-9']), constant_term='-2')
class TestPlane(unittest.TestCase): def setUp(self): self.plane = Plane() self.plane.land() def test_land_sets_grounded_status_to_true(self): self.assertEqual(self.plane.grounded, True) def test_land_raises_error_if_plane_grounded(self): self.assertRaises(TypeError, self.plane.land) def test_take_off_sets_grounded_status_to_false(self): self.plane.take_off() self.assertEqual(self.plane.grounded, False) def test_take_off_raises_error_if_plane_not_grounded(self): self.plane.take_off() self.assertRaises(TypeError, self.plane.take_off)
def test_linsys_multiply_times_row(): p0 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2') p1 = Plane(normal_vector=Vector(['10', '10', '10']), constant_term='10') p2 = Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2') s = LinearSystem([p0, p1, p2, p3]) assert s[0] == p0 assert s[1] == p1 assert s[2] == p2 assert s[3] == p3 s.add_multiple_times_row_to_row(0, 0, 1) assert s[0] == p0 assert s[1] == p1 assert s[2] == p2 assert s[3] == p3 s.add_multiple_times_row_to_row(1, 0, 1) assert s[0] == p0 assert s[1].normal_vector == Vector(['10', '11', '10']) assert s[1].constant_term == 12 assert s[1] == Plane(normal_vector=Vector(['10', '11', '10']), constant_term='12') assert s[2] == p2 assert s[3] == p3 s.add_multiple_times_row_to_row(-1, 1, 0) assert s[0] == Plane(normal_vector=Vector(['-10', '-10', '-10']), constant_term='-10') assert s[1] == Plane(normal_vector=Vector(['10', '11', '10']), constant_term='12') assert s[2] == p2 assert s[3] == p3
def setUp(self): self.plane = Plane() self.plane.land()
def main(): me_destroy_index = 0 bullets = [] pygame.mixer.music.play(loops=-1) # loops = -1,音乐无限循环(默认循环播放一次) running = True delay = 60 # 对一些效果进行延迟,效果更好一些 plane = Plane(bg_size) switch_image = False enemies = pygame.sprite.Group() # 生成敌方飞机组(一种精灵组用以存储所有敌机精灵) small_enemies = pygame.sprite.Group() # 敌方小型飞机组(不同型号敌机创建不同的精灵组来存储) add_small_enemies(small_enemies, enemies, 4) # 生成若干敌方小型飞机 # 定义子弹, 各种敌机和我方敌机的毁坏图像索引 bullet_index = 0 e1_destroy_index = 0 plane_destroy_index = 0 # 定义子弹实例化个数 bullet1 = [] bullet_num = 6 for i in range(bullet_num): bullet1.append(Bullet(plane.rect.midtop)) while running: # 绘制背景图 screen.blit(backgroud, (0, 0)) # 飞机是喷气式的, 那么这个就涉及到一个帧数的问题 clock = pygame.time.Clock() clock.tick(60) # 绘制我方飞机的两种不同状态,喷气时与不喷气时 if not delay % 3: switch_image = not switch_image for each in small_enemies: if each.active: each.move() screen.blit(each.image, each.rect) pygame.draw.line(screen, color_black, (each.rect.left, each.rect.top - 5), (each.rect.right, each.rect.top - 5), 2) energy_remain = each.energy / SmallEnemy.energy # 如果血量大约百分之二十则为绿色,否则为红色 if energy_remain > 0.2: energy_color = color_green else: energy_color = color_red pygame.draw.line( screen, energy_color, (each.rect.left, each.rect.top - 5), (each.rect.left + each.rect.width * energy_remain, each.rect.top - 5), 2) # 随机循环输出敌方小飞机 for e in small_enemies: e.move() screen.blit(e.image, e.rect) else: while e1_destroy_index == 0: e1_destroy_index_second = 0 screen.blit(each.destroy_images[e1_destroy_index_second], each.rect) e1_destroy_index = (e1_destroy_index_second + 1) % 4 e1_destroy_index_second += 1 enemy1_die_sound.play() each.reset() # 飞机存活状态 if plane.active: if switch_image: screen.blit(plane.image_one, plane.rect) else: screen.blit(plane.image_two, plane.rect) # 飞机存活状态下才能发射子弹,且没10帧发射一颗移动的子弹 if not (delay % 10): bullet_sound.play() bullets = bullet1 bullets[bullet_index].reset(plane.rect.midtop) bullet_index = (bullet_index + 1) % bullet_num for b in bullets: # 只有激活的子弹才能击中敌机 if b.active: b.move() screen.blit(b.image, b.rect) enemies_hit = pygame.sprite.spritecollide( b, enemies, False, pygame.sprite.collide_mask) # 如果子弹击中敌机 if enemies_hit: b.active = False # 子弹损毁 for e in enemies_hit: e.active = False # 小型飞机被毁 # 毁坏状态绘制爆炸的场面 else: if not (delay % 3): while me_destroy_index == 0: screen.blit(plane.destroy_images[plane_destroy_index], plane.rect) # 四张图片切换实现动画效果 me_destroy_index = (plane_destroy_index + 1) % 4 plane_destroy_index += 1 # 爆炸声音效果 me_die_sound.play() plane.reset() # 调用 pygame 实现的碰撞方法 spritecollide (我方飞机如果和敌机碰撞, 更改飞机的存活属性) enemies_down = pygame.sprite.spritecollide(plane, enemies, False, pygame.sprite.collide_mask) if enemies_down: plane.active = False for enemy in enemies: enemy.active = False # 响应用户的操作 for event in pygame.event.get(): # 如果按下屏幕上的关闭按钮,触发quit事件,游戏退出 if event.type == 12: pygame.quit() sys.exit() if delay == 0: delay = 60 delay -= 1 # 获取用户输入的所有键盘序列,如向上 key_pressed = pygame.key.get_pressed() if key_pressed[K_w] or key_pressed[K_UP]: plane.move_up() if key_pressed[K_s] or key_pressed[K_DOWN]: plane.move_down() if key_pressed[K_a] or key_pressed[K_LEFT]: plane.move_left() if key_pressed[K_d] or key_pressed[K_RIGHT]: plane.move_right() # 再而我们将背景图像输出到屏幕上 pygame.display.flip()
bg_size = 480, 700 # 初始化游戏背景大小(宽, 高) screen = pygame.display.set_mode(bg_size) # 设置背景对话框 pygame.display.set_caption("Plane Wars") # 设置标题 backgroud = pygame.image.load( os.path.join(BASE_DIR, "material/image/background.png")) # 加载背景图片,并设置为不透明 # 血槽颜色绘制 color_black = (0, 0, 0) color_green = (0, 255, 0) color_red = (255, 0, 0) color_white = (255, 255, 255) # 获取我方飞机 plane = Plane(bg_size) def add_small_enemies(group1, group2, num): """ 添加小型敌机 指定多个敌机对象添加到精灵组(sprite.group) 参数group1、group2是两个精灵组类型的形参,用以存储多个精灵对象(敌机)。 需要注意的一点是group既然是特定的精灵组结构体,在向其内部添加精灵对象时需要调用其对应的成员函数add() """ for i in range(num): small_enemy = SmallEnemy(bg_size) group1.add(small_enemy) group2.add(small_enemy)
def test_Plane_parallel(): plane1 = Plane(Vector([1, 1, 1]), 1) plane2 = Plane(Vector([1, 1, 1]), 2) assert plane1 != plane2 assert plane1.isParallel(plane2)
def test_Plane_init_labels(): plane1 = Plane(normal_vector=Vector([2, 2, 2]), constant_term=3) assert isinstance(plane1, Plane) assert plane1.normal_vector == Vector([2, 2, 2]) assert plane1.constant_term == 3
def test_null_Plane(): plane1 = Plane() assert isinstance(plane1, Plane) assert plane1.normal_vector == Vector([0, 0, 0]) assert plane1.constant_term == 0
def test_is_first_passenger_returns_false_if_they_are_not_the_first(self): fake_passenger_assignments = [2, 1, 3, 5, 4] size_of_plane = 5 subject = Plane(fake_passenger_assignments, size_of_plane) result = subject.is_first_passenger(subject.passengers_assignments[1]) assert result is False
def test_gaussian_elimination_infinite_solutions(): p1 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1') p2 = Plane(normal_vector=Vector(['0', '-1', '-1']), constant_term='-1') s = LinearSystem([p1, p2]) r = s.gaussian_elimination()
def test_Plane_parallel_identical(): plane1 = Plane(Vector([1, 1, 1]), 1) plane2 = Plane(Vector([1, 1, 1]), 1) assert plane1 == plane2 assert plane1.isParallel(plane2)
def test_when_take_seat_the_seat_is_removed_from_available_seats(self): fake_passenger_assignments = [2, 1, 3, 5, 4] size_of_plane = 5 subject = Plane(fake_passenger_assignments, size_of_plane) subject.take_seat(3, 3) assert 3 not in subject.available_seats
def test_when_a_plane_is_created_a_list_of_seats_is_generated_from_size_given( self): fake_passenger_assignments = [2, 1, 3, 5, 4] size_of_plane = 5 subject = Plane(fake_passenger_assignments, size_of_plane) assert len(subject.available_seats) == 5
def drawScene(): global camerapos, first_run glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() cube = Cube() plane = Plane() # Set up the current maze view. # Reset position to zero, rotate around y-axis, restore position. glTranslatef(0.0, 0.0, 0.0) glRotatef(camerarot, 0.0, 1.0, 0.0) glTranslatef(camerapos[0], camerapos[1], camerapos[2]) # Draw floor. glPushMatrix() glTranslatef(0.0, -2.0, 0.0) glScalef(30.0, 1.0, 30.0) plane.drawplane(floortexture, 40.0) glPopMatrix() # Draw ceiling. glPushMatrix() glTranslatef(0.0, 2.0, 0.0) glRotatef(180.0, 0.0, 0.0, 1.0) glScalef(30.0, 1.0, 30.0) plane.drawplane(ceilingtexture, 50.0) glPopMatrix() # Build the maze like a printer; back to front, left to right. row_count = 0 column_count = 0 wall_x = 0.0 wall_z = 0.0 for i in map: wall_z = (row_count * (cubesize * -1)) for j in i: # 1 = cube, 0 = empty space. if (j == 1): cube.drawcube(walltexture, 1.0) wall_x = (column_count * (cubesize * -1)) if (first_run != True): print('Drawing cube at X:', wall_x, 'Z:', wall_z) # Move from left to right one cube size. glTranslatef(cubesize, 0.0, 0.0) column_count += 1 # Reset position before starting next row, while moving # one cube size towards the camera. glTranslatef(((cubesize * column_count) * -1), 0.0, cubesize) row_count += 1 # Reset the column count; this is a new row. column_count = 0 glutSwapBuffers() handleInput() if (first_run != True): first_run = True
def test_Plane_init_nolabels(): plane1 = Plane(normal_vector=Vector([1, 1, 1]), constant_term=5) assert isinstance(plane1, Plane) assert plane1.normal_vector == Vector([1, 1, 1]) assert plane1.constant_term == 5