Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.92 KB | None | 0 0
  1. import math
  2. import pygame as pg, sys
  3. from pygame.locals import *
  4. import copy
  5.  
  6. class roundmasc:
  7. def __init__(self,r,intens,color):
  8. self.r = r
  9. self.intens = intens
  10. self.color = color
  11. self.SURF = pg.Surface((2*r+1,2*r+1),SRCALPHA)
  12. mascarr = pg.PixelArray(self.SURF)
  13. self.h2 = r**2*(1/(self.intens**(1/3)-1))
  14. imult = self.intens*self.h2**1.5
  15. for x in range(len(mascarr)):
  16. for y in range(len(mascarr[0])):
  17. nowr2 = (r-x)**2 + (r-y)**2
  18. calculat = int(imult/(nowr2+self.h2)**1.5)
  19. nowint = max(0,min(255,255-calculat))
  20. mascarr[x,y]=(color[0],color[1],color[2],nowint)
  21. self.SURF.unlock()
  22.  
  23. def sortobjects(objectslist,func):
  24. start1=0
  25. stop1=len(objectslist)-1
  26. while start1<stop1:
  27. for i in range(start1, stop1, 1):
  28. o1=copy.copy(objectslist[i])
  29. o2=copy.copy(objectslist[i+1])
  30. if func(o1)>func(o2):
  31. objectslist[i],objectslist[i+1]=o2,o1
  32. start2=stop1-1
  33. stop2=start1
  34. for i in range(start2, stop2-1, -1):
  35. o1=copy.copy(objectslist[i])
  36. o2=copy.copy(objectslist[i+1])
  37. if func(o1)>func(o2):
  38. objectslist[i],objectslist[i+1]=o2,o1
  39. start1=stop2+1
  40. stop1=start2
  41. return objectslist
  42.  
  43. def func(el):
  44. return el[0]
  45.  
  46. class lamp:
  47. def __init__(self,color,rmasc):
  48. self.color = color
  49. self.rmasc = rmasc
  50. self.r = self.rmasc.r
  51. self.SURF = pg.Surface((self.r*2+1,self.r*2+1),SRCALPHA)
  52. def light(self,x,y):
  53. deltax = self.r-x*sizeofcell
  54. deltay = self.r-y*sizeofcell
  55. numx = int(x)
  56. numy = int(y)
  57. dix0 = x-numx
  58. dix1 = 1-dix0
  59. diy0 = y-numy
  60. diy1 = 1-diy0
  61. pointstodraw=[]
  62. for el in pointsarray:
  63. elx=el['x']
  64. ely=el['y']
  65. quadra = (elx>=x)+(ely>=y)*2
  66. if el['id'] in quadrapoints[quadra]:#point can be visible
  67. tan = (y-ely)/(elx-x)
  68. cotan = 1/tan
  69. collision = False
  70. if quadra==1:
  71. ypos = y+dix0*tan
  72. for jx in range(numx+1,elx):
  73. ypos-=tan
  74. jy=int(ypos)
  75. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  76. collision = True
  77. break
  78. if not collision:
  79. xpos = x-diy1*cotan
  80. for jy in range(numy-1,ely-1,-1):
  81. xpos+=cotan
  82. jx=int(xpos)
  83. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  84. collision = True
  85. break
  86. elif quadra==0:
  87. ypos = y-dix1*tan
  88. for jx in range(numx-1,elx-1,-1):
  89. ypos+=tan
  90. jy=int(ypos)
  91. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  92. collision = True
  93. break
  94. if not collision:
  95. xpos = x-diy1*cotan
  96. for jy in range(numy-1,ely-1,-1):
  97. xpos+=cotan
  98. jx=int(xpos)
  99. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  100. collision = True
  101. break
  102. elif quadra==2:
  103. ypos = y-dix1*tan
  104. for jx in range(numx-1,elx-1,-1):
  105. ypos+=tan
  106. jy=int(ypos)
  107. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  108. collision = True
  109. break
  110. if not collision:
  111. xpos = x+diy0*cotan
  112. for jy in range(numy+1,ely):
  113. xpos-=cotan
  114. jx=int(xpos)
  115. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  116. collision = True
  117. break
  118. elif quadra==3:
  119. ypos = y+dix0*tan
  120. for jx in range(numx+1,elx):
  121. ypos-=tan
  122. jy=int(ypos)
  123. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  124. collision = True
  125. break
  126. if not collision:
  127. xpos = x+diy0*cotan
  128. for jy in range(numy+1,ely):
  129. xpos-=cotan
  130. jx=int(xpos)
  131. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  132. collision = True
  133. break
  134. if not collision:
  135. angle = math.atan2(y-ely,elx-x)
  136. if el['id'] in (7,11,13,14) and quadra in (el['quadras'][0],el['quadras'][2]):
  137. if quadra==1:
  138. ypos = y+(dix0+numx-elx)*tan
  139. for jx in range(elx+1,mapshapex):
  140. ypos-=tan
  141. jy=int(ypos)
  142. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  143. xl=y-ypos
  144. xpoint = (jx*sizeofcell-1,ypos*sizeofcell)
  145. break
  146. xpos = x-(diy1-numy+ely-1)*cotan
  147. for jy in range(ely-2,-1,-1):
  148. xpos+=cotan
  149. jx=int(xpos)
  150. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  151. yl=y-jy-1
  152. ypoint=(xpos*sizeofcell,(jy+1)*sizeofcell)
  153. break
  154. elif quadra==0:
  155. ypos = y-(dix1-numx+elx-1)*tan
  156. for jx in range(elx-2,-1,-1):
  157. ypos+=tan
  158. jy=int(ypos)
  159. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  160. xl=y-ypos
  161. xpoint = ((jx+1)*sizeofcell,ypos*sizeofcell)
  162. break
  163. xpos = x-(diy1-numy+ely-1)*cotan
  164. for jy in range(ely-2,-1,-1):
  165. xpos+=cotan
  166. jx=int(xpos)
  167. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  168. yl=y-jy-1
  169. ypoint=(xpos*sizeofcell,(jy+1)*sizeofcell)
  170. break
  171. elif quadra==2:
  172. ypos = y-(dix1-numx+elx-1)*tan
  173. for jx in range(elx-2,-1,-1):
  174. ypos+=tan
  175. jy=int(ypos)
  176. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  177. xl=y-ypos
  178. xpoint = ((jx+1)*sizeofcell,ypos*sizeofcell)
  179. break
  180. xpos = x+(diy0+numy-ely)*cotan
  181. for jy in range(ely+1,mapshapey):
  182. xpos-=cotan
  183. jx=int(xpos)
  184. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  185. yl=y-jy
  186. ypoint=(xpos*sizeofcell,jy*sizeofcell-1)
  187. break
  188. elif quadra==3:
  189. ypos = y+(dix0+numx-elx)*tan
  190. for jx in range(elx+1,mapshapex):
  191. ypos-=tan
  192. jy=int(ypos)
  193. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  194. xl=y-ypos
  195. xpoint = (jx*sizeofcell-1,ypos*sizeofcell)
  196. break
  197. xpos = x+(diy0+numy-ely)*cotan
  198. for jy in range(ely+1,mapshapey):
  199. xpos-=cotan
  200. jx=int(xpos)
  201. if jx<0 or jy<0 or jx>=mapshapex or jy>=mapshapey or maparray[jx][jy]==0:
  202. yl=y-jy
  203. ypoint=(xpos*sizeofcell,jy*sizeofcell-1)
  204. break
  205. if abs(xl)<abs(yl):
  206. xdpos = xpoint[0]+deltax
  207. ydpos = xpoint[1]+deltay
  208. l = xl
  209. else:
  210. l = yl
  211. xdpos = ypoint[0]+deltax
  212. ydpos = ypoint[1]+deltay
  213. if quadra==el['quadras'][0]:
  214. point=[(xdpos,ydpos),(el['coord'][0]+deltax,el['coord'][1]+deltay)]
  215.  
  216. elif quadra==el['quadras'][2]:
  217. point=[(el['coord'][0]+deltax,el['coord'][1]+deltay),(xdpos,ydpos)]
  218.  
  219. else:
  220. point=[(el['coord'][0]+deltax,el['coord'][1]+deltay)]
  221. pointstodraw.append([angle,point])
  222. pointsdraw = []
  223. for obj in sortobjects(pointstodraw,func):
  224. pointsdraw.extend(obj[1])
  225. self.SURF.fill((0,0,0,255))
  226. pg.draw.polygon(self.SURF, (0,0,0,0), pointsdraw, 0)
  227. self.SURF.blit(self.rmasc.SURF,(0,0),None,BLEND_RGBA_MAX)
  228. SHADOWSURF.blit(self.SURF,(-deltax,-deltay),None,BLEND_RGBA_MIN)
  229.  
  230. def drawmap(NSURF):
  231. for i in range(len(maparray)):
  232. for j in range(len(maparray[0])):
  233. if maparray[i][j]==0:
  234. pg.draw.rect(NSURF,black,(i*sizeofcell,j*sizeofcell,sizeofcell,sizeofcell))
  235. #NSURF.fill(transp,(i*sizeofcell,j*sizeofcell,sizeofcell,sizeofcell))
  236.  
  237.  
  238.  
  239. #Set up colours
  240. black = (0, 0, 0)
  241. blue = (0, 0, 255)
  242. green = (0,255,0)
  243. gray = (128, 128, 128)
  244. red = (255, 0, 0)
  245. silver = (192,192,192)
  246. white = (255,255,255)
  247. cyan = (0,255,255)
  248. yellow = (255,255,0)
  249. magenta = (255,0,255)
  250.  
  251.  
  252. im = pg.image.load('map.png')
  253. maparray0 = pg.surfarray.array2d(im)
  254. maparray = [[]]
  255. for j in range(len(maparray0[0])+2):
  256. maparray[0].append(0)
  257. for i in range(1,len(maparray0)+1):
  258. maparray.append([])
  259. maparray[i].append(0)
  260. for j in range(len(maparray0[0])):
  261. maparray[i].append((maparray0[i-1][j]+16777216)//16777215)
  262. maparray[i].append(0)
  263. maparray.append([])
  264. for j in range(len(maparray0[0])+2):
  265. maparray[len(maparray0)+1].append(0)
  266.  
  267. #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]]
  268.  
  269. mapshapex = len(maparray)
  270. mapshapey = len(maparray[0])
  271.  
  272. sizeofcell = 50
  273. screenx=sizeofcell*mapshapex
  274. screeny=sizeofcell*mapshapey
  275.  
  276. pointsarray = []
  277. for i in range(len(maparray)-1):
  278. for j in range(len(maparray[0])-1):
  279. idnumber = maparray[i][j]+maparray[i][j+1]*2+maparray[i+1][j+1]*4+maparray[i+1][j]*8
  280. if idnumber in (1,2,4,8,14,13,11,7,5,10): #it's edge
  281. pointsarray.append({'x':i+1,'y':j+1,'id':idnumber,'coord':((i+1)*sizeofcell,(j+1)*sizeofcell)})
  282. if idnumber==14:
  283. pointsarray[-1]['quadras']=(1,0,2)
  284. elif idnumber==13:
  285. pointsarray[-1]['quadras']=(0,2,3)
  286. elif idnumber==11:
  287. pointsarray[-1]['quadras']=(2,3,1)
  288. elif idnumber==7:
  289. pointsarray[-1]['quadras']=(3,1,0)
  290.  
  291. quadrapoints = ((4,5,7,13,14),(2,10,7,11,14),(8,10,11,13,14),(5,1,7,11,13))
  292.  
  293. pg.init()
  294. DISPLAYSURF = pg.display.set_mode((screenx,screeny))
  295. DISPLAYSURF.fill(blue)
  296. SHADOWSURF = pg.Surface((screenx,screeny),SRCALPHA)
  297. pg.display.set_caption('lighting')
  298.  
  299. font = pg.font.SysFont('comicsansms', 15)
  300.  
  301. dx = 0.01
  302. pi05 = math.pi*0.5
  303. pi = math.pi
  304. pi15 = math.pi*1.5
  305. pi2 = math.pi*2
  306.  
  307. x = 5.5
  308. y = 5.5
  309. movestatex = 0
  310. movestatey = 0
  311. clock = pg.time.Clock()
  312.  
  313. shadowcolor = (0,0,0,255) #black not transp
  314.  
  315. roundmasc200 = roundmasc(200,255,(0,0,0))
  316.  
  317. sch = 0
  318. mylamp = lamp(white,roundmasc200)
  319.  
  320. #background
  321. TEXTURE = pg.image.load('texture.png').convert()
  322. BACKSURF = DISPLAYSURF.copy()
  323. for i in range(screenx//TEXTURE.get_width()):
  324. for j in range(screeny//TEXTURE.get_height()):
  325. BACKSURF.blit(TEXTURE, (i*TEXTURE.get_width(),j*TEXTURE.get_height()))
  326.  
  327. while True: # main game loop
  328.  
  329. for event in pg.event.get():
  330. if event.type == QUIT:
  331. pg.quit()
  332. sys.exit()
  333. #move
  334. elif event.type == KEYDOWN:
  335. if event.key == K_UP:
  336. movestatey = 1
  337. elif event.key == K_DOWN:
  338. movestatey = -1
  339. elif event.key == K_RIGHT:
  340. movestatex = 1
  341. elif event.key == K_LEFT:
  342. movestatex = -1
  343. elif event.type == KEYUP:
  344. if event.key == K_UP or event.key == K_DOWN:
  345. movestatey = 0
  346. elif event.key == K_RIGHT or event.key == K_LEFT:
  347. movestatex = 0
  348. newx=x+dx*movestatex
  349. newy=y-dx*movestatey
  350. if newx-int(newx)<0.001:
  351. newx+=0.001
  352. if newx-int(newx)>0.999:
  353. newx-=0.001
  354. if newy-int(newy)<0.001:
  355. newy+=0.001
  356. if newy-int(newy)>0.999:
  357. newy-=0.001
  358. if maparray[int(newx)][int(newy)]==1:
  359. x=newx
  360. y=newy
  361. elif maparray[int(x)][int(newy)]==1:
  362. y=newy
  363. elif maparray[int(newx)][int(y)]==1:
  364. x=newx
  365.  
  366. #light
  367. DISPLAYSURF.blit(BACKSURF,(0,0))
  368. SHADOWSURF.fill(shadowcolor)
  369. mylamp.light(x,y)
  370. DISPLAYSURF.blit(SHADOWSURF,(0,0))
  371. pg.draw.line(DISPLAYSURF, white, (x*sizeofcell,y*sizeofcell), (x*sizeofcell,y*sizeofcell), 3)
  372.  
  373. sch+=1
  374. if sch%1000==0:
  375. print(clock.get_fps())
  376.  
  377.  
  378. pg.display.update()
  379. clock.tick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement