这是本人编写的飞机大战中途岛海战的玩家所操控的敌机8字型阵列模块。以供给有需要的人士。
下面是部分代码预览:
"""
中途岛海战敌机的八字飞行阵列,
本程序新建敌机类,然后让敌机绕着画8的轨迹不断地移动。
"""
import math
import pygame
from pygame.locals import *
class Enemy8(pygame.sprite.Sprite):
"""
绕8字飞行的敌机类
"""
amounts = 100 # 出动的数量
counter = 0 # 计数器
end = False
def __init__(self,image,pos):
"""
image:图形,pos:初始坐标
"""
pygame.sprite.Sprite.__init__(self)
# 由于图像为向上,把它转为向右朝向的造型,以便
# 和它默认的朝向在视觉上一致
self.rawimage = pygame.transform.rotate(image,-90)
self._angle = 180 # 和x轴的夹角
self.rotate_image()
self.rect = self.image.get_rect(center=pos)
self.frame_counter = 0
Enemy8.counter += 1 # 统计敌机数目
def setheading(self,angle):
"""设置和x轴的夹角,相当于方向"""
def right(self,da):
"""向右旋转da度"""
def left(self,da):
"""向左旋转da度"""
def rotate_image(self):
"""根据和x轴的角度旋转图形"""
def fd(self,distance):
"""朝当前方向移动一定的距离"""
def update(self):
"""更新"""
def main():
"""主要函数"""
bg = 'images/海洋.png'
image = "images/Raven_128x128_red.png"
width,height = size = 960,720
screen = pygame.display.set_mode(size)
pygame.display.set_caption("中途岛海战八字飞行阵列by lixingqiu")
bg = pygame.image.load(bg)
# 下面准备把图像缩小
image = pygame.image.load(image)
w,h = image.get_size()
image = pygame.transform.scale(image,(w//3,h//3))
group = pygame.sprite.Group()
pygame.time.set_timer(USEREVENT,200) # 设置自定义事件定时器
clock = pygame.time.Clock()
Enemy = Enemy8
p = image,(width//2,100)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:running=False
if Enemy.counter < Enemy.amounts:
if event.type == USEREVENT:
group.add(Enemy(*p))
group.update()
screen.blit(bg,(0,0)) # 渲染背景
group.draw(screen) # 画每架飞机
pygame.display.update() # 更新显示
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

