All pastes #71624 Raw Edit

Something

public text v1 · immutable
#71624 ·published 2006-06-25 23:35 UTC
rendered paste body
import pygame, math, random
from pygame.locals import *
import sys

pygame.init()
myscreen = pygame.display.set_mode((640,480))

r = random.randint
speed = 10.0
korv = []
mouse = 0

class Korv(pygame.sprite.Sprite):
    def __init__(self, destination):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((20,20))
        self.image.fill( (r(0,255), r(0,255), r(0,255)) )
        self.rect = self.image.get_rect()
        self.rect.center = (320,240)
        self.vector = GetVector(destination, self.rect.center)
        self.dest = destination

    def update(self):
        x, y = self.vector
        x = x * speed
        y = y * speed
        self.rect.centerx += x
        self.rect.centery += y

def GetVector(pA, pB):
    px = pA[0] - pB[0]
    py = pA[1] - pB[1]
    factor = math.sqrt(px ** 2 + py ** 2)

    return (px / factor, py / factor)

while 1:
    myscreen.fill([0, 0, 0])
    pygame.time.delay(15)
    for b in korv:
        b.update()
        myscreen.blit(b.image, b.rect)

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
 
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse = 1
        if event.type == pygame.MOUSEBUTTONUP:
            mouse = 0

        if mouse and event.type == pygame.MOUSEMOTION:
            korv.append(Korv(pygame.mouse.get_pos()))