Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, math
- import numpy as np
- from scipy import interpolate
- from matplotlib import pyplot as plt
- pygame.init()
- ###\/
- display_width = 1000
- display_height = 1000
- showNodes = True #True = nodes drawn as green dots, False = nodes not drawn
- #If the window is too big, make these numbers smaller
- ###/\
- font = pygame.font.SysFont("Courier_New", 90)
- screen = pygame.display.set_mode((display_width,display_height))
- pygame.display.set_caption('Window')
- black = (0,0,0)
- white = (255,255,255)
- zoom = 1
- image_size = (30*zoom, 30*zoom)
- clock = pygame.time.Clock()
- crashed = False
- ###\/
- backgroundmap = pygame.image.load(r'C:\Users\[User]\Pictures\background.png')
- exampleunit = pygame.image.load(r'C:\Users\[User]\Pictures\unit1.png')
- exampleunit2 = pygame.image.load(r'C:\Users\[User]\Pictures\unit2.png')
- background = pygame.transform.scale(backgroundmap, (display_width, display_height))
- exampleunit = pygame.transform.scale(exampleunit, image_size)
- exampleunit2 = pygame.transform.scale(exampleunit2, image_size)
- ### /\
- prev = 0 #don't touch
- time = 0 #starting time
- day = 23.0 #unused
- month = 2 #unused
- transformx = 0 #don't use
- class Line():
- def __init__(self):
- self.nodes = []
- def appendNode(self, xs, ys, ts): #list of x positions to reach, list of y positions to reach, at these times
- self.nodes.append(Node(xs, ys, ts))
- def insertNode(self, x, y, position): #x position, y position, at which part of the line (drawing order)
- self.nodes.insert(position, Node(xs, ys, ts))
- def removeNode(self, position): #removes specified node
- self.nodes.pop(position)
- def loop(self):
- for i in self.nodes:
- i.glide()
- def draw(self):
- xs = []
- ys = []
- for i in range(0, len(self.nodes)):
- xs.append(self.nodes[i].x)
- ys.append(self.nodes[i].y)
- tck, u = interpolate.splprep([xs, ys], s=0, per=False)
- xi, yi = interpolate.splev(np.linspace(0, 1, 1000), tck)
- index = 0
- for i in range(0, 999):
- 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)
- if showNodes:
- for i in range(0, len(self.nodes)):
- pygame.draw.circle(screen, (0, 255, 0), ((self.nodes[i].x+transformx)*math.sqrt(zoom), self.nodes[i].y*math.sqrt(zoom)), 5)
- def getNodes(self):
- return self.nodes
- class Node():
- def __init__(self, xs, ys, ts):
- self.xs = xs
- self.ys = ys
- self.ts = ts
- self.x = xs[0]
- self.y = ys[0]
- def glide(self):
- for i in range(0, len(self.ts)-1):
- if time in range(self.ts[i], self.ts[i+1]):
- duration = self.ts[i+1]-self.ts[i]
- if time-self.ts[i]<self.ts[i+1]-time:
- velocityfactor = time-self.ts[i]
- else:
- velocityfactor = self.ts[i+1]-time
- dimensionlessintegration = ((self.ts[i+1]-self.ts[i])/2)*((self.ts[i+1]-self.ts[i])/2)
- self.x+=velocityfactor*((self.xs[i+1]-self.xs[i])/dimensionlessintegration)
- self.y+=velocityfactor*((self.ys[i+1]-self.ys[i])/dimensionlessintegration)
- class UnitHandler():
- def __init__(self):
- self.units = []
- self.id = 0
- def addUnit(self, width, height, image, xs, ys, ts):
- #adds a unit that goes to all positions specified in xs and ys and times listed in ts
- self.units.append((Unit(width, height, image, xs, ys, ts), self.id))
- self.id+=1
- def loop(self):
- for i in self.units:
- i[0].draw()
- i[0].glide()
- i[0].image = pygame.transform.scale(i[0].image, (image_size[0]*zoom, image_size[1]*zoom))
- class Unit():
- def __init__(self, width, height, image, xs, ys, ts):
- self.xs = xs
- self.ys = ys
- self.ts = ts
- self.x = xs[0]
- self.y = ys[0]
- self.width = width
- self.height = height
- self.image = image
- def draw(self):
- blitat(self.image, self.x*math.sqrt(zoom)+transformx, self.y*math.sqrt(zoom), (self.width*zoom, self.height*zoom))
- def glide(self):
- for i in range(0, len(self.ts)-1):
- if time in range(self.ts[i], self.ts[i+1]):
- duration = self.ts[i+1]-self.ts[i]
- if time-self.ts[i]<self.ts[i+1]-time:
- velocityfactor = time-self.ts[i]
- else:
- velocityfactor = self.ts[i+1]-time
- dimensionlessintegration = ((self.ts[i+1]-self.ts[i])/2)*((self.ts[i+1]-self.ts[i])/2)
- self.x+=velocityfactor*((self.xs[i+1]-self.xs[i])/dimensionlessintegration)
- self.y+=velocityfactor*((self.ys[i+1]-self.ys[i])/dimensionlessintegration)
- def blitat(image, x, y, image_size):
- screen.blit(image, ((x-image_size[0]/2),(y-image_size[1]/2)))
- def drawText(display, x, y, color, textcolor = (0, 0, 0)):
- text = font.render(display, True, textcolor, color)
- textRect = text.get_rect()
- textRect.center = (x, y)
- screen.blit(text, textRect)
- frontline = Line()
- units = UnitHandler()
- ###\/
- units.addUnit(image_size[0], image_size[1], exampleunit, [250, 350], [350, 500], [0, 200])
- units.addUnit(image_size[0], image_size[1], exampleunit2, [500, 250], [350, 350], [0, 200])
- frontline.appendNode([100 , 0 ], [100 , 0 ], [0, 200])
- frontline.appendNode([200 , 100], [200 , 100], [0, 200])
- frontline.appendNode([300 , 200], [300 , 200], [0, 200])
- frontline.appendNode([400 , 238], [400 , 417], [0, 200])
- frontline.appendNode([500 , 400], [500 , 400], [0, 200])
- frontline.appendNode([600 , 500], [600 , 500], [0, 200])
- frontline.appendNode([700 , 600], [700 , 600], [0, 200])
- frontline.appendNode([800 , 700], [800 , 700], [0, 200])
- frontline.appendNode([900 , 800], [900 , 800], [0, 200])
- frontline.appendNode([1000, 900], [1000, 900], [0, 200])
- #example numbers, change as needed
- ###/\
- zooms = [[0, 400], [1, 1]] #(times, zooms to reach), do not use
- while not crashed:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- crashed = True
- if event.type == pygame.MOUSEBUTTONDOWN:
- print((mouse[0]-transformx)/zoom," ", mouse[1]/zoom)
- time+=1
- day+=0.05
- for i in range(0, len(zooms[0])-1):
- if time in range(zooms[0][i], zooms[0][i+1]):
- if time-zooms[0][i]<zooms[0][i+1]-time:
- velocityfactor = time-zooms[0][i]
- else:
- velocityfactor = zooms[0][i+1]-time
- dimensionlessintegration = ((zooms[0][i+1]-zooms[0][i])/2)*((zooms[0][i+1]-zooms[0][i])/2)
- zoom+=velocityfactor*((zooms[1][i+1]-zooms[1][i])/dimensionlessintegration)
- background = pygame.transform.scale(backgroundmap, ((2490+transformx)*math.sqrt(zoom), 1540*math.sqrt(zoom)))
- blitat(background, display_width/2+100,display_height/2-100, (2560, 1440))
- units.loop()
- frontline.draw()
- frontline.loop()
- mouse = pygame.mouse.get_pos()
- pygame.display.flip()
- clock.tick(10000)
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement