Advertisement
joshgreatuk

Help

May 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.22 KB | None | 0 0
  1. #Imports
  2.  
  3. import pyglet
  4. from pyglet.window import key
  5. from pyglet.gl import *
  6. import xml.etree.ElementTree as ET
  7. import math
  8. import time
  9. from pyglet.gl.glu import gluLookAt
  10. from pyglet.window import mouse
  11.  
  12. #Constants
  13.  
  14. pack = "track pack 1"
  15. track = "bronzestone"
  16. movespeed = 10
  17. carpack = "f1pack"
  18. car = "F1red"
  19. carcfg = "F1"
  20.  
  21. #Variables
  22.  
  23. points = []
  24. objects = []
  25. global offsetx
  26. offsetx = 0
  27. global offsety
  28. offsety = 0
  29. global scalex
  30. scalex = 1
  31. global scaley
  32. scaley = 1
  33. global cartest
  34. cartest = False
  35. global start
  36. start = []
  37. global specs
  38. specs = []
  39. gears = []
  40. global game_objects
  41. game_objects = []
  42. global camx
  43. camx = 0
  44. global camy
  45. camy = 0
  46. global event_stack_size
  47. event_stack_size = 0
  48. global lastox
  49. global lastoy
  50. lastox = 0
  51. lastoy = 0
  52. global packlabel,tracklabel
  53. global tilesel, invtiles
  54. invtiles = []
  55. global tilesel
  56. tilesel = "track pack 1/start1.png"
  57. global camposx,camposy
  58. camposx,camposy = 0,0
  59.  
  60. #Init
  61.  
  62. window = pyglet.window.Window(800,600)
  63. batch = pyglet.graphics.Batch()
  64. textbatch = pyglet.graphics.Batch()
  65. key_handler = key.KeyStateHandler()
  66. window.push_handlers(key_handler)
  67. pyglet.resource.path = ["resources"]
  68. pyglet.resource.reindex()
  69. counter = pyglet.window.FPSDisplay(window=window)
  70.  
  71. #Classes
  72.  
  73. class Tile(pyglet.sprite.Sprite):
  74.     def __init__(self,*args,**kwargs):
  75.         super(Tile,self).__init__(*args,**kwargs)
  76.         self.event_handlers = []
  77.  
  78.     def myround(x, base=64):
  79.         return (x+63)//base*base
  80.  
  81.     @window.event
  82.     def on_mouse_motion(x,y,dx,dy):
  83.         for obj in invtiles:
  84.             obj.delete
  85.             invtiles.remove(obj)
  86.         global camposx,camposy
  87.         dir = "tracks/"+tilesel
  88.         fakeimg = pyglet.resource.image(dir)
  89.         fakeobj = pyglet.sprite.Sprite(img=fakeimg, x=Tile.myround(x-camposx)-64, y=Tile.myround(y-camposy)-64, batch=batch)
  90.         fakeobj.z = 3
  91.         fakeobj.opacity = 64
  92.         invtiles.append(fakeobj)
  93.  
  94. class Object(pyglet.sprite.Sprite):
  95.     def __init__(self,*args,**kwargs):
  96.         super(Object,self).__init__(*args,**kwargs)
  97.         self.velocity_x, self.velocity_y = 0.0, 0.0
  98.         self.event_handlers = []
  99.  
  100.     def update(self,dt):
  101.         self.x -= self.velocity_x * dt
  102.         self.y -= self.velocity_y * dt
  103.  
  104. class Player(Object):
  105.  
  106.     global imgpath
  107.     imgpath = "cars/"+carpack+"/"+car+".png"
  108.  
  109.     def __init__(self,*args,**kwargs):
  110.         global imgpath
  111.         super(Player,self).__init__(img=pyglet.resource.image(imgpath),*args,**kwargs)
  112.         global startrotation
  113.         self.z = 2
  114.         cfgpath = "Resources/cars/"+carpack+"/"+carcfg+".xml"
  115.         config = ET.parse(cfgpath)
  116.         global specs
  117.         for elem in config.iter(tag='spec'):
  118.             specs.append(elem.text)
  119.         for elem in config.iter(tag='gear'):
  120.             gears.append(elem.text)
  121.         self.key_handler = key.KeyStateHandler()
  122.         self.event_handlers = [self,self.key_handler]
  123.         self.rotate_speed = int(specs[2]) / 5
  124.         self.force_x = 0
  125.         self.force_y = 0
  126.         self.gear = 1
  127.         self.angularVelocity = 0
  128.         self.drag = 0.9
  129.         self.angularDrag = 0.9
  130.  
  131.     def update(self,dt):
  132.         super(Player,self).update(dt)
  133.         self.velocity_x *= self.drag
  134.         self.velocity_y *= self.drag
  135.         self.rotation += self.angularVelocity
  136.         self.angularVelocity *= self.angularDrag
  137.         if self.key_handler[key.UP]:
  138.             self.velocity_y -= math.sin(self.rotation) * int(specs[2])
  139.             self.velocity_x -= math.cos(self.rotation) * int(specs[2])
  140.         elif self.key_handler[key.DOWN]:
  141.             self.velocity_y += math.sin(self.rotation) * int(specs[5])
  142.             self.velocity_x += math.cos(self.rotation) * int(specs[5])
  143.         if self.key_handler[key.LEFT]:
  144.             self.angularVelocity -= int(specs[3])
  145.         elif self.key_handler[key.RIGHT]:
  146.             self.angularVelocity += int(specs[3])
  147.  
  148.     def on_key_press(self,symbol,modifiers):
  149.         if symbol == key.LSHIFT:
  150.             self.gear += 1
  151.         elif symbol == key.LCTRL:
  152.             self.gear -= 1
  153.  
  154. #Functions
  155.  
  156. def myround(x, base=64):
  157.     return (x+63)//base*base
  158.  
  159. @window.event
  160. def on_mouse_release(x,y,button,modifiers):
  161.     global tilesel, game_objects, camposx,camposy
  162.     if button == pyglet.window.mouse.LEFT:
  163.         dir = "tracks/"+tilesel
  164.         img = pyglet.resource.image(dir)
  165.         obj = pyglet.sprite.Sprite(img=img, x=(x-camposx), y=myround(y-camposy)-64, batch=batch)
  166.         print(obj.x)
  167.         obj.z = 3
  168.         game_objects.append(obj)
  169.     if button == pyglet.window.mouse.RIGHT:
  170.         for obj in game_objects:
  171.             x1 = obj.x + camposx
  172.             y1 = obj.y + camposy
  173.             x2 = obj.image.width + obj.x + camposx
  174.             y2 = obj.image.height +  obj.y + camposy
  175.             if x > x1 and x < x2 and y > y1 and y < y2:
  176.                 obj.delete()
  177.                 game_objects.remove(obj)
  178.  
  179. def refreshtrack():
  180.     window.clear()
  181.     for obj in objects:
  182.         obj.delete
  183.     init()
  184.  
  185. def init():
  186.     global objects, game_objects
  187.     grassimg = pyglet.resource.image("grass.png")
  188.     tileimg = pyglet.resource.image("tile.png")
  189.     pointx = -1024
  190.     pointy = 8128
  191.     batch.draw()
  192.     for i in range(100):
  193.         for j in range(100):
  194.             grass = pyglet.sprite.Sprite(img=grassimg,x=pointx,y=pointy,batch=batch)
  195.             grass.z = -1
  196.             objects.append(grass)
  197.             pointx += 128
  198.         pointy -= 128
  199.         pointx = -1024
  200.     pointx = -1024
  201.     pointy = 8128
  202.     for i in range(200):
  203.         for j in range(200):
  204.             global tile
  205.             tile = Tile(img=tileimg,x=pointx,y=pointy,batch=batch)
  206.             tile.z = 0
  207.             objects.append(tile)
  208.             pointx += 64
  209.         pointy -= 64
  210.         pointx = -1024
  211.     batch.draw()
  212.     global event_stack_size
  213.     while event_stack_size > 0:
  214.         window.pop_handlers()
  215.         event_stack_size -= 1
  216.     path = "Resources/tracks/"+pack+"/"+track+".xml"
  217.     config = ET.parse(path)
  218.     for elem in config.iter(tag='point'):
  219.         points.append(elem.text)
  220.     for i in range(len(points)):
  221.         point = points[i].split(":")
  222.         dir = "tracks/"+pack+"/"+point[0]
  223.         pointimg = pyglet.resource.image(dir)
  224.         pointx = int(point[1]) * 64
  225.         pointy = int(point[2]) * 64
  226.         object = pyglet.sprite.Sprite(img=pointimg,x=pointx,y=pointy,batch=batch)
  227.         object.update(rotation=int(point[4]))
  228.         object.z = 1
  229.         game_objects.append(object)
  230.     for elem in config.iter(tag='start'):
  231.         start.append(elem.text)
  232.     global startx
  233.     global starty
  234.     global startrotation
  235.     startx = start[0]
  236.     starty = start[1]
  237.     startrotation = start[2]
  238.     headerimg = pyglet.resource.image("header.png")
  239.     global header
  240.     header  = pyglet.sprite.Sprite(img=headerimg,x=0,y=536,batch=batch)
  241.     header.z = 4
  242.     global packlabel,tracklabel
  243.     packtext = "pack : "+pack
  244.     tracktext = "track : "+track
  245.     packlabel = pyglet.text.Label(text=packtext,x=10,y=575,batch=textbatch)
  246.     tracklabel = pyglet.text.Label(text=tracktext,x=10,y=550,batch=textbatch)
  247.     packlabel.z = 5
  248.     tracklabel.z = 5
  249.  
  250. @window.event
  251. def on_draw():
  252.     global specs
  253.     window.clear()
  254.     global offsetx
  255.     global offsety
  256.     global scalex
  257.     global scaley
  258.     global camposx
  259.     global camposy
  260.     camposx += offsetx
  261.     camposy += offsety
  262.     packlabel.x -= offsetx
  263.     packlabel.y -= offsety
  264.     tracklabel.x -= offsetx
  265.     tracklabel.y -= offsety
  266.     header.x -= offsetx
  267.     header.y -= offsety
  268.     glScalef(scalex,scaley,0)
  269.     if cartest == True:
  270.         global player
  271.         global lastox
  272.         global lastoy
  273.         lastox = offsetx
  274.         lastoy = offsety
  275.         offsetx = lastox + player.velocity_x / 110
  276.         offsety = lastoy + player.velocity_y / 110
  277.     glTranslatef(round(offsetx),round(offsety),3)
  278.     offsetx = 0
  279.     offsety = 0
  280.     scalex = 1
  281.     scaley = 1
  282.     batch.draw()
  283.     textbatch.draw()
  284.     counter.draw()
  285.     if cartest == True:
  286.         player.draw()
  287.  
  288. def update(dt):
  289.     if key_handler[key.R]:
  290.         refreshtrack()
  291.     global cartest
  292.     if cartest == False:
  293.         global offsetx
  294.         global offsety
  295.         global scalex
  296.         global scaley
  297.         if key_handler[key.UP]:
  298.             offsety -= movespeed
  299.         if key_handler[key.DOWN]:
  300.             offsety += movespeed
  301.         if key_handler[key.LEFT]:
  302.             offsetx += movespeed
  303.         if key_handler[key.RIGHT]:
  304.             offsetx -= movespeed
  305.         if key_handler[key.T]:
  306.             cartest = True
  307.             print(cartest)
  308.             global startx
  309.             global starty
  310.             global startrotation
  311.             global player
  312.             player = Player(x=int(startx),y=int(starty))
  313.             game_objects.append(player)
  314.             for obj in game_objects:
  315.                 for handler in obj.event_handlers:
  316.                     window.push_handlers(handler)
  317.                     global event_stack_size
  318.                     event_stack_size += 1
  319.     for obj in game_objects:
  320.         obj.update(dt)
  321.  
  322. #Start Game
  323.  
  324. if __name__ == "__main__":
  325.     init()
  326.     pyglet.clock.schedule_interval(update,1/120.0)
  327.     pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement