def getRotationMatrix(angle): radiansAngle = math_radians(angle) # 9 digits of precision should be more than enough sinAngle = round(math_sin(radiansAngle), 9) cosAngle = round(math_cos(radiansAngle), 9) return Mat33([[cosAngle, -sinAngle, 0], [sinAngle, cosAngle, 0], [0, 0, 1]])
def show_start_screen(): #Title titleFont = pygame.font.Font(const.BASICFONT, 100) titleSurf = titleFont.render('Asteroids', True, const.WHITE, const.BGCOLOR) displacement = 1 state = 0 #Objects asteroid_list = [] battleShip = obj.Ship( [const.WINDOW_WIDTH // 2 - 15, 2 * const.WINDOW_HEIGHT // 3, 10, 10]) slow_ship = True #Buttons StartButton = obj.Button( const.DARKGREEN, const.GREEN, (const.WINDOW_WIDTH // 2 - 50, const.WINDOW_HEIGHT * 2 // 3, 100, 40)) pygame.mixer.music.play() pygame.key.set_repeat(1, 1) while True: if random_randrange(25) == 0: asteroid_list.append(obj.Asteroid()) const.DISPLAYSURF.fill(const.BGCOLOR) titleRect = titleSurf.get_rect() titleRect.center = (const.WINDOW_WIDTH / 2, const.WINDOW_HEIGHT / 2 + displacement) for i in asteroid_list: i.move() battleShip.move(slow_ship) const.DISPLAYSURF.blit(titleSurf, titleRect) for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location) StartButton.Func('Start', const.WHITE, 20) #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() elif event.type == pygame.MOUSEBUTTONDOWN: if StartButton.Press(): pass #start game elif event.type == pygame.KEYDOWN: slow_ship = False if event.key == pygame.K_LEFT: battleShip.angle += 3 elif event.key == pygame.K_RIGHT: battleShip.angle -= 3 elif event.key == pygame.K_DOWN: battleShip.acceleration = [ math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180) ] elif event.key == pygame.K_UP: battleShip.acceleration = [ -math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180) ] elif event.type == pygame.KEYUP: slow_ship = True pygame.display.update() const.FPSCLOCK.tick(15) if displacement > 10: state = 0 elif displacement < -10: state = 1 if state: displacement += 1 else: displacement -= 1
def play(): #Objects asteroid_list = [] bullet_list = [] battleShip = obj.Ship((const.WINDOW_WIDTH // 2 - const.SPRITE_SHIP.get_width() //2 , const.WINDOW_HEIGHT // 2 - const.SPRITE_SHIP.get_height() // 2)) slow_ship = True const.DISPLAYSURF.fill(const.BGCOLOR) #clear the screen #Music settings pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play() pygame.key.set_repeat(1, 1) while True: if random_randrange(25) == 0: asteroid_list.append( obj.Asteroid() ) const.DISPLAYSURF.fill(const.BGCOLOR) for i in asteroid_list: i.move() battleShip.move(slow_ship) for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) if len(bullet_list) != 0: for i in bullet_list: i.move(battleShip.image.get_width()) const.DISPLAYSURF.blit(i.image, i.location) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location ) #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() elif event.type == pygame.KEYDOWN: slow_ship = False if event.key == pygame.K_LEFT: battleShip.angle += 3 elif event.key == pygame.K_RIGHT: battleShip.angle -= 3 elif event.key == pygame.K_DOWN: battleShip.acceleration = [math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180)] elif event.key == pygame.K_UP: battleShip.acceleration = [-math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180)] elif event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: bullet_list.append(obj.Bullet(battleShip.location[0] + const.SPRITE_SHIP.get_width() // 2 - const.SPRITE_BULLET.get_width() // 2, battleShip.location[1] + const.SPRITE_SHIP.get_height() // 4, battleShip.angle)) const.BLAST.play() else: slow_ship = True pygame.display.update() const.FPSCLOCK.tick(10)
def qcos(a): return Q(math_cos(a.number), "cos(%s)", a.units, abs(a.uncert * math_sin(a.number)), a.prefu, (a,))
def test_prim_cos(x): return math_cos(x)
def move(self, width): self.location[0] -= 30*math_sin(self.angle) self.location[1] -= 30*math_cos(self.angle)
def play(): #Objects asteroid_list = [] bullet_list = [] battleShip = obj.Ship((const.WINDOW_WIDTH // 2 - const.SPRITE_SHIP.get_width() //2 , const.WINDOW_HEIGHT // 2 - const.SPRITE_SHIP.get_height() // 2)) slow_ship = True list_explosion = [] lives = 2 lives_string = 'Lives: ' + str(lives) lives_font = pygame.font.Font(const.BASICFONT, 20) lives_surf = lives_font.render(lives_string, True, const.WHITE) lives_rect = lives_surf.get_rect() lives_rect.center = (lives_surf.get_width(), const.WINDOW_HEIGHT - lives_surf.get_height()) score = 0 score_string = 'Score: ' + str(score) score_font = pygame.font.Font(const.BASICFONT, 20) score_surf = score_font.render(score_string, True, const.WHITE) score_rect = score_surf.get_rect() score_rect.center = (const.WINDOW_WIDTH - score_surf.get_width(), const.WINDOW_HEIGHT - score_surf.get_height()) const.DISPLAYSURF.fill(const.BGCOLOR) #clear the screen #Music settings pygame.mixer.music.set_volume(0.2) pygame.mixer.music.play() while lives > 0: #Generating the asteroids if random_randrange(15) == 0: asteroid_list.append( obj.Asteroid() ) const.DISPLAYSURF.fill(const.BGCOLOR) const.DISPLAYSURF.blit(const.BG, (0,0)) for i in asteroid_list: i.move() if len(bullet_list) != 0: for i in bullet_list: if battleShip != 0: i.move(battleShip.image.get_width()) const.DISPLAYSURF.blit(i.image, i.location) for j in asteroid_list: if i.check_collision(j): asteroid_list.remove(j) bullet_list.remove(i) list_explosion.append(obj.Explosion(j)) score += 1 break if battleShip != 0: battleShip.move(slow_ship) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location ) for i in asteroid_list: if battleShip != 0: if battleShip.check_collision(i): pos_exp = battleShip.location battleShip = 0 lives -= 1 for j in range(len(asteroid_list)): asteroid_list.pop() for i in const.EXP_LIST: const.DISPLAYSURF.fill(const.BGCOLOR) const.DISPLAYSURF.blit(const.BG, (0,0)) const.DISPLAYSURF.blit(i, pos_exp) pygame.display.update() const.FPSCLOCK.tick(15) else: continue for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) score_string = 'Score: ' + str(score) score_surf = score_font.render(score_string, True, const.WHITE) const.DISPLAYSURF.blit(score_surf, score_rect) lives_string = 'Lives: ' + str(lives) lives_surf = lives_font.render(lives_string, True, const.WHITE) const.DISPLAYSURF.blit(lives_surf, lives_rect) if len(list_explosion) != 0: for i in list_explosion: i.draw() #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() if battleShip == 0: battleShip = obj.Ship((const.WINDOW_WIDTH // 2 - const.SPRITE_SHIP.get_width() //2 , const.WINDOW_HEIGHT // 2 - const.SPRITE_SHIP.get_height() // 2)) else: list_key = [] list_key = pygame.key.get_pressed() if len(list_key) != 0: slow_ship = True if list_key[pygame.K_LEFT]: battleShip.angle += 10 slow_ship = False if list_key[pygame.K_RIGHT]: battleShip.angle -= 10 slow_ship = False if list_key[pygame.K_DOWN]: battleShip.acceleration = [math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180)] slow_ship = False if list_key[pygame.K_UP]: battleShip.acceleration = [-math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180)] slow_ship = False if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: bullet_list.append(obj.Bullet(battleShip.location[0] + const.SPRITE_SHIP.get_width() // 2 - const.SPRITE_BULLET.get_width() // 2, battleShip.location[1] + const.SPRITE_SHIP.get_height() // 4, battleShip.angle)) const.BLAST.play() #delete the bullets that can't be seen on the screen for i in bullet_list: if i.location[0] < 0 or i.location[0] > const.WINDOW_WIDTH\ or i.location[1] < 0 or i.location[1] > const.WINDOW_HEIGHT: bullet_list.remove(i) #Same for asteroids for i in asteroid_list: if i.location[0] + i.image.get_width() < 0 or i.location[0] - i.image.get_width() > const.WINDOW_WIDTH\ or i.location[1] + i.image.get_height() < 0 or i.location[1] - i.image.get_height() > const.WINDOW_HEIGHT: asteroid_list.remove(i) for i in list_explosion: if i.size > 30: list_explosion.remove(i) pygame.display.update() const.FPSCLOCK.tick(15) pygame.mixer.music.stop() game_over()
def move(self, width): #self.location[0] = self.location[0] - abs(math_sin(math_pi*self.angle/90)*width/math_sqrt(8)) #self.location[1] = self.location[1] - abs(math_sin(math_pi*self.angle/90)*width/math_sqrt(8)) self.location[0] -= 30*math_sin(self.angle) self.location[1] -= 30*math_cos(self.angle)
def show_start_screen(): #Title titleFont = pygame.font.Font(const.BASICFONT, 100) titleSurf = titleFont.render('Asteroids', True, const.WHITE, const.BGCOLOR) displacement = 1 state = 0 #Objects asteroid_list = [] battleShip = obj.Ship([const.WINDOW_WIDTH // 2 - 15, 2 * const.WINDOW_HEIGHT // 3, 10, 10]) #Buttons StartButton = obj.Button(const.DARKGREEN, const.GREEN, (const.WINDOW_WIDTH // 2 - 50, const.WINDOW_HEIGHT * 2 // 3, 100, 40)) pygame.mixer.music.play() pygame.key.set_repeat(1, 1) while True: if random_randrange(25) == 0: asteroid_list.append( obj.Asteroid() ) const.DISPLAYSURF.fill(const.BGCOLOR) titleRect = titleSurf.get_rect() titleRect.center = (const.WINDOW_WIDTH / 2, const.WINDOW_HEIGHT / 2 + displacement) for i in asteroid_list: i.move() battleShip.move() const.DISPLAYSURF.blit(titleSurf, titleRect) for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location ) StartButton.Func('Start', const.WHITE, 20) #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() elif event.type==pygame.MOUSEBUTTONDOWN: if StartButton.Press(): pass #start game elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: battleShip.angle += 3 elif event.key == pygame.K_RIGHT: battleShip.angle -= 3 elif event.key == pygame.K_DOWN: battleShip.acceleration = [math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180)] elif event.key == pygame.K_UP: battleShip.acceleration = [-math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180)] pygame.display.update() const.FPSCLOCK.tick(15) if displacement > 10: state = 0 elif displacement < -10: state = 1 if state: displacement += 1 else: displacement -= 1