Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1-init.py
- print __name__
- from Vector import Vector
- import pygame
- #a = Vector(1,2)
- #print a.eye()
- class Circle(object):
- def __init__ (self, pos, vel, rad, color):
- # pos, vel - 2D-vectors
- # rad - float
- # color - tuple (r, g, b)
- self.pos = pos
- self.vel = vel
- self.rad = rad
- self.color = color
- def show(self, screen):
- pygame.draw.circle(screen, self.color, (int(self.pos.x), int(self.pos.y)), self.rad)
- def updateState(self):
- self.pos += self.vel
- 2-sharik.py
- import pygame
- pygame.init()
- DISPLAY_WIDTH = 700
- DISPLAY_HEIGHT = 600
- screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
- TICK_COUNT = 60 # per second
- from Vector import Vector
- from init import Circle
- from collider import intersect, collide
- def key_handle():
- done__ = False
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done__ = True
- return done__
- def shower(objects):
- done = False
- clock = pygame.time.Clock()
- while not done:
- screen.fill((0, 0, 0))
- done = key_handle()
- for i in xrange(len(objects)):
- for j in xrange(i+1, len(objects)):
- if intersect(objects[i], objects[j]):
- print "INTERSECTION"
- collide(objects[i], objects[j])
- for obj in objects:
- obj.updateState()
- obj.show(screen)
- pygame.display.flip()
- clock.tick(TICK_COUNT)
- #pygame.draw.rect(screen, (0, 128, 255), pygame.Rect(30, 30, 60, 60))
- def main():
- #quad = Quad((255, 255, 255), 80, 80, 30, 30, 0, 0)
- c1 = Circle(Vector(50,50), Vector(2,1), 20, (255, 0, 0))
- c2 = Circle(Vector(220,50), Vector(-4,2), 20, (125, 22, 0))
- shower([c1,c2])
- #shower(get_action([quad]))
- if __name__ == "__main__":
- main()
- 3-vector.py
- from math import sqrt
- print __name__
- class Vector(object):
- def __init__(self,x,y):
- self.x=x
- self.y=y
- def length(self):
- return sqrt(self.x * self.x + self.y * self.y)
- def __add__ (self,other):
- return Vector(self.x+other.x,self.y+other.y)
- def __sub__ (self,other):
- return Vector(self.x-other.x,self.y-other.y)
- def __repr__ (self):
- return str(self.x) + " " + str(self.y)
- def dot(self,other):
- return self.x*other.x+self.y*other.y
- def __mul__ (self, c):
- return Vector(c*(self.x),c*(self.y))
- def __rmul__ (self, c):
- return Vector(c*(self.x),c*(self.y))
- def __div__ (self, c):
- return self*(1.0 / c)
- def eye(self):
- return self/self.length()
- def main():
- a=Vector(1,2)
- b=Vector(3,4)
- print a
- print b
- c=a.dot(b)
- print c
- c=b.__add__(a)
- print c
- print c.eye()
- i = Vector(1, 1)
- print i.eye()
- if __name__ == "__main__":
- main()
- 4- Collide.py
- from Vector import Vector
- def intersect(ball_1, ball_2):
- v = ball_2.pos-ball_1.pos
- if v.length() <= ball_1.rad + ball_2.rad:
- return True
- else:
- return False
- def collide(ball_1, ball_2, options = None):
- d = ball_2.pos - ball_1.pos
- dlen = d.length()
- delta = ball_1.rad + ball_2.rad - dlen
- dn = d.eye()
- ball_2.pos = ball_2.pos + dn*delta*0.5
- ball_1.pos = ball_1.pos - dn*delta*0.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement