pygame 飞机大战子弹的编写(八)用精灵类实现导弹的跟踪系统,旋转图像

目标:1 使用精灵类

          2 实现精灵图像的任意角度旋转

          3 实现类似导弹的跟踪系统

很多文章介绍了图像的转换功能,但都是基于 Surface.blit(image,rect())这个功能。一旦使用在精灵类里,图像漫天飞舞。

偶然看到 pygame自带的chimp示例,里面打大猩猩的游戏,打中后大猩猩会旋转,巧的是,他是基于精灵的类。

经过不停的删除测试,剥离最简代码,加上之前写的文章里的跟踪弹知识,终于玩出了跟踪弹的写法。只是不明白为什么要

if not self.rotate:
            self.original = self.image
            self.rotate = True

这么搞一下,直接用self.orginal = self.image 图像也会漫天飞。

 

import os, pygame
from pygame.locals import *

import math
vect = pygame.Vector2


class Chimp(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)  # call Sprite intializer
        self.image = pygame.image.load('e:/missle.png')
        # colorkey = self.image.get_at((0, 1))
        # self.image.set_colorkey(colorkey, RLEACCEL)
        self.rect = self.image.get_rect()
        self.rect.topleft = 200, 100
        self.angle = 0
        self.x_speed = 1
        self.y_speed = 1
        self.speed = 3
        self.rotate = False

    def update(self):
        if not self.rotate:
            self.original = self.image
            self.rotate = True
        if self.rotate:
            center = self.rect.center
            rotate = pygame.transform.rotate
            self.image = rotate(self.original, -self.angle )
            self.rect = self.image.get_rect(center=center)
            print(self.rect)
        print(self.rotate)
        angle = self.angle * math.pi / 180
        self.x_speed = self.speed * math.cos(angle)
        self.y_speed = self.speed * math.sin(angle)
        self.rect.x += self.x_speed
        self.rect.y += self.y_speed

    def punched(self, pos1, pos2):
        pos0 = vect(0, 0)
        arch = pos2 - pos1
        angle = pos0.angle_to(arch)
        self.angle = angle


def main():
    pygame.init()
    screen = pygame.display.set_mode((468, 500))
    pygame.display.set_caption('missle demo')
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

    if pygame.font:
        font = pygame.font.Font(None, 36)
        text = font.render("missle demo", 1, (10, 10, 10))
        textpos = text.get_rect(centerx=background.get_width() / 2)
        background.blit(text, textpos)

    screen.blit(background, (0, 0))
    clock = pygame.time.Clock()
    chimp = Chimp()
    allsprites = pygame.sprite.Group()
    allsprites.add(chimp)

    while True:
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                cur_pos = pygame.mouse.get_pos()
                pos1 = vect(chimp.rect.centerx, chimp.rect.centery)
                pos2 = vect(cur_pos[0], cur_pos[1])
                chimp.punched(pos1, pos2)
            else:
                pass
        allsprites.update()
        screen.blit(background, (0, 0))
        allsprites.draw(screen)
        pygame.display.flip()

if __name__ == '__main__':
    main()

 

你可能感兴趣的:(python,pygame)