Pages

Thursday, March 6, 2014

pygame vector image bounce with Vector class

##### mostly this code is re-written, but if I had to look for it, maybe you did too
##  if you cut and paste (which you should type yourself to learn)  just the
## import  math, and vector class , and the stuff that's
## edited out at the bottom, you can see what the vector class does
## I looked forever for a code to make the object bounce, or follow.
## nobody really says, "you have to have a vector class, or module to do it"
## apparently you do.   If not, let me know in comments, I'd love to learn how


import pygame
from pygame.locals import*
from sys import exit
import math
#from book by Will McGugan Beginning Game Development with Python and Pygame
#and modification from stackoverflow.com,  Dominic Kexel
class Vector2(tuple):
   
    def __new__(typ, x = 1.0, y = 1.0):
        n = tuple.__new__(typ, (int(x), int(y)))
        n.x = x
        n.y = y
        return n
    def __str__(self):
        return "(%s, %s)"%(self.x, self.y)
    def __add__(self, other): 
        return self.__new__(type(self), self.x + other.x, self.y + other.y)
   
    def __sub__(self, other):
        return self.__new__(type(self), self.x - other.x, self.y - other.y)
   
    def __neg__(self, other):
        return self.__new__(type(self), -self.x, -self.y)

    def __mul__(self, scalar):
        return self.__new__(type(self), self.x * scalar, self.y * scalar)
   
    def __div__(self, scalar):
        return self.__new__(type(self), self.x / scalar, self.y / scalar)

    @staticmethod
    def from_points(P1, P2):
        return Vector2( P2[0] - P1[0], P2[1] - P1[1] )

    def get_magnitude(self):
        return math.sqrt( self.x**2 + self.y**2 )

    def normalize(self):
        magnitude = self.get_magnitude()
        self.x /= magnitude
        self.y /= magnitude

   
   
class main(Vector2):

    background_image = ("UnicornPoop/CRSwall.jpg") # for the images, you'll have to put your own in
    sprite_image_Right = ("UnicornPoop/brushRight.png") #filename / your image
    sprite_image_Left = ("UnicornPoop/brushLeft.png")   #filename / your image

    pygame.init()

    screen = pygame.display.set_mode((600, 800), 0, 32)

    background= pygame.image.load(background_image).convert()

    sprite = pygame.image.load(sprite_image_Right)
    spriteLeft = pygame.image.load(sprite_image_Left)

    clock = pygame.time.Clock()

    position = Vector2(100.0, 100.0)
    speed = 250
    heading = Vector2()
    sprite_pos = pygame.mouse.get_pos()
    while True:

       


        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
        if event.type == MOUSEBUTTONDOWN:
           
           
            destination = Vector2(*event.pos) - Vector2(*sprite.get_size())/2
            heading = Vector2.from_points(position, destination)
            heading.normalize()
           
           
        screen.blit(background, (-5, -5))
        screen.blit(sprite, position)

   
        time_passed = clock.tick()
        time_passed_seconds = time_passed / 1000.

        distance_moved = time_passed_seconds * speed
        position += heading * distance_moved
   
        pygame.display.update()

main()

#A = (10.0, 20.0)
#B = (30.0, 35.0)
#AB = Vector2.from_points(A, B)
#step = AB * .1
#position = Vector2(10, 20)  #the book says to put (A.x, A.y) but I get error, has no attribute x
#for n in range(10):
    #position += step
    #print position
#A = (10.0, 20.0)
#B = (30.0, 35.0)
#C = (15.0, 45.0)
#AB = Vector2.from_points(A, B)
#BC = Vector2.from_points(B, C)

#AC = Vector2.from_points(A, C)
#print "Vector AC is",AC

#AC = AB + BC

#print "AB + BC is", AC


#print "Vector AB is", AB
#print "Magnitude of Vector AB is ", AB.get_magnitude()
#AB.normalize()
#print "Vector AB normalized is", AB
#print AB.get_magnitude()

No comments:

Post a Comment