中途岛海战飞行阵列之椭圆

中途岛海战飞行阵列之椭圆

这是本人编写的飞机大战中途岛海战的敌机类模块。这些敌机飞行的阵列为椭圆形。这里单独列出以供给有需要的人士。

下面是部分代码预览:

"""
   中途岛海战飞行阵列之椭圆,
   本程序新建敌机类,然后让敌机绕着椭圆的轨迹不断地移动。
"""
import math
import pygame
from pygame.locals import *

class Enemy(pygame.sprite.Sprite):
    """
       绕椭圆飞行的敌机类
    """
    def __init__(self,image,a,b,rotate_center):
        """
           iamge:图形,a:长半轴,b:短半轴,rotate_center:旋转中心点
        """
        pygame.sprite.Sprite.__init__(self)
        self.rc = rotate_center  # 飞机的旋转中心

    def rotate(self):
        """根据和x轴的角度旋转图形"""
        self.image = pygame.transform.rotate(self.rawimage,self._angle)
        self.rect = self.image.get_rect()
        r = math.radians(self._angle)   # 把角度值转为弧度值
        x = self.a * math.cos(r)        # 根据椭圆参数方程算x值

    def update(self):
        """更新"""
        self._angle += 1

def main():
    """主要函数"""
    bg = 'sea.png'
    image = "Raven_128x128_red.png"
    width,height = size = 800,600
    
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("中途岛海战飞行阵列之椭圆by lixingqiu")
    bg = pygame.image.load(bg)
    
    pygame.time.set_timer(USEREVENT,1000) # 设置自定义事件定时器
    clock = pygame.time.Clock()
    running = True
    while running:
             
        group.update()
        screen.blit(bg,(0,0))
        group.draw(screen)
        pygame.display.update()
        clock.tick(60)
        
    pygame.quit()

if __name__ == "__main__":

    main()    

如需要查看完整源代码,请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。