Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import pygame as pg, sys
- from pygame.locals import *
- import copy
- class roundmasc:
- def __init__(self,r,intens,color):
- self.r = r
- self.intens = intens
- self.color = color
- self.SURF = pg.Surface((2*r+1,2*r+1),SRCALPHA)
- mascarr = pg.PixelArray(self.SURF)
- self.h2 = r**2*(1/(self.intens**(1/3)-1))
- imult = self.intens*self.h2**1.5
- for x in range(len(mascarr)):
- for y in range(len(mascarr[0])):
- nowr2 = (r-x)**2 + (r-y)**2
- calculat = int(imult/(nowr2+self.h2)**1.5)
- nowint = max(0,min(255,255-calculat))
- mascarr[x,y]=(color[0],color[1],color[2],nowint)
- self.SURF.unlock()
- def sortobjects(objectslist,func):
- start1=0
- stop1=len(objectslist)-1
- while start1<stop1:
- for i in range(start1, stop1, 1):
- o1=copy.copy(objectslist[i])
- o2=copy.copy(objectslist[i+1])
- if func(o1)>func(o2):
- objectslist[i],objectslist[i+1]=o2,o1
- start2=stop1-1
- stop2=start1
- for i in range(start2, stop2-1, -1):
- o1=copy.copy(objectslist[i])
- o2=copy.copy(objectslist[i+1])
- if func(o1)>func(o2):
- objectslist[i],objectslist[i+1]=o2,o1
- start1=stop2+1
- stop1=start2
- return objectslist
- def func(el):
- return el[0]
- class lamp:
- def __init__(self,color,rmasc):
- self.color = color
- self.rmasc = rmasc
- self.r = self.rmasc.r
- self.SURF = pg.Surface((self.r*2+1,self.r*2+1),SRCALPHA)
- def light(self,x,y):
- deltax = self.r-x*sizeofcell
- deltay = self.r-y*sizeofcell
- numx = int(x)
- numy = int(y)
- dix0 = x-numx
- dix1 = 1-dix0
- diy0 = y-numy
- diy1 = 1-diy0
- pointstodraw=[]
- for el in pointsarray:
- elx=el['x']
- ely=el['y']
- quadra = (elx>=x)+(ely>=y)*2
- if el['id'] in quadrapoints[quadra]:#point can be visible
- tan = (y-ely)/(elx-x)
- cotan = 1/tan
- collision = False
- if quadra==1:
- ypos = y+dix0*tan
- for jx in range(numx+1,elx):
- ypos-=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- if not collision:
- xpos = x-diy1*cotan
- for jy in range(numy-1,ely-1,-1):
- xpos+=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- elif quadra==0:
- ypos = y-dix1*tan
- for jx in range(numx-1,elx-1,-1):
- ypos+=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- if not collision:
- xpos = x-diy1*cotan
- for jy in range(numy-1,ely-1,-1):
- xpos+=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- elif quadra==2:
- ypos = y-dix1*tan
- for jx in range(numx-1,elx-1,-1):
- ypos+=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- if not collision:
- xpos = x+diy0*cotan
- for jy in range(numy+1,ely):
- xpos-=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- elif quadra==3:
- ypos = y+dix0*tan
- for jx in range(numx+1,elx):
- ypos-=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- if not collision:
- xpos = x+diy0*cotan
- for jy in range(numy+1,ely):
- xpos-=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- collision = True
- break
- if not collision:
- angle = math.atan2(y-ely,elx-x)
- if el['id'] in (7,11,13,14) and quadra in (el['quadras'][0],el['quadras'][2]):
- if quadra==1:
- ypos = y+(dix0+numx-elx)*tan
- for jx in range(elx+1,mapshapex):
- ypos-=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- xl=y-ypos
- xpoint = (jx*sizeofcell-1,ypos*sizeofcell)
- break
- xpos = x-(diy1-numy+ely-1)*cotan
- for jy in range(ely-2,-1,-1):
- xpos+=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- yl=y-jy-1
- ypoint=(xpos*sizeofcell,(jy+1)*sizeofcell)
- break
- elif quadra==0:
- ypos = y-(dix1-numx+elx-1)*tan
- for jx in range(elx-2,-1,-1):
- ypos+=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- xl=y-ypos
- xpoint = ((jx+1)*sizeofcell,ypos*sizeofcell)
- break
- xpos = x-(diy1-numy+ely-1)*cotan
- for jy in range(ely-2,-1,-1):
- xpos+=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- yl=y-jy-1
- ypoint=(xpos*sizeofcell,(jy+1)*sizeofcell)
- break
- elif quadra==2:
- ypos = y-(dix1-numx+elx-1)*tan
- for jx in range(elx-2,-1,-1):
- ypos+=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- xl=y-ypos
- xpoint = ((jx+1)*sizeofcell,ypos*sizeofcell)
- break
- xpos = x+(diy0+numy-ely)*cotan
- for jy in range(ely+1,mapshapey):
- xpos-=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- yl=y-jy
- ypoint=(xpos*sizeofcell,jy*sizeofcell-1)
- break
- elif quadra==3:
- ypos = y+(dix0+numx-elx)*tan
- for jx in range(elx+1,mapshapex):
- ypos-=tan
- jy=int(ypos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- xl=y-ypos
- xpoint = (jx*sizeofcell-1,ypos*sizeofcell)
- break
- xpos = x+(diy0+numy-ely)*cotan
- for jy in range(ely+1,mapshapey):
- xpos-=cotan
- jx=int(xpos)
- if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
- yl=y-jy
- ypoint=(xpos*sizeofcell,jy*sizeofcell-1)
- break
- if abs(xl)<abs(yl):
- xdpos = xpoint[0]+deltax
- ydpos = xpoint[1]+deltay
- l = xl
- else:
- l = yl
- xdpos = ypoint[0]+deltax
- ydpos = ypoint[1]+deltay
- if quadra==el['quadras'][0]:
- point=[(xdpos,ydpos),(el['coord'][0]+deltax,el['coord'][1]+deltay)]
- elif quadra==el['quadras'][2]:
- point=[(el['coord'][0]+deltax,el['coord'][1]+deltay),(xdpos,ydpos)]
- else:
- point=[(el['coord'][0]+deltax,el['coord'][1]+deltay)]
- pointstodraw.append([angle,point])
- pointsdraw = []
- for obj in sortobjects(pointstodraw,func):
- pointsdraw.extend(obj[1])
- self.SURF.fill((0,0,0,255))
- pg.draw.polygon(self.SURF, (0,0,0,0), pointsdraw, 0)
- self.SURF.blit(self.rmasc.SURF,(0,0),None,BLEND_RGBA_MAX)
- SHADOWSURF.blit(self.SURF,(-deltax,-deltay),None,BLEND_RGBA_MIN)
- def drawmap(NSURF):
- for i in range(len(maparray)):
- for j in range(len(maparray[0])):
- if maparray[i][j]==0:
- pg.draw.rect(NSURF,black,(i*sizeofcell,j*sizeofcell,sizeofcell,sizeofcell))
- #NSURF.fill(transp,(i*sizeofcell,j*sizeofcell,sizeofcell,sizeofcell))
- #Set up colours
- black = (0, 0, 0)
- blue = (0, 0, 255)
- green = (0,255,0)
- gray = (128, 128, 128)
- red = (255, 0, 0)
- silver = (192,192,192)
- white = (255,255,255)
- cyan = (0,255,255)
- yellow = (255,255,0)
- magenta = (255,0,255)
- im = pg.image.load('map.png')
- maparray0 = pg.surfarray.array2d(im)
- maparray = [[]]
- for j in range(len(maparray0[0])+2):
- maparray[0].append(0)
- for i in range(1,len(maparray0)+1):
- maparray.append([])
- maparray[i].append(0)
- for j in range(len(maparray0[0])):
- maparray[i].append((maparray0[i-1][j]+16777216)//16777215)
- maparray[i].append(0)
- maparray.append([])
- for j in range(len(maparray0[0])+2):
- maparray[len(maparray0)+1].append(0)
- #maparray = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0], [0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
- mapshapex = len(maparray)
- mapshapey = len(maparray[0])
- sizeofcell = 50
- screenx=sizeofcell*mapshapex
- screeny=sizeofcell*mapshapey
- pointsarray = []
- for i in range(len(maparray)-1):
- for j in range(len(maparray[0])-1):
- idnumber = maparray[i][j]+maparray[i][j+1]*2+maparray[i+1][j+1]*4+maparray[i+1][j]*8
- if idnumber in (1,2,4,8,14,13,11,7,5,10): #it's edge
- pointsarray.append({'x':i+1,'y':j+1,'id':idnumber,'coord':((i+1)*sizeofcell,(j+1)*sizeofcell)})
- if idnumber==14:
- pointsarray[-1]['quadras']=(1,0,2)
- elif idnumber==13:
- pointsarray[-1]['quadras']=(0,2,3)
- elif idnumber==11:
- pointsarray[-1]['quadras']=(2,3,1)
- elif idnumber==7:
- pointsarray[-1]['quadras']=(3,1,0)
- quadrapoints = ((4,5,7,13,14),(2,10,7,11,14),(8,10,11,13,14),(5,1,7,11,13))
- pg.init()
- DISPLAYSURF = pg.display.set_mode((screenx,screeny))
- DISPLAYSURF.fill(blue)
- SHADOWSURF = pg.Surface((screenx,screeny),SRCALPHA)
- pg.display.set_caption('lighting')
- font = pg.font.SysFont('comicsansms', 15)
- dx = 0.01
- pi05 = math.pi*0.5
- pi = math.pi
- pi15 = math.pi*1.5
- pi2 = math.pi*2
- x = 5.5
- y = 5.5
- movestatex = 0
- movestatey = 0
- clock = pg.time.Clock()
- shadowcolor = (0,0,0,255) #black not transp
- roundmasc200 = roundmasc(200,255,(0,0,0))
- sch = 0
- mylamp = lamp(white,roundmasc200)
- #background
- TEXTURE = pg.image.load('texture.png').convert()
- BACKSURF = DISPLAYSURF.copy()
- for i in range(screenx//TEXTURE.get_width()):
- for j in range(screeny//TEXTURE.get_height()):
- BACKSURF.blit(TEXTURE, (i*TEXTURE.get_width(),j*TEXTURE.get_height()))
- while True: # main game loop
- for event in pg.event.get():
- if event.type == QUIT:
- pg.quit()
- sys.exit()
- #move
- elif event.type == KEYDOWN:
- if event.key == K_UP:
- movestatey = 1
- elif event.key == K_DOWN:
- movestatey = -1
- elif event.key == K_RIGHT:
- movestatex = 1
- elif event.key == K_LEFT:
- movestatex = -1
- elif event.type == KEYUP:
- if event.key == K_UP or event.key == K_DOWN:
- movestatey = 0
- elif event.key == K_RIGHT or event.key == K_LEFT:
- movestatex = 0
- newx=x+dx*movestatex
- newy=y-dx*movestatey
- if newx-int(newx)<0.001:
- newx+=0.001
- if newx-int(newx)>0.999:
- newx-=0.001
- if newy-int(newy)<0.001:
- newy+=0.001
- if newy-int(newy)>0.999:
- newy-=0.001
- if maparray[int(newx)][int(newy)]==1:
- x=newx
- y=newy
- elif maparray[int(x)][int(newy)]==1:
- y=newy
- elif maparray[int(newx)][int(y)]==1:
- x=newx
- #light
- DISPLAYSURF.blit(BACKSURF,(0,0))
- SHADOWSURF.fill(shadowcolor)
- mylamp.light(x,y)
- DISPLAYSURF.blit(SHADOWSURF,(0,0))
- pg.draw.line(DISPLAYSURF, white, (x*sizeofcell,y*sizeofcell), (x*sizeofcell,y*sizeofcell), 3)
- sch+=1
- if sch%1000==0:
- print(clock.get_fps())
- pg.display.update()
- clock.tick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement