Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. 1-init.py
  2. print __name__
  3. from Vector import Vector
  4. import pygame
  5.  
  6. #a = Vector(1,2)
  7. #print a.eye()
  8. class Circle(object):
  9. def __init__ (self, pos, vel, rad, color):
  10. # pos, vel - 2D-vectors
  11. # rad - float
  12. # color - tuple (r, g, b)
  13. self.pos = pos
  14. self.vel = vel
  15. self.rad = rad
  16. self.color = color
  17.  
  18. def show(self, screen):
  19. pygame.draw.circle(screen, self.color, (int(self.pos.x), int(self.pos.y)), self.rad)
  20.  
  21. def updateState(self):
  22. self.pos += self.vel
  23. 2-sharik.py
  24.  
  25. import pygame
  26.  
  27. pygame.init()
  28.  
  29. DISPLAY_WIDTH = 700
  30. DISPLAY_HEIGHT = 600
  31.  
  32. screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
  33. TICK_COUNT = 60 # per second
  34.  
  35. from Vector import Vector
  36. from init import Circle
  37. from collider import intersect, collide
  38.  
  39. def key_handle():
  40. done__ = False
  41. for event in pygame.event.get():
  42. if event.type == pygame.QUIT:
  43. done__ = True
  44. return done__
  45.  
  46. def shower(objects):
  47. done = False
  48. clock = pygame.time.Clock()
  49. while not done:
  50. screen.fill((0, 0, 0))
  51. done = key_handle()
  52. for i in xrange(len(objects)):
  53. for j in xrange(i+1, len(objects)):
  54. if intersect(objects[i], objects[j]):
  55. print "INTERSECTION"
  56. collide(objects[i], objects[j])
  57. for obj in objects:
  58. obj.updateState()
  59. obj.show(screen)
  60. pygame.display.flip()
  61. clock.tick(TICK_COUNT)
  62. #pygame.draw.rect(screen, (0, 128, 255), pygame.Rect(30, 30, 60, 60))
  63.  
  64. def main():
  65. #quad = Quad((255, 255, 255), 80, 80, 30, 30, 0, 0)
  66. c1 = Circle(Vector(50,50), Vector(2,1), 20, (255, 0, 0))
  67. c2 = Circle(Vector(220,50), Vector(-4,2), 20, (125, 22, 0))
  68. shower([c1,c2])
  69.  
  70. #shower(get_action([quad]))
  71.  
  72. if __name__ == "__main__":
  73. main()
  74. 3-vector.py
  75. from math import sqrt
  76. print __name__
  77. class Vector(object):
  78. def __init__(self,x,y):
  79. self.x=x
  80. self.y=y
  81. def length(self):
  82. return sqrt(self.x * self.x + self.y * self.y)
  83. def __add__ (self,other):
  84. return Vector(self.x+other.x,self.y+other.y)
  85. def __sub__ (self,other):
  86. return Vector(self.x-other.x,self.y-other.y)
  87. def __repr__ (self):
  88. return str(self.x) + " " + str(self.y)
  89. def dot(self,other):
  90. return self.x*other.x+self.y*other.y
  91. def __mul__ (self, c):
  92. return Vector(c*(self.x),c*(self.y))
  93. def __rmul__ (self, c):
  94. return Vector(c*(self.x),c*(self.y))
  95. def __div__ (self, c):
  96. return self*(1.0 / c)
  97. def eye(self):
  98. return self/self.length()
  99.  
  100.  
  101. def main():
  102. a=Vector(1,2)
  103. b=Vector(3,4)
  104. print a
  105. print b
  106. c=a.dot(b)
  107. print c
  108. c=b.__add__(a)
  109. print c
  110. print c.eye()
  111. i = Vector(1, 1)
  112. print i.eye()
  113.  
  114. if __name__ == "__main__":
  115. main()
  116. 4- Collide.py
  117. from Vector import Vector
  118.  
  119. def intersect(ball_1, ball_2):
  120. v = ball_2.pos-ball_1.pos
  121. if v.length() <= ball_1.rad + ball_2.rad:
  122. return True
  123. else:
  124. return False
  125.  
  126. def collide(ball_1, ball_2, options = None):
  127. d = ball_2.pos - ball_1.pos
  128. dlen = d.length()
  129. delta = ball_1.rad + ball_2.rad - dlen
  130. dn = d.eye()
  131. ball_2.pos = ball_2.pos + dn*delta*0.5
  132. ball_1.pos = ball_1.pos - dn*delta*0.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement