Advertisement
Guest User

Untitled

a guest
Oct 15th, 2022
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. import pygame, math
  2. import numpy as np
  3. from scipy import interpolate
  4. from matplotlib import pyplot as plt
  5.  
  6.  
  7. pygame.init()
  8.  
  9. ###\/
  10. display_width = 1000
  11. display_height = 1000
  12. showNodes = True #True = nodes drawn as green dots, False = nodes not drawn
  13. #If the window is too big, make these numbers smaller
  14. ###/\
  15. font = pygame.font.SysFont("Courier_New", 90)
  16.  
  17. screen = pygame.display.set_mode((display_width,display_height))
  18. pygame.display.set_caption('Window')
  19.  
  20. black = (0,0,0)
  21. white = (255,255,255)
  22. zoom = 1
  23. image_size = (30*zoom, 30*zoom)
  24.  
  25. clock = pygame.time.Clock()
  26. crashed = False
  27. ###\/
  28. backgroundmap = pygame.image.load(r'C:\Users\[User]\Pictures\background.png')
  29. exampleunit = pygame.image.load(r'C:\Users\[User]\Pictures\unit1.png')
  30. exampleunit2 = pygame.image.load(r'C:\Users\[User]\Pictures\unit2.png')
  31.  
  32. background = pygame.transform.scale(backgroundmap, (display_width, display_height))
  33. exampleunit = pygame.transform.scale(exampleunit, image_size)
  34. exampleunit2 = pygame.transform.scale(exampleunit2, image_size)
  35. ### /\
  36.  
  37. prev = 0 #don't touch
  38.  
  39. time = 0 #starting time
  40. day = 23.0 #unused
  41. month = 2 #unused
  42. transformx = 0 #don't use
  43.  
  44. class Line():
  45. def __init__(self):
  46. self.nodes = []
  47. def appendNode(self, xs, ys, ts): #list of x positions to reach, list of y positions to reach, at these times
  48. self.nodes.append(Node(xs, ys, ts))
  49. def insertNode(self, x, y, position): #x position, y position, at which part of the line (drawing order)
  50. self.nodes.insert(position, Node(xs, ys, ts))
  51. def removeNode(self, position): #removes specified node
  52. self.nodes.pop(position)
  53. def loop(self):
  54. for i in self.nodes:
  55. i.glide()
  56. def draw(self):
  57. xs = []
  58. ys = []
  59. for i in range(0, len(self.nodes)):
  60. xs.append(self.nodes[i].x)
  61. ys.append(self.nodes[i].y)
  62. tck, u = interpolate.splprep([xs, ys], s=0, per=False)
  63. xi, yi = interpolate.splev(np.linspace(0, 1, 1000), tck)
  64. index = 0
  65. for i in range(0, 999):
  66. pygame.draw.line(screen, (255, 0, 0), [(xi[i]+transformx)*math.sqrt(zoom), yi[i]*math.sqrt(zoom)], [xi[i+1]*math.sqrt(zoom)+transformx, yi[i+1]*math.sqrt(zoom)], 7)
  67. if showNodes:
  68. for i in range(0, len(self.nodes)):
  69. pygame.draw.circle(screen, (0, 255, 0), ((self.nodes[i].x+transformx)*math.sqrt(zoom), self.nodes[i].y*math.sqrt(zoom)), 5)
  70. def getNodes(self):
  71. return self.nodes
  72.  
  73. class Node():
  74. def __init__(self, xs, ys, ts):
  75. self.xs = xs
  76. self.ys = ys
  77. self.ts = ts
  78. self.x = xs[0]
  79. self.y = ys[0]
  80. def glide(self):
  81. for i in range(0, len(self.ts)-1):
  82. if time in range(self.ts[i], self.ts[i+1]):
  83. duration = self.ts[i+1]-self.ts[i]
  84. if time-self.ts[i]<self.ts[i+1]-time:
  85. velocityfactor = time-self.ts[i]
  86. else:
  87. velocityfactor = self.ts[i+1]-time
  88. dimensionlessintegration = ((self.ts[i+1]-self.ts[i])/2)*((self.ts[i+1]-self.ts[i])/2)
  89. self.x+=velocityfactor*((self.xs[i+1]-self.xs[i])/dimensionlessintegration)
  90. self.y+=velocityfactor*((self.ys[i+1]-self.ys[i])/dimensionlessintegration)
  91.  
  92. class UnitHandler():
  93. def __init__(self):
  94. self.units = []
  95. self.id = 0
  96. def addUnit(self, width, height, image, xs, ys, ts):
  97. #adds a unit that goes to all positions specified in xs and ys and times listed in ts
  98. self.units.append((Unit(width, height, image, xs, ys, ts), self.id))
  99. self.id+=1
  100. def loop(self):
  101. for i in self.units:
  102. i[0].draw()
  103. i[0].glide()
  104. i[0].image = pygame.transform.scale(i[0].image, (image_size[0]*zoom, image_size[1]*zoom))
  105.  
  106. class Unit():
  107. def __init__(self, width, height, image, xs, ys, ts):
  108. self.xs = xs
  109. self.ys = ys
  110. self.ts = ts
  111. self.x = xs[0]
  112. self.y = ys[0]
  113. self.width = width
  114. self.height = height
  115. self.image = image
  116. def draw(self):
  117. blitat(self.image, self.x*math.sqrt(zoom)+transformx, self.y*math.sqrt(zoom), (self.width*zoom, self.height*zoom))
  118. def glide(self):
  119. for i in range(0, len(self.ts)-1):
  120. if time in range(self.ts[i], self.ts[i+1]):
  121. duration = self.ts[i+1]-self.ts[i]
  122. if time-self.ts[i]<self.ts[i+1]-time:
  123. velocityfactor = time-self.ts[i]
  124. else:
  125. velocityfactor = self.ts[i+1]-time
  126. dimensionlessintegration = ((self.ts[i+1]-self.ts[i])/2)*((self.ts[i+1]-self.ts[i])/2)
  127. self.x+=velocityfactor*((self.xs[i+1]-self.xs[i])/dimensionlessintegration)
  128. self.y+=velocityfactor*((self.ys[i+1]-self.ys[i])/dimensionlessintegration)
  129.  
  130. def blitat(image, x, y, image_size):
  131. screen.blit(image, ((x-image_size[0]/2),(y-image_size[1]/2)))
  132.  
  133. def drawText(display, x, y, color, textcolor = (0, 0, 0)):
  134. text = font.render(display, True, textcolor, color)
  135. textRect = text.get_rect()
  136. textRect.center = (x, y)
  137. screen.blit(text, textRect)
  138.  
  139. frontline = Line()
  140. units = UnitHandler()
  141. ###\/
  142. units.addUnit(image_size[0], image_size[1], exampleunit, [250, 350], [350, 500], [0, 200])
  143. units.addUnit(image_size[0], image_size[1], exampleunit2, [500, 250], [350, 350], [0, 200])
  144.  
  145. frontline.appendNode([100 , 0 ], [100 , 0 ], [0, 200])
  146. frontline.appendNode([200 , 100], [200 , 100], [0, 200])
  147. frontline.appendNode([300 , 200], [300 , 200], [0, 200])
  148. frontline.appendNode([400 , 238], [400 , 417], [0, 200])
  149. frontline.appendNode([500 , 400], [500 , 400], [0, 200])
  150. frontline.appendNode([600 , 500], [600 , 500], [0, 200])
  151. frontline.appendNode([700 , 600], [700 , 600], [0, 200])
  152. frontline.appendNode([800 , 700], [800 , 700], [0, 200])
  153. frontline.appendNode([900 , 800], [900 , 800], [0, 200])
  154. frontline.appendNode([1000, 900], [1000, 900], [0, 200])
  155. #example numbers, change as needed
  156. ###/\
  157.  
  158. zooms = [[0, 400], [1, 1]] #(times, zooms to reach), do not use
  159.  
  160. while not crashed:
  161. for event in pygame.event.get():
  162. if event.type == pygame.QUIT:
  163. crashed = True
  164. if event.type == pygame.MOUSEBUTTONDOWN:
  165. print((mouse[0]-transformx)/zoom," ", mouse[1]/zoom)
  166. time+=1
  167. day+=0.05
  168. for i in range(0, len(zooms[0])-1):
  169. if time in range(zooms[0][i], zooms[0][i+1]):
  170. if time-zooms[0][i]<zooms[0][i+1]-time:
  171. velocityfactor = time-zooms[0][i]
  172. else:
  173. velocityfactor = zooms[0][i+1]-time
  174. dimensionlessintegration = ((zooms[0][i+1]-zooms[0][i])/2)*((zooms[0][i+1]-zooms[0][i])/2)
  175. zoom+=velocityfactor*((zooms[1][i+1]-zooms[1][i])/dimensionlessintegration)
  176.  
  177. background = pygame.transform.scale(backgroundmap, ((2490+transformx)*math.sqrt(zoom), 1540*math.sqrt(zoom)))
  178. blitat(background, display_width/2+100,display_height/2-100, (2560, 1440))
  179. units.loop()
  180. frontline.draw()
  181. frontline.loop()
  182. mouse = pygame.mouse.get_pos()
  183. pygame.display.flip()
  184. clock.tick(10000)
  185.  
  186. pygame.quit()
  187. quit()
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement