Advertisement
Guest User

Cardistry

a guest
May 28th, 2022
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.32 KB | None | 0 0
  1. import pygame as py
  2. import math as m
  3. from random import randint as rn
  4.  
  5. py.font.init()
  6. font = py.font.Font("slkscre.ttf",16)
  7.  
  8. bigfont = py.font.Font("slkscre.ttf",32)
  9.  
  10. def dis(x,y,x2,y2):
  11. return m.sqrt((x-x2)**2+(y-y2)**2)
  12.  
  13. def upload(imag, sx, sy):
  14. i = py.image.load(imag+".png").convert()
  15. i = py.transform.scale(i,(sx,sy))
  16. i.set_colorkey((110.0, 184.0, 168.0))
  17.  
  18. return i
  19. def rotation(angle, image, screen, xyset, size):
  20. x, y = xyset
  21. avv = py.transform.rotate(image, -angle / m.pi * 180+90)
  22. difx = int(abs(avv.get_width()-size)/2)
  23. dify = int(abs(avv.get_height()-size)/2)
  24.  
  25. screen.blit(avv, (x-difx+sx, y-dify+sy))
  26.  
  27. particles =[]
  28.  
  29.  
  30. def createP(gx,gy,num,color,speed,size,special_param):
  31.  
  32. for i in range(num):
  33. if special_param == "default":
  34. ag = rn(0,640)*0.01
  35. speedo = rn(speed[0],speed[1])*0.1
  36. sizeo = rn(size[0],size[1])*0.1
  37. p = [gx,gy,m.cos(ag)*speedo,m.sin(ag)*speedo,sizeo,color]
  38. else:
  39. vx_add,vy_add, gradient_1, gradient_2,gravity = special_param
  40. ag = rn(0,640)*0.01
  41. speedo = rn(speed[0],speed[1])*0.1
  42. sizeo = rn(size[0],size[1])*0.1
  43. p = [gx,gy,m.cos(ag)*speedo+vx_add,m.sin(ag)*speedo+vy_add,sizeo,[gradient_1,gradient_2,0,gravity]]
  44. particles.append(p)
  45. screen = py.display.set_mode((800,800))
  46. global absId
  47. absId = 0
  48. class Card():
  49. def __init__(self,img,cost):
  50. global absId
  51.  
  52. self.id = absId
  53. self.img = img
  54. self.cost = cost
  55. self.bigimg = py.transform.scale(self.img,(192,192))
  56. absId += 1
  57. global AgList
  58. AgList = [-m.pi*0.075,-m.pi*0.05,-m.pi*0.025,0,
  59. m.pi*0.025,m.pi*0.05,m.pi*0.075,m.pi*0.125]
  60. global yOffsetList
  61. yOffsetList= [30,20,10,0,10,20,30,50]
  62. global mox
  63. global moy
  64. mox, moy = 0,0
  65. global sx
  66. global sy
  67. sx, sy = 0,0
  68. global bullets
  69. bullets = []
  70. global runo
  71. runo = 0
  72. global win
  73. win = 0
  74.  
  75. class Player():
  76. def __init__(self,x,y):
  77. self.x = x
  78. self.y = y
  79. self.vx = 0
  80. self.vy = 0
  81. self.health = 100
  82. self.hand = []
  83. self.shield = 0
  84. self.orbsOwned = 5
  85. def draw(self):
  86. py.draw.rect(screen,(255,255,255),(self.x-7+sx,self.y-7+sy,14,14))
  87. def drawCards(self):
  88.  
  89. isLookingAtCard = 0
  90. for card in self.hand:
  91. indexx = self.hand.index(card)
  92. #py.draw.ellipse(screen,(255,255,255),(230+indexx*60-50+64,650+yOffsetList[indexx]-50+64,100,100),2)
  93. if dis(200+indexx*60+64,580+yOffsetList[indexx]+64,mox,moy)<50 and isLookingAtCard == 0:
  94. isLookingAtCard = card
  95.  
  96. else:
  97. rotation(m.pi/2+AgList[indexx],card.img,screen,(200+indexx*60,580+yOffsetList[indexx]),128)
  98.  
  99.  
  100. for card in self.hand:
  101. indexx = self.hand.index(card)
  102.  
  103. if dis(200+indexx*60+64,580+yOffsetList[indexx]+64,mox,moy)<50 and card == isLookingAtCard:
  104. screen.blit(card.bigimg,(200+indexx*60+sx,550+yOffsetList[indexx]+sy))
  105. txt = font.render("Cost: " + str(card.cost),True,(255,255,255))
  106. screen.blit(txt,(200+indexx*60+sx+60,550+yOffsetList[indexx]+sy-20))
  107. return card
  108. if isLookingAtCard == 0:
  109. return 0
  110.  
  111. def move(self):
  112. self.x += self.vx
  113. self.y += self.vy
  114. if abs(self.x-400)>400 and abs(self.vx)/self.vx*abs(self.x-400)/(self.x-400) > 0:
  115. self.x-=self.vx
  116. if abs(self.y-250)>250 and abs(self.vy)/self.vy*abs(self.y-250)/(self.y-250) > 0:
  117. self.y-=self.vy
  118. def checkKeys(self,e,booll):
  119. key = e.key
  120. if booll:
  121.  
  122. if key == py.K_a:
  123. self.vx = -2
  124. if key == py.K_d:
  125. self.vx = 2
  126. if key == py.K_w:
  127. self.vy = -2
  128. if key == py.K_s:
  129. self.vy = 2
  130. else:
  131. if key == py.K_a or key == py.K_d:
  132. self.vx = 0
  133. if key == py.K_w or key == py.K_s:
  134. self.vy = 0
  135. class Boss():
  136. def __init__(self,img):
  137. self.x = 400
  138. self. y = 250
  139. self.pattern = 0
  140. self.internalClock = 0
  141. self.img = img
  142. self.size = self.img.get_width()
  143. self.health = 1000
  144. def draw(self):
  145. screen.blit(self.img,(self.x-self.size/2+sx,self.y-self.size/2+sy))
  146. py.draw.rect(screen,(255,255,255),(self.x-100,self.y-100,self.health/5,15))
  147. py.draw.rect(screen,(255,255,255),(self.x-110,self.y-110,200+20,15+20),5)
  148. def attack(self):
  149. self.internalClock += 1
  150. if self.internalClock > 500:
  151. if self.pattern == 0 and self.internalClock%8 == 0:
  152. for i in range(12):
  153. b = [self.x,self.y,m.cos(i/6*m.pi+m.pi*self.internalClock/600)*4,m.sin(i/6*m.pi+m.pi*self.internalClock/600)*4]
  154. bullets.append(b)
  155. if self.pattern == 1 and self.internalClock%8 == 0:
  156. for i in range(12):
  157. b = [self.x,self.y,m.cos(i/6*m.pi-m.pi*self.internalClock/600)*4,m.sin(i/6*m.pi-m.pi*self.internalClock/600)*4]
  158. bullets.append(b)
  159. if self.pattern == 2 and self.internalClock%2 == 0:
  160. ag = rn(0,640)*0.01
  161. b = [self.x,self.y,m.cos(ag)*2,m.sin(ag)*2]
  162. bullets.append(b)
  163.  
  164. if self.internalClock > 600:
  165. self.internalClock = 0
  166. self.pattern = rn(0,2)
  167.  
  168.  
  169. def GameLoop():
  170. global mox
  171. global moy
  172. global sx
  173. global sy
  174. global bullets
  175. global runo
  176. global win
  177.  
  178. def DrawACard(player):
  179. if len(player.hand)<8 and player.orbsOwned>0:
  180. player.orbsOwned -= 1
  181. player.hand.append(deck[0])
  182. deck.remove(deck[0])
  183. return 1
  184. else:
  185. return 0
  186. def doCardEffect(card):
  187. p1.orbsOwned -= card.cost
  188. if card.img == laserimg:
  189. nodes = []
  190. for i in range(int(dis(p1.x,p1.y,boss1.x,boss1.y)/50)):
  191. nodes.append([rn(-80,80)*0.01,rn(8,40)+i*30])
  192. lasers.append([40,nodes])
  193. if card.img == shieldimg:
  194. if p1.shield+30>70:
  195. p1.shield = 70
  196. else:
  197. p1.shield+=15
  198. createP(p1.x,p1.y,30,(255,255,255),[5,15],[200,320],"default")
  199. if card.img == swordcardimg:
  200. for i in range(12):
  201. disto = dis(p1.x,p1.y,boss1.x,boss1.y)+rn(-20,20)
  202. x = boss1.x+m.cos(rn(0,640))*disto
  203. y = boss1.y+m.sin(rn(0,640))*disto
  204. ag = m.atan2(y-boss1.y,x-boss1.x)+m.pi
  205.  
  206. setOffTimer = rn(2,60)
  207. sword = [x,y,setOffTimer,ag]
  208. swords.append(sword)
  209. if card.img == rocket:
  210. ag = m.atan2(p1.y-boss1.y,p1.x-boss1.x)+m.pi
  211. for i in range(2):
  212. ro = [p1.x+m.cos(ag+m.pi/2-m.pi*i)*30,p1.y+m.cos(ag+m.pi/2-m.pi*i)*30,m.cos(ag)*5,m.sin(ag)*5,50+100*i,ag]
  213. rockets.append(ro)
  214.  
  215. p1 = Player(200,400)
  216. deckimg = upload("CBHCardsBack",128,128)
  217. laserimg = upload("CBHlaser",128,128)
  218. shieldimg = upload("CBHshield",128,128)
  219. swordcardimg = upload("CBHsword",128,128)
  220. rocket = upload("CBHrocket",128,128)
  221.  
  222. cardSets = [[laserimg,3],[shieldimg,2],[swordcardimg,5],[rocket,4]]
  223.  
  224. p1.hand=[]
  225. deck = []
  226. for i in range(80):
  227. img, cost = cardSets[rn(0,len(cardSets)-1)]
  228. actualCard = Card(img,cost)
  229. if len(deck)>3:
  230. if not (actualCard == deck[i-1] and actualCard == deck[i-2]):
  231. deck.append(actualCard)
  232. else:
  233. deck.append(actualCard)
  234.  
  235. print(len(deck))
  236. cardImLookingAt = p1.drawCards()
  237. bullets = []
  238. boss1 = Boss(upload("CBHBoss0",128,128))
  239. lasers = []
  240. sx,sy = 0,0
  241. shake = [0,0]
  242. youCanDraw = 1
  243. for i in range(7):
  244. DrawACard(p1)
  245. orbs = []
  246. drawTimer = 300
  247. orbimg = upload("CBHOrbs",32,32)
  248. vialimg = upload("CBHVial",256,256)
  249. vialimg2 = upload("CBHVialBitFilled",256,256)
  250. p1.orbsOwned = 10
  251. swords = []
  252. swordimg = upload("CBHindividualsword",64,64)
  253. automaticOrb = 0
  254. rockets = []
  255. rocketimg = upload("CBHrocketimg",96,96)
  256. cl = py.time.Clock()
  257. run = 1
  258. while run:
  259. screen.fill((0,0,0))
  260. mox, moy = py.mouse.get_pos()
  261.  
  262. if shake[1]:
  263. shake[1]-=1
  264. sx,sy = rn(-10,10)*shake[0],rn(-4,4)*shake[0]
  265. else:
  266. sx,sy = 0,0
  267. #Bars
  268. py.draw.rect(screen,(255,255,255),(20,20,p1.health*3,15))
  269. py.draw.rect(screen,(255,255,255),(10,10,300+20,15+20),5)
  270. if p1.health>50:
  271. txt = font.render(str(p1.health)+ "/100",True,(0,0,0))
  272. screen.blit(txt,(30,27-txt.get_height()/2))
  273. else:
  274. txt = font.render(str(p1.health)+ "/100",True,(255,255,255))
  275. screen.blit(txt,(310-txt.get_width(),27-txt.get_height()/2))
  276. if p1.health<0:
  277. run = 0
  278.  
  279. py.draw.rect(screen,(255,255,255),(20,70,p1.shield*3,15))
  280. py.draw.rect(screen,(255,255,255),(10,60,210+20,15+20),5)
  281. if p1.shield>35:
  282. txt = font.render(str(p1.shield)+ "/70",True,(0,0,0))
  283. screen.blit(txt,(30,77-txt.get_height()/2))
  284. else:
  285. txt = font.render(str(p1.shield)+ "/70",True,(255,255,255))
  286. screen.blit(txt,(220-txt.get_width(),77-txt.get_height()/2))
  287.  
  288.  
  289. if p1.shield<0:
  290. p1.shield = 0
  291.  
  292.  
  293. #Orbs and vial
  294. if drawTimer>0:
  295. drawTimer -= 1
  296. elif len(orbs)<10:
  297. orbs.append([rn(100,500),rn(100,400)])
  298. drawTimer = 150
  299. if p1.orbsOwned>0:
  300. youCanDraw = 1
  301. else:
  302. youCanDraw = 0
  303. if p1.orbsOwned>23:
  304. p1.orbsOwned = 23
  305. if p1.orbsOwned>0:
  306. screen.blit(vialimg2,(600,530))
  307. else:
  308. screen.blit(vialimg,(600,530))
  309. if automaticOrb<10 and p1.orbsOwned>0:
  310. automaticOrb += 0.01
  311. else:
  312. automaticOrb = 0
  313. p1.orbsOwned += 1
  314. if boss1.health>0:
  315. py.draw.rect(screen,(255,255,255),(695,760-(p1.orbsOwned*10+automaticOrb),60,(p1.orbsOwned*10+automaticOrb)))
  316.  
  317. #Event handling
  318. for e in py.event.get():
  319. if e.type == py.QUIT:
  320. run =0
  321. runo = 0
  322. if e.type == py.KEYDOWN:
  323. p1.checkKeys(e,1)
  324. if e.key == py.K_SPACE and boss1.health<0:
  325. run = 0
  326. win = 1
  327. if e.type == py.KEYUP:
  328. p1.checkKeys(e,0)
  329. if e.type == py.MOUSEBUTTONDOWN:
  330. if e.button == 1 and boss1.health>0:
  331. if youCanDraw and dis(30+64,630+64,mox,moy)<50:
  332. DrawACard(p1)
  333.  
  334. if cardImLookingAt != 0:
  335. if p1.orbsOwned>= cardImLookingAt.cost:
  336. p1.hand.remove(cardImLookingAt)
  337. doCardEffect(cardImLookingAt)
  338. screen.blit(py.transform.scale(cardImLookingAt.img,(256,256)),(400-128+sx,400-128+sy))
  339. py.display.update()
  340. py.time.wait(450)
  341.  
  342.  
  343. #Bullets
  344. for b in bullets:
  345. py.draw.circle(screen,(255,255,255),(b[0]+sx,b[1]+sy),6)
  346. b[0] += b[2]
  347. b[1] += b[3]
  348. if abs(b[0]-400)>400 or abs(b[1]-250)>250 or b[1]>500:
  349. bullets.remove(b)
  350. if abs(b[0]-p1.x)<7 and abs(b[1]-p1.y)<7 and b in bullets:
  351. if p1.shield:
  352. p1.shield-=6
  353. else:
  354. p1.health -= 6
  355. bullets.remove(b)
  356. shake = [1.2,60]
  357. #Swords
  358. for sw in swords:
  359. rotation(sw[3],swordimg,screen,(sw[0]-32,sw[1]-32),64)
  360. sw[2]-=1
  361. if sw[2]<0:
  362. sw[0]+=m.cos(sw[3])*8
  363. sw[1]+=m.sin(sw[3])*8
  364. if abs(sw[0]-boss1.x)<50 and abs(sw[1]-boss1.y)<50:
  365. createP(sw[0],sw[1],10,(255,255,255),[2,15],[40,400],"default")
  366.  
  367. boss1.health -= 15
  368. swords.remove(sw)
  369.  
  370.  
  371. #Lasers
  372. for laser in lasers:
  373. nodes = laser[1]
  374. createP(boss1.x,boss1.y,2,(255,255,255),[2,25],[int(laser[0]*5),int(laser[0]*5)+50],"default")
  375. Ag = m.atan2(p1.y-boss1.y,p1.x-boss1.x)+m.pi
  376. for i in nodes:
  377. if i == nodes[0]:
  378. py.draw.line(screen,(255,255,255),(p1.x+sx,p1.y+sy),(p1.x+m.cos(Ag+i[0])*i[1],p1.y+m.sin(Ag+i[0])*i[1]),int(laser[0]))
  379. else:
  380. o = nodes[nodes.index(i)-1]
  381. py.draw.line(screen,(255,255,255),(p1.x+m.cos(Ag+o[0])*o[1],p1.y+m.sin(Ag+o[0])*o[1]),(p1.x+m.cos(Ag+i[0])*i[1],p1.y+m.sin(Ag+i[0])*i[1]),int(laser[0]))
  382. if i == nodes[len(nodes)-1]:
  383. py.draw.line(screen,(255,255,255),(boss1.x,boss1.y),(p1.x+m.cos(Ag+i[0])*i[1],p1.y+m.sin(Ag+i[0])*i[1]),int(laser[0]))
  384. if int(laser[0]%4)== 0:
  385. nodesnew = []
  386. for i in range(int(dis(p1.x,p1.y,boss1.x,boss1.y)/50)):
  387. nodesnew.append([rn(-80,80)*0.01,rn(8,40)+i*30])
  388. laser[1]=nodesnew
  389. laser[0]-=0.2
  390. boss1.health -= 0.4
  391.  
  392. if laser[0]<10:
  393. lasers.remove(laser)
  394. #Particles
  395. for p in particles:
  396. py.draw.circle(screen,(255,255,255),(p[0]+sx,p[1]+sy),p[4])
  397. p[0]+=p[2]
  398. p[1]+=p[3]
  399. p[4]-=0.4
  400. if p[4]<3:
  401. particles.remove(p)
  402. #Rockets
  403. for ro in rockets:
  404. rotation(ro[5]+m.pi,rocketimg,screen,(ro[0]-48,ro[1]-48),96)
  405. createP(ro[0]-ro[2]*10,ro[1]-ro[3]*10,1,(255,255,255),[2,10],[40,200],[-ro[2],-ro[3],(255,255,255),(255,255,255),0])
  406.  
  407. ro[4]-=1
  408. if ro[4]<0:
  409. ro[0]+=ro[2]
  410. ro[1]+=ro[3]
  411. if abs(ro[0]-boss1.x)<50 and abs(ro[1]-boss1.y)<50:
  412. createP(ro[0],ro[1],60,(255,255,255),[2,25],[40,500],"default")
  413.  
  414. boss1.health -= 55
  415. rockets.remove(ro)
  416. #Orbs
  417. if boss1.health<0:
  418. orbs = []
  419. p1.orbsOwned = 0
  420. for orb in orbs:
  421. screen.blit(orbimg,(orb[0]-16,orb[1]-16))
  422. if abs(orb[0]-p1.x)<10 and abs(orb[1]-p1.y)<10:
  423. orbs.remove(orb)
  424. p1.orbsOwned += 1
  425. ag = m.atan2(p1.y-orb[1],p1.x-orb[0])
  426. dist = dis(orb[0],orb[1],p1.x,p1.y)+0.01
  427. orb[0] += m.cos(ag)/((dist/20)**2)
  428. orb[1] += m.sin(ag)/((dist/20)**2)
  429. #Boss
  430.  
  431. if boss1.health<0:
  432. p1.hand = []
  433. txt = font.render("Press Space to go back to menu",True,(255,255,255))
  434. screen.blit(txt,(400-txt.get_width()/2,250-txt.get_height()/2))
  435. else:
  436. boss1.draw()
  437. boss1.attack()
  438. #Drawing deck
  439. for i in range(3):
  440. screen.blit(deckimg,(30-i*5+sx,630-i*5+sy))
  441.  
  442. #Player
  443. p1.draw()
  444. p1.move()
  445. cardImLookingAt = p1.drawCards()
  446.  
  447. cl.tick(150)
  448. py.display.update()
  449. def MainMenu():
  450. global runo
  451. global win
  452. letter = upload("CBHLetter",256,256)
  453. string1 = list("Our village has been haunted by The Sad Potato for yearsnow, it destroys our crops and kills us."+
  454. " Many have tried fighting it before but it's useless, only the chosen one can do it. Go on then chosen one, save the village.")
  455. if win:
  456.  
  457. string1 = list("You have done it !!! We are free at last.")
  458.  
  459. actualString1 = ""
  460. actualString2 = ""
  461. actualString3 = ""
  462. actualString4 = ""
  463. letterOpen = 0
  464. frame = 0
  465. cl = py.time.Clock()
  466. runo = 1
  467. while runo:
  468. screen.fill((0,0,0))
  469. mox, moy = py.mouse.get_pos()
  470. for e in py.event.get():
  471. if e.type == py.QUIT:
  472. runo = 0
  473. if e.type == py.MOUSEBUTTONDOWN:
  474. if e.button == 1:
  475. if abs(mox-600)<100 and abs(moy-400)<50:
  476. GameLoop()
  477. string1 = []
  478. if abs(mox-228)<128 and abs(moy-428)<128:
  479. letterOpen = 1
  480. frame+= 1
  481. if frame>20000000:
  482. frame = 0
  483.  
  484. if letterOpen and frame%8 == 0 and string1 != []:
  485. if font.render(actualString3,True,(255,255,255)).get_width()>670 :
  486. actualString4 = actualString4 + string1[0]
  487. elif font.render(actualString2,True,(255,255,255)).get_width()>670:
  488. actualString3 = actualString3 + string1[0]
  489. elif font.render(actualString1,True,(255,255,255)).get_width()>670:
  490. actualString2 = actualString2 + string1[0]
  491. else:
  492. actualString1 = actualString1 + string1[0]
  493.  
  494. del string1[0]
  495. if win and string1 == []:
  496. actualString1 = ""
  497. actualString2 = ""
  498. actualString3 = ""
  499. actualString4 = ""
  500. win = 0
  501. string1 = list("You have done it !!! We are free at last.")
  502. if letterOpen:
  503. txt = font.render(actualString4,True,(255,255,255))
  504. screen.blit(txt,(20,120))
  505. txt = font.render(actualString3,True,(255,255,255))
  506. screen.blit(txt,(12,100))
  507. txt = font.render(actualString2,True,(255,255,255))
  508. screen.blit(txt,(20,80))
  509. txt = font.render(actualString1,True,(255,255,255))
  510. screen.blit(txt,(20,60))
  511.  
  512.  
  513. screen.blit(letter,(100,300))
  514. py.draw.rect(screen,(255,255,255),(500,350,200,100))
  515. txt = bigfont.render("Play",True,(0,0,0))
  516. screen.blit(txt,(600-txt.get_width()/2,400-txt.get_height()/2))
  517. cl.tick(150)
  518. py.display.update()
  519. MainMenu()
  520.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement