Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Imports
- import pyglet
- from pyglet.window import key
- from pyglet.gl import *
- import xml.etree.ElementTree as ET
- import math
- import time
- from pyglet.gl.glu import gluLookAt
- from pyglet.window import mouse
- #Constants
- pack = "track pack 1"
- track = "bronzestone"
- movespeed = 10
- carpack = "f1pack"
- car = "F1red"
- carcfg = "F1"
- #Variables
- points = []
- objects = []
- global offsetx
- offsetx = 0
- global offsety
- offsety = 0
- global scalex
- scalex = 1
- global scaley
- scaley = 1
- global cartest
- cartest = False
- global start
- start = []
- global specs
- specs = []
- gears = []
- global game_objects
- game_objects = []
- global camx
- camx = 0
- global camy
- camy = 0
- global event_stack_size
- event_stack_size = 0
- global lastox
- global lastoy
- lastox = 0
- lastoy = 0
- global packlabel,tracklabel
- global tilesel, invtiles
- invtiles = []
- global tilesel
- tilesel = "track pack 1/start1.png"
- global camposx,camposy
- camposx,camposy = 0,0
- #Init
- window = pyglet.window.Window(800,600)
- batch = pyglet.graphics.Batch()
- textbatch = pyglet.graphics.Batch()
- key_handler = key.KeyStateHandler()
- window.push_handlers(key_handler)
- pyglet.resource.path = ["resources"]
- pyglet.resource.reindex()
- counter = pyglet.window.FPSDisplay(window=window)
- #Classes
- class Tile(pyglet.sprite.Sprite):
- def __init__(self,*args,**kwargs):
- super(Tile,self).__init__(*args,**kwargs)
- self.event_handlers = []
- def myround(x, base=64):
- return (x+63)//base*base
- @window.event
- def on_mouse_motion(x,y,dx,dy):
- for obj in invtiles:
- obj.delete
- invtiles.remove(obj)
- global camposx,camposy
- dir = "tracks/"+tilesel
- fakeimg = pyglet.resource.image(dir)
- fakeobj = pyglet.sprite.Sprite(img=fakeimg, x=Tile.myround(x-camposx)-64, y=Tile.myround(y-camposy)-64, batch=batch)
- fakeobj.z = 3
- fakeobj.opacity = 64
- invtiles.append(fakeobj)
- class Object(pyglet.sprite.Sprite):
- def __init__(self,*args,**kwargs):
- super(Object,self).__init__(*args,**kwargs)
- self.velocity_x, self.velocity_y = 0.0, 0.0
- self.event_handlers = []
- def update(self,dt):
- self.x -= self.velocity_x * dt
- self.y -= self.velocity_y * dt
- class Player(Object):
- global imgpath
- imgpath = "cars/"+carpack+"/"+car+".png"
- def __init__(self,*args,**kwargs):
- global imgpath
- super(Player,self).__init__(img=pyglet.resource.image(imgpath),*args,**kwargs)
- global startrotation
- self.z = 2
- cfgpath = "Resources/cars/"+carpack+"/"+carcfg+".xml"
- config = ET.parse(cfgpath)
- global specs
- for elem in config.iter(tag='spec'):
- specs.append(elem.text)
- for elem in config.iter(tag='gear'):
- gears.append(elem.text)
- self.key_handler = key.KeyStateHandler()
- self.event_handlers = [self,self.key_handler]
- self.rotate_speed = int(specs[2]) / 5
- self.force_x = 0
- self.force_y = 0
- self.gear = 1
- self.angularVelocity = 0
- self.drag = 0.9
- self.angularDrag = 0.9
- def update(self,dt):
- super(Player,self).update(dt)
- self.velocity_x *= self.drag
- self.velocity_y *= self.drag
- self.rotation += self.angularVelocity
- self.angularVelocity *= self.angularDrag
- if self.key_handler[key.UP]:
- self.velocity_y -= math.sin(self.rotation) * int(specs[2])
- self.velocity_x -= math.cos(self.rotation) * int(specs[2])
- elif self.key_handler[key.DOWN]:
- self.velocity_y += math.sin(self.rotation) * int(specs[5])
- self.velocity_x += math.cos(self.rotation) * int(specs[5])
- if self.key_handler[key.LEFT]:
- self.angularVelocity -= int(specs[3])
- elif self.key_handler[key.RIGHT]:
- self.angularVelocity += int(specs[3])
- def on_key_press(self,symbol,modifiers):
- if symbol == key.LSHIFT:
- self.gear += 1
- elif symbol == key.LCTRL:
- self.gear -= 1
- #Functions
- def myround(x, base=64):
- return (x+63)//base*base
- @window.event
- def on_mouse_release(x,y,button,modifiers):
- global tilesel, game_objects, camposx,camposy
- if button == pyglet.window.mouse.LEFT:
- dir = "tracks/"+tilesel
- img = pyglet.resource.image(dir)
- obj = pyglet.sprite.Sprite(img=img, x=(x-camposx), y=myround(y-camposy)-64, batch=batch)
- print(obj.x)
- obj.z = 3
- game_objects.append(obj)
- if button == pyglet.window.mouse.RIGHT:
- for obj in game_objects:
- x1 = obj.x + camposx
- y1 = obj.y + camposy
- x2 = obj.image.width + obj.x + camposx
- y2 = obj.image.height + obj.y + camposy
- if x > x1 and x < x2 and y > y1 and y < y2:
- obj.delete()
- game_objects.remove(obj)
- def refreshtrack():
- window.clear()
- for obj in objects:
- obj.delete
- init()
- def init():
- global objects, game_objects
- grassimg = pyglet.resource.image("grass.png")
- tileimg = pyglet.resource.image("tile.png")
- pointx = -1024
- pointy = 8128
- batch.draw()
- for i in range(100):
- for j in range(100):
- grass = pyglet.sprite.Sprite(img=grassimg,x=pointx,y=pointy,batch=batch)
- grass.z = -1
- objects.append(grass)
- pointx += 128
- pointy -= 128
- pointx = -1024
- pointx = -1024
- pointy = 8128
- for i in range(200):
- for j in range(200):
- global tile
- tile = Tile(img=tileimg,x=pointx,y=pointy,batch=batch)
- tile.z = 0
- objects.append(tile)
- pointx += 64
- pointy -= 64
- pointx = -1024
- batch.draw()
- global event_stack_size
- while event_stack_size > 0:
- window.pop_handlers()
- event_stack_size -= 1
- path = "Resources/tracks/"+pack+"/"+track+".xml"
- config = ET.parse(path)
- for elem in config.iter(tag='point'):
- points.append(elem.text)
- for i in range(len(points)):
- point = points[i].split(":")
- dir = "tracks/"+pack+"/"+point[0]
- pointimg = pyglet.resource.image(dir)
- pointx = int(point[1]) * 64
- pointy = int(point[2]) * 64
- object = pyglet.sprite.Sprite(img=pointimg,x=pointx,y=pointy,batch=batch)
- object.update(rotation=int(point[4]))
- object.z = 1
- game_objects.append(object)
- for elem in config.iter(tag='start'):
- start.append(elem.text)
- global startx
- global starty
- global startrotation
- startx = start[0]
- starty = start[1]
- startrotation = start[2]
- headerimg = pyglet.resource.image("header.png")
- global header
- header = pyglet.sprite.Sprite(img=headerimg,x=0,y=536,batch=batch)
- header.z = 4
- global packlabel,tracklabel
- packtext = "pack : "+pack
- tracktext = "track : "+track
- packlabel = pyglet.text.Label(text=packtext,x=10,y=575,batch=textbatch)
- tracklabel = pyglet.text.Label(text=tracktext,x=10,y=550,batch=textbatch)
- packlabel.z = 5
- tracklabel.z = 5
- @window.event
- def on_draw():
- global specs
- window.clear()
- global offsetx
- global offsety
- global scalex
- global scaley
- global camposx
- global camposy
- camposx += offsetx
- camposy += offsety
- packlabel.x -= offsetx
- packlabel.y -= offsety
- tracklabel.x -= offsetx
- tracklabel.y -= offsety
- header.x -= offsetx
- header.y -= offsety
- glScalef(scalex,scaley,0)
- if cartest == True:
- global player
- global lastox
- global lastoy
- lastox = offsetx
- lastoy = offsety
- offsetx = lastox + player.velocity_x / 110
- offsety = lastoy + player.velocity_y / 110
- glTranslatef(round(offsetx),round(offsety),3)
- offsetx = 0
- offsety = 0
- scalex = 1
- scaley = 1
- batch.draw()
- textbatch.draw()
- counter.draw()
- if cartest == True:
- player.draw()
- def update(dt):
- if key_handler[key.R]:
- refreshtrack()
- global cartest
- if cartest == False:
- global offsetx
- global offsety
- global scalex
- global scaley
- if key_handler[key.UP]:
- offsety -= movespeed
- if key_handler[key.DOWN]:
- offsety += movespeed
- if key_handler[key.LEFT]:
- offsetx += movespeed
- if key_handler[key.RIGHT]:
- offsetx -= movespeed
- if key_handler[key.T]:
- cartest = True
- print(cartest)
- global startx
- global starty
- global startrotation
- global player
- player = Player(x=int(startx),y=int(starty))
- game_objects.append(player)
- for obj in game_objects:
- for handler in obj.event_handlers:
- window.push_handlers(handler)
- global event_stack_size
- event_stack_size += 1
- for obj in game_objects:
- obj.update(dt)
- #Start Game
- if __name__ == "__main__":
- init()
- pyglet.clock.schedule_interval(update,1/120.0)
- pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement