Pygame在一个方向上移动图像

时间:2015-05-28 05:00:24

标签: python pygame

这是我在这里的第一篇文章,如果有人能告诉我如何将图像移动到某一点,我将非常感激。这是我的代码:

class bullet:

image = "bullet.png"
spr = pygame.image.load(image).convert_alpha()
x = 0
y = 0
def __init__(self, a, b):
    self.x = a
    self.y = b

    while self.x > 0 and self.x < 500 and self.y > 0 and self.y < 500:#the boundries for the bullet
    CODE GOES IN HERE TO MOVE IMAGE

2 个答案:

答案 0 :(得分:1)

看一下这个例子:

import pygame
import base64
import math
import random

# some functions for vector math

def magnitude(v):
    return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def add(u, v):
    return [(a+b) for (a, b) in zip(u, v)]

def sub(u, v):
    return [(a-b) for (a, b) in zip(u, v)]

def dot(u, v):
    return sum((a*b) for a, b in zip(u, v))

def normalize(v):
    vmag = magnitude(v)
    return [ v[i]/vmag  for i in range(len(v)) ]

def length(v):
  return math.sqrt(dot(v, v))

def angle(v1, v2):
  return math.acos(dot(v1, v2) / (length(v1) * length(v2)))

def get_image():
    dec = base64.b64decode(img, 'UTF-8')
    surf = pygame.image.fromstring(dec, (32, 32), 'RGBA')
    return surf

def rot_center(image, angle):
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image

class Actor(object):

    surf = None

    def __init__(self):
        self.x, self.y = (0, 0)
        self.speed, self.angle = 1.7, 0
        self.target_vector = [0, 0]

    @property
    def img(self):
        if not self.surf:
            self.surf = get_image()
        return self.surf


    @property
    def pos(self):
        return self.x, self.y

    @property
    def int_pos(self):
        return map(int, self.pos)

    @property
    def center_pos(self):
        return [a-16 for a in self.int_pos]

    def update(self):
        if self.speed == 0:
            return

        # apply speed to target_vector
        move_vector = [c * self.speed for c in normalize(self.target_vector)]

        # update position by adding the position vector to the movment vector
        self.x, self.y = add(self.pos, move_vector)

    def draw(self, s):
        s.blit(rot_center(self.img, self.angle), self.center_pos)

def fire(start, angle, target_vector):
    act = Actor()
    act.x, act.y = start
    act.angle = angle
    act.target_vector = target_vector
    return act

def main():    
    pygame.init()
    quit = False
    s = pygame.display.set_mode((300, 300))
    c = pygame.time.Clock()
    pos = (134, 268)
    actors = []
    FIRE = pygame.USEREVENT + 1
    pygame.time.set_timer(FIRE, 1000)
    while not quit:

        quit = pygame.event.get(pygame.QUIT)

        if pygame.event.get(FIRE):
            angle = random.randint(0, 180)
            print 'angle is ', angle
            # calculate vector that represents the direction
            rangle = math.radians(-angle)
            target_vector = normalize((math.cos(rangle), math.sin(rangle)))
            actors.append(fire(pos, angle, target_vector))

        pygame.event.poll()
        for ar in actors:
            ar.update()
        s.fill((255, 255, 255))
        for ar in actors:
            ar.draw(s)
        pygame.display.flip()
        c.tick(60)

img = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAOwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFEAAADUAAAAXgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAP8AAADTAAAAVAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQ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TAAAATwAAAP8AAADTAAAA3QAAABUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACHAAAAhwAAAIcAAACHAAAAhwAAAIcAAACHAAAAhwAAAIcAAACHAAAAhwAAAIcAAACHAAAAhgAAAK0AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAA7wAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAA7wAAAC8AAAAAAAAAAAAAAAAAAAAAAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAADwAAAAAAAAAAAAAAAAAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAAUgAAAFMAAAAAAAAAAAAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAGQAAAAAAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAIAAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAAggAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAGUAAAAAAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAPoAAABUAAAAAAAAAAAAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAAPgAAAAAAAAAAAAAAAAAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAA7wAAADAAAAAAAAAAAAAAAAAAAAAAAAAAhwAAAIcAAACHAAAAhwAAAIcAAACHAAAAhwAAAIcAAACHAAAAhwAAAIcAAACHAAAAhwAAAIYAAACtAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAPAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEsAAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADTAAAATwAAAP8AAADdAAAAFQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAP8AAADTAAAAVAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABRAAAATgAAAF4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4AAAA7AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='

if __name__ == '__main__':
    main()

这里,随机角度被转换为用于移动对象的向量。

enter image description here

答案 1 :(得分:0)

首先,您需要一个屏幕来设置显示:

size = (640, 480) # window size
screen = pygame.display.set_mode(size) # screen where the image will be displayed

您需要使用screen.blit来显示图像。它将图像对象和x,y坐标作为参数。在你的情况下它应该是:

screen.blit(spr, (x,y)) # draws image at x,y coordinate