jason31416

blockthem

May 6th, 2022 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 26.90 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4.  
  5. pygame.init()
  6. pygame.font.init()
  7. pygame.mixer.init()
  8.  
  9. sc = pygame.display.set_mode((800, 600))
  10.  
  11. lbird, lbird2 = pygame.image.load("src/loadbird.png"), pygame.image.load("src/loadbird2.png")
  12.  
  13. fontt = "pingfang"
  14.  
  15. font = pygame.font.SysFont(fontt, 24)
  16. tfont = pygame.font.SysFont(fontt, 72)
  17. sfont = pygame.font.SysFont(fontt, 18)
  18.  
  19. fl = open("src/userdata", "r")
  20. udata = fl.read().split("\n")
  21. fl.close()
  22.  
  23.  
  24. def loading():
  25.     sc.fill((0, 0, 0))
  26.     for i in range(100):
  27.         sc.blit(tfont.render("By Jason Chen", False, (0, 255, 255, i)), (170, 250))
  28.         pygame.display.update()
  29.         time.sleep(0.01)
  30.         sc.fill((0, 0, 0))
  31.         for ev in pygame.event.get():
  32.             if ev.type == pygame.QUIT:
  33.                 pygame.quit()
  34.                 exit(0)
  35.     time.sleep(0.5)
  36.     for i in range(100):
  37.         sc.fill((0, 255, 255))
  38.         sc.blit(tfont.render("Block them!", False, (0, 0, 0)), (180, 150))
  39.         pygame.draw.rect(sc, (0, 0, 255), (170, 290, i * 4, 20))
  40.         pygame.draw.rect(sc, (255, 255, 255), (170, 290, i * 4, 20), width=10)
  41.         if i % 16 <= 7:
  42.             sc.blit(lbird, (150 + i * 4, 240))
  43.         else:
  44.             sc.blit(lbird2, (150 + i * 4, 240))
  45.         sc.blit(font.render(f"加载中... {i}%", False, (0, 0, 0)), (340, 350))
  46.         pygame.display.update()
  47.         for ev in pygame.event.get():
  48.             if ev.type == pygame.QUIT:
  49.                 pygame.quit()
  50.                 exit(0)
  51.         time.sleep(0.015)
  52.     rc = pygame.surface.Surface((120, 45))
  53.     rc.fill((255, 255, 255))
  54.     ry = 600
  55.     diry = -10
  56.     st = 0
  57.     while True:
  58.         sc.fill((0, 255, 255))
  59.         sc.blit(tfont.render("Block them!", False, (0, 0, 0)), (180, 150))
  60.         pygame.draw.rect(sc, (0, 0, 255), (170, 290, 100 * 4, 20))
  61.         pygame.draw.rect(sc, (255, 255, 255), (170, 290, 100 * 4, 20), width=10)
  62.         sc.blit(lbird, (150 + 100 * 4, 240))
  63.         sc.blit(font.render(f"加载中... 100%", False, (0, 0, 0)), (340, 350))
  64.         sc.blit(rc, (360, ry))
  65.         sc.blit(font.render(f"开始游戏!", False, (0, 0, 0)), (370, ry + 5))
  66.         if rc.get_rect().move(360, ry).collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
  67.             return
  68.         ry += diry
  69.         if ry > 400 and diry == 1:
  70.             diry = -1
  71.         if ry < 390:
  72.             diry = 1
  73.         if st % 64 <= 31:
  74.             sc.blit(lbird, (150 + 400, 240))
  75.         else:
  76.             sc.blit(lbird2, (150 + 400, 240))
  77.         st += 1
  78.         pygame.display.update()
  79.         for ev in pygame.event.get():
  80.             if ev.type == pygame.QUIT:
  81.                 pygame.quit()
  82.                 exit(0)
  83.         time.sleep(0.01)
  84.  
  85.  
  86. worldwide, worldheight = 20, 15
  87.  
  88. world = [[0] * worldheight for i in range(worldwide)]
  89.  
  90. page = 0
  91.  
  92. blank = pygame.surface.Surface((39, 39))
  93. blank.fill((170, 255, 255))
  94.  
  95. nblank = pygame.surface.Surface((39, 39))
  96. nblank.fill((0, 0, 30))
  97.  
  98. pflag = pygame.image.load("src/pflag.png")
  99.  
  100. mine = pygame.surface.Surface((39, 39))
  101. mine.fill((0, 0, 0))
  102.  
  103. btower = pygame.transform.scale(pygame.image.load("src/bowtower.png"), (39, 39))
  104.  
  105. countdown = 60
  106.  
  107. coin = 50
  108.  
  109. tick = 0
  110. ticktime = 0
  111.  
  112. loading()
  113. # fixme: loading
  114.  
  115. level = -1
  116.  
  117. levpos = [(10, 275), (80, 275), (150, 275), (220, 275), (290, 275), (360, 275), (430, 275), (500, 275), (150, 175),
  118.           (220, 175), (290, 175), (360, 175), (430, 175), (500, 175), (570, 175), (570, 275), (640, 175)] # now end on BOSS1
  119.  
  120. levname = ["1", "2", "3", "4", "5", "6", "7", "8", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "B1", "B2", "B3", "B4",
  121.            "C1", "D1", "D2", "D3", "D4", "D5"]
  122.  
  123. """
  124. levels:
  125. normal:
  126. 0. 1
  127. 1. 2
  128. 2. 3
  129. 3. 4
  130. 4. 5
  131. 5. 6
  132. 6. 7
  133. 7. 8
  134. night:
  135. 8. A1 unlock by 3
  136. 9. A2
  137. 10. A3
  138. 11. A4
  139. 12. A5
  140. 13. A6
  141. 14. A7
  142. Bosses:
  143. 15. BOSS1 unlock by 8
  144. 16. BOSS2 unlock by A7
  145. """
  146.  
  147. lines = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (2, 8), (8, 9), (9, 10), (10, 11), (11, 12), (12, 13), (13, 14), (7, 15), (14, 16)]
  148.  
  149. progress = [[1], [2], [3, 8], [4], [5], [6], [7], [15], [9], [10], [11], [12], [13], [14], [16], [], []]
  150.  
  151. unlockedt = udata[2].split(",")
  152.  
  153. night_levels = [8, 9, 10, 11, 12, 13, 14]
  154.  
  155. innpage = 0
  156.  
  157. # (Name, unlock level, hp, cost, surface)
  158.  
  159. towers = [("矿洞", 0, 50, 50, mine), ("弓箭塔", 0, 50, 100, btower), ("炸药", 1, 50, 175, pygame.image.load("src/TNT.png")),
  160.           ("砖头", 1, 150, 100, pygame.transform.scale(pygame.image.load("src/brick.png"), (39, 39))),
  161.           ("信标", 2, 20, 225, pygame.transform.scale(pygame.image.load("src/xb.png"), (39, 39))),
  162.           ("火把", 2, 10, 50, pygame.transform.scale(pygame.image.load("src/torch.png"), (39, 39))),
  163.           ("铁傀儡", 6, 100, 200, pygame.transform.scale(pygame.image.load("src/iron_golem.png"), (39, 39))),
  164.           ("高压炸药", 15, 100, 250, pygame.transform.scale(pygame.image.load("src/tntp.png"), (39, 39)))]
  165. thingsonhand = -1
  166.  
  167. tntnoharm = [6]
  168.  
  169. monster = [[0] * worldheight for i in range(worldwide)]
  170.  
  171. hp = [[0] * worldheight for i in range(worldwide)]
  172.  
  173. # (hp, att, spd, surface)
  174. monsters = [(), (30, 5, 34, pygame.transform.scale(pygame.image.load("src/zombie.png"), (29, 29))),
  175.             (20, 10, 56, pygame.transform.scale(pygame.image.load("src/creeper.png"), (29, 29))),
  176.             (61, 6, 66, pygame.transform.scale(pygame.image.load("src/pilliger.png"), (39, 39))),
  177.             (60, 0, 10000000, pygame.transform.scale(pygame.image.load("src/enderman.png"), (29, 29))),
  178.             (181, 5, 120, pygame.transform.scale(pygame.image.load("src/slime.png"), (29, 29))),
  179.             (60, 3, 66, pygame.transform.scale(pygame.image.load("src/blaze.png"), (29, 29)))]
  180.  
  181. lx = levpos[int(udata[1])][0] - 10
  182. mxcoin = int(udata[4])
  183.  
  184. retbut = pygame.surface.Surface((120, 45))
  185. retbut.fill((255, 255, 255))
  186. retbut.blit(font.render("返回", False, (0, 0, 0)), (30, 5))
  187.  
  188. levelmonst = [{30: [0, 0, 1], 45: [0, 5, 1], 46: [0, 8, 1], 56: [0, 9, 1], 57: [0, 14, 1], 58: [0, 0, 1], 60: -1},  # 1
  189.               {20: [0, 0, 1], 21: [0, 14, 1], 40: [0, 1, 1], 41: [0, 13, 1], 57: [5, 0, 2], 58: [5, 14, 1],
  190.                59: [5, 1, 1], 60: [5, 13, 1], 61: -1},  # 2
  191.               {15: [0, 7, 1], 25: [0, 7, 2], 26: [0, 8, 1], 38: [0, 0, 2], 39: [0, 14, 2], 40: [0, 14, 1],
  192.                41: [0, 0, 1], 53: [0, 7, 1], 54: [10, 7, 2], 55: [10, 8, 1], 56: [10, 6, 1], 57: -1}, # 3
  193.               {15: [0, 7, 1], 30: [0, 7, 2], 50: [0, 7, 3], 51: [0, 0, 2], 70: [0, 14, 2], 90: [0, 8, 2], 91: -1}, # 4
  194.               {15: [0, 7, 2], 30: [1, 7, 2], 50: [0, 7, 3], 70: [0, 0, 3], 71: [1, 14, 3], 72: [2, 7, 3], 73: -1}, # 5
  195.               {15: [0, 14, 1], 25: [0, 0, 1], 40: [0, 9, 1], 50: [0, 0, 1], 49: [0, 0, 3], 51: [0, 7, 3], 70: [0, 5, 3], 71: -1}, # 6
  196.               {15: [0, 7, 2], 45: [0, 9, 1], 60: [0, 7, 3], 61: [0, 8, 3], 62: [0, 14, 3], 63: [0, 0, 3], 64: [0, 1, 3], 65: -1}, # 7
  197.               {15: [0, 7, 1], 30: [0, 7, 2], 45: [0, 7, 3], 40: [0, 7, 4], 50: [0, 7, 2], 51: [1, 8, 2], 52: [2, 9, 2], 53: [3, 6, 2], 54: [4, 5, 2], 65: [0, 0, 4], 66: [0, 14, 4], 67: [0, 8, 4], 68: [0, 7, 3], 69: [0, 0, 3], 70: [0, 14, 3], 71: -1}, # 8
  198.               {20: [0, 7, 1], 30: [0, 5, 1], 40: [0, 8, 2], 41: [0, 0, 2], 42: [0, 14, 2], 43: [0, 7, 1], 44: -1}, # A1
  199.               {15: [0, 8, 1], 20: [0, 2, 1], 30: [0, 12, 2], 35: [0, 1, 2], 50: [0, 7, 1], 70: [0, 7, 3], 71: -1}, # A2
  200.               {15: [0, 8, 1], 30: [0, 7, 2], 40: [0, 7, 4], 60: [0, 1, 3], 81: [0, 13, 3], 95: [0, 7, 3], 96: [2, 7, 2], 97: -1}, # A3
  201.               {15: [0, 8, 1], 30: [0, 1, 2], 31: [0, 14, 2], 50: [0, 1, 1], 51: [0, 2, 1], 52: [0, 3, 1], 53: [0, 4, 1], 70: [0, 5, 1], 71: [0, 6, 1], 72: [0, 7, 1], 73: [0, 8, 1], 130: [0, 8, 3], 131: [0, 1, 3], 132: [0, 14, 3], 133: [0, 5, 5], 134: [0, 10, 5], 135: -1}, # A4
  202.               {15: [0, 8, 5], 30: [0, 8, 5], 45: [0, 8, 5], 46: [0, 8, 3], 60: [0, 1, 5], 61: [0, 14, 5], 62: [0, 1, 3], 63: [0, 4, 3], 64: [0, 4, 5], 70: [0, 3, 5], 71: [0, 11, 5], 72: [0, 3, 4], 73: [0, 11, 4], 100: [0, 11, 3], 101: [0, 4, 4], 102: [0, 8, 5], 103: -1}, # A5
  203.               {15: [0, 8, 5], 30: [0, 8, 5], 45: [0, 8, 6], 46: [0, 1, 6], 47: [0, 14, 6], 48: [0, 8, 3], 60: [0, 3, 5], 61: [0, 14, 3], 75: [0, 2, 4], 76: [0, 13, 6], 77: [0, 12, 6], 78: [0, 2, 6], 79: [0, 3, 6], 80: [0, 8, 4], 100: [0, 8, 5], 101: [0, 8, 2], 102: [0, 1, 3], 103: [0, 13, 3], 104: [0, 3, 3], 105: [0, 8, 6], 106: -1}, # A6
  204.               {},  # A7
  205.               {20: [0, 8, 5], 40: [0, 9, 5], 41: [0, 2, 5], 42: [0, 7, 5], 60: [0, 5, 4], 61: [0, 8, 5], 80: [0, 2, 4], 81: [0, 13, 4], 150: [0, 1, 3], 151: [0, 8, 3], 152: [0, 14, 3], 153: [0, 4, 3], 154: [0, 11, 3], 155: [0, 8, 5], 156: [0, 8, 2], 157: [0, 8, 5], 158: [0, 8, 2], 159: [0, 8, 4], 160: [0, 8, 5], 161: [0, 8, 5], 162: [0, 8, 3], 170: -1},  # BOSS1
  206.               {} # BOSS2
  207.               ] # todo: levels
  208. winning = False
  209.  
  210. unlockedl = [int(i) for i in udata[3].split(",")]
  211.  
  212. while True:
  213.     if page == 1:
  214.         if level not in night_levels:
  215.             sc.fill((0, 255, 255))
  216.         else:
  217.             sc.fill((0, 0, 30))
  218.     else:
  219.         sc.fill((0, 255, 255))
  220.     ticktime = time.time()
  221.     if page == 0:
  222.         sc.blit(font.render(udata[0], False, (0, 0, 0)), (10, 10))
  223.         fut = []
  224.         for i in unlockedl:
  225.             for j in progress[i]:
  226.                 if j not in unlockedl and j not in fut:
  227.                     fut.append(j)
  228.         for i in lines:
  229.             if i[0] in unlockedl and i[1] in unlockedl:
  230.                 pygame.draw.line(sc, (0, 0, 0), (levpos[i[0]][0] + lx + 25, levpos[i[0]][1] + 25),
  231.                                  (levpos[i[1]][0] + lx + 25, levpos[i[1]][1] + 25))
  232.             elif i[0] in unlockedl and i[1] in fut:
  233.                 pygame.draw.line(sc, (170, 170, 170), (levpos[i[0]][0] + lx + 25, levpos[i[0]][1] + 25),
  234.                                  (levpos[i[1]][0] + lx + 25, levpos[i[1]][1] + 25))
  235.             elif i[0] in fut and i[1] in unlockedl:
  236.                 pygame.draw.line(sc, (170, 170, 170), (levpos[i[0]][0] + lx + 25, levpos[i[0]][1] + 25),
  237.                                  (levpos[i[1]][0] + lx + 25, levpos[i[1]][1] + 25))
  238.         for i in unlockedl:
  239.             pygame.draw.rect(sc, (255, 255, 255), (levpos[i][0] + lx, levpos[i][1], 50, 50))
  240.             sc.blit(font.render(levname[i], False, (0, 0, 0)), (levpos[i][0] + 15 + lx, levpos[i][1] + 10))
  241.             if pygame.rect.Rect(levpos[i][0] + lx, levpos[i][1], 50, 50).collidepoint(pygame.mouse.get_pos()) and \
  242.                     pygame.mouse.get_pressed()[0]:
  243.                 level = i
  244.                 world = [[0] * worldheight for i in range(worldwide)]
  245.                 monster = [[[0, 0, 0] for j in range(worldheight)] for i in range(worldwide)]
  246.                 world[worldwide - 1][worldheight // 2] = -1
  247.                 hp[worldwide - 1][worldheight // 2] = 10000000
  248.                 page = 1
  249.                 tick = 0
  250.                 countdown = 0
  251.                 coin = 50
  252.                 winning = False
  253.         for i in fut:
  254.             pygame.draw.rect(sc, (170, 170, 170), (levpos[i][0] + lx, levpos[i][1], 50, 50))
  255.             sc.blit(font.render(levname[i], False, (0, 0, 0)), (levpos[i][0] + 15 + lx, levpos[i][1] + 10))
  256.         keys = pygame.key.get_pressed()
  257.         if keys[pygame.K_RIGHT] and lx >= 0:
  258.             lx -= 5
  259.         if keys[pygame.K_LEFT] and lx < 1000:
  260.             lx += 5
  261.     elif page == 1:
  262.         tick += 1
  263.         stillmob = True
  264.         if coin > mxcoin:
  265.             coin = mxcoin
  266.         for i in range(worldwide):
  267.             for j in range(worldheight):
  268.                 if world[i][j] == 0:
  269.                     if level not in night_levels:
  270.                         sc.blit(blank, (i * 40, j * 40))
  271.                 elif world[i][j] == -1:
  272.                     sc.blit(pflag, (i * 40, j * 40))
  273.                 elif world[i][j] > 0:
  274.                     if night_levels:
  275.                         sc.blit(blank, (i * 40, j * 40))
  276.                     sc.blit(towers[world[i][j] - 1][4], (i * 40, j * 40))
  277.                     pygame.draw.rect(sc, (0, 255, 0), (i * 40, j * 40 + 30, int(hp[i][j]/towers[world[i][j]-1][2]*39), 8))
  278.                     if world[i][j] == 1 and tick % 166 == 0:
  279.                         coin += 25
  280.                     elif world[i][j] == 2 and tick % 33 == 0:
  281.                         shooted = False
  282.                         for k in range(max(0, i - 2), min(worldwide, i + 2)):
  283.                             for l in range(max(0, j - 2), min(worldheight, j + 2)):
  284.                                 if monster[k][l][0] != 0:
  285.                                     monster[k][l][1] -= 2
  286.                                     if monster[k][l][1] <= 0:
  287.                                         monster[k][l] = [0, 0, 0]
  288.                                     pygame.draw.line(sc, (0, 0, 0), (i * 40, j * 40), (k * 40, l * 40), width=10)
  289.                                     shooted = True
  290.                                     break
  291.                             if shooted:
  292.                                 break
  293.                     elif world[i][j] == 3:
  294.                         shooted = False
  295.                         for k in range(max(0, i - 2), min(worldwide, i + 2)):
  296.                             for l in range(max(0, j - 2), min(worldheight, j + 2)):
  297.                                 if monster[k][l][0] != 0 and monster[k][l][1] not in tntnoharm:
  298.                                     shooted = True
  299.                                     break
  300.                             if shooted:
  301.                                 break
  302.                         if shooted:
  303.                             for k in range(max(0, i - 2), min(worldwide, i + 3)):
  304.                                 for l in range(max(0, j - 2), min(worldheight, j + 3)):
  305.                                     if monster[k][l][0] != 0 and monster[k][l][1] not in tntnoharm:
  306.                                         monster[k][l][1] -= 40
  307.                                         if monster[k][l][1] <= 0:
  308.                                             monster[k][l] = [0, 0, 0]
  309.                             world[i][j] = 0
  310.                             hp[i][j] = -1
  311.                     elif world[i][j] == 5 and tick%33 == 0:
  312.                         for k in range(max(0, i - 2), min(worldwide, i + 3)):
  313.                             for l in range(max(0, j - 2), min(worldheight, j + 3)):
  314.                                 if world[k][l] > 0 and (k != i or l != j) and hp[k][l]+3 <= towers[world[k][l]-1][2]:
  315.                                     hp[k][l] += 3
  316.                                     pygame.draw.line(sc, (0, 255, 0), (i * 40, j * 40), (k * 40, l * 40), width=10)
  317.                     elif world[i][j] == 6 and 8 <= level <= 15:
  318.                         for k in range(max(0, i - 3), min(worldwide, i + 4)):
  319.                             for l in range(max(0, j - 3), min(worldheight, j + 4)):
  320.                                 if world[k][l] == 0:
  321.                                     sc.blit(blank, (k * 40, l * 40))
  322.                                 if monster[k][l][0] != 0:
  323.                                     sc.blit(monsters[monster[k][l][0]][3], (k * 40, l * 40))
  324.                     elif world[i][j] == 7 and tick%66 == 0:
  325.                         shooted = False
  326.                         for k in range(max(0, i - 1), min(worldwide, i + 2)):
  327.                             for l in range(max(0, j - 1), min(worldheight, j + 2)):
  328.                                 if monster[k][l][0] != 0:
  329.                                     monster[k][l][1] -= 20
  330.                                     if monster[k][l][1] <= 0:
  331.                                         monster[k][l] = [0, 0, 0]
  332.                                     pygame.draw.line(sc, (0, 0, 0), (i * 40, j * 40), (k * 40, l * 40), width=10)
  333.                                     shooted = True
  334.                                     break
  335.                             if shooted:
  336.                                 break
  337.                     elif world[i][j] == 8 and tick%5 == 0:
  338.                         if 50 >= hp[i][j] > 0:
  339.                             shooted = False
  340.                             for k in range(max(0, i - 3), min(worldwide, i + 4)):
  341.                                 for l in range(max(0, j - 3), min(worldheight, j + 4)):
  342.                                     if monster[k][l][0] != 0 and monster[k][l][1] not in tntnoharm:
  343.                                         shooted = True
  344.                                         break
  345.                                 if shooted:
  346.                                     break
  347.                             if shooted:
  348.                                 for k in range(max(0, i - 4), min(worldwide, i + 5)):
  349.                                     for l in range(max(0, j - 4), min(worldheight, j + 5)):
  350.                                         if monster[k][l][0] != 0 and monster[k][l][1] not in tntnoharm:
  351.                                             monster[k][l][1] -= 130
  352.                                             if monster[k][l][1] <= 0:
  353.                                                 monster[k][l] = [0, 0, 0]
  354.                                 world[i][j] = 0
  355.                                 hp[i][j] = -1
  356.                         elif hp[i][j] > 50:
  357.                             hp[i][j] -= 1
  358.                     # todo: towers
  359.                 if monster[i][j][0] != 0:
  360.                     stillmob = False
  361.                     if level not in night_levels:
  362.                         sc.blit(monsters[monster[i][j][0]][3], (i * 40, j * 40))
  363.                     if monster[i][j][0] == 2 and tick % 33 == 0:
  364.                         for k in range(max(0, i - 3), min(worldwide, i + 4)):
  365.                             for l in range(max(0, j - 3), min(worldheight, j + 4)):
  366.                                 if world[k][l] > 0:
  367.                                     hp[k][l] -= 5
  368.                                     if hp[k][l] < 0:
  369.                                         world[k][l] = 0
  370.                     elif monster[i][j][0] == 3 and tick % 100 == 0:
  371.                         ak, al = -1, -1
  372.                         for k in range(max(0, i - 3), min(worldwide, i + 4)):
  373.                             for l in range(max(0, j - 3), min(worldheight, j + 4)):
  374.                                 if monster[k][l][0] == 0:
  375.                                     if ak == -1 or random.randint(0, 2) == 0:
  376.                                         ak, al = k, l
  377.                         if ak != -1:
  378.                             mtype = random.choice([1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 5, 5, 2, 3, 4])
  379.                             monster[ak][al] = [mtype, monsters[mtype][0], 0]
  380.                     elif monster[i][j][0] == 4 and tick % 33 == 0 and monster[i][j][2] != tick:
  381.                         shooted = False
  382.                         for k in range(max(0, i - 2), min(worldwide, i + 3)):
  383.                             for l in range(max(0, j - 2), min(worldheight, j + 3)):
  384.                                 if world[k][l] != 0:
  385.                                     hp[k][l] -= 10
  386.                                     if hp[k][l] <= 0:
  387.                                         world[k][l] = 0
  388.                         monster[i][j][2] = tick
  389.                         if random.randint(0, 3) == 0:
  390.                             rx, ry = random.randint(0, worldwide-2), random.randint(0, worldheight-1)
  391.                             rdcnt = 0
  392.                             while monster[rx][ry][0] != 0 or world[rx][ry] != 0:
  393.                                 rx, ry = random.randint(0, worldwide - 2), random.randint(0, worldheight - 1)
  394.                                 rdcnt += 1
  395.                                 if rdcnt >= 100:
  396.                                     break
  397.                             if rdcnt < 60:
  398.                                 monster[rx][ry] = [4, monster[i][j][1], tick]
  399.                                 monster[i][j] = [0, 0, 0]
  400.                                 continue
  401.                     elif monster[i][j][0] == 6 and tick % 33 == 0:
  402.                         shooted = False
  403.                         for k in range(i, i+15):
  404.                             if k >= worldwide:
  405.                                 break
  406.                             if world[k][j] != 0:
  407.                                 hp[k][j] -= 5
  408.                                 pygame.draw.line(sc, (231, 101, 26), (i * 40, j * 40), (k * 40, j * 40), width=10)
  409.                                 if hp[k][j] <= 0:
  410.                                     world[k][j] = 0
  411.                                 break
  412.                     if tick % monsters[monster[i][j][0]][2] == 0 and monster[i][j][2] != tick:
  413.                         if i < worldwide - 1:
  414.                             if monster[i + 1][j] == [0, 0, 0]:
  415.                                 if world[i + 1][j] <= 0:
  416.                                     monster[i + 1][j] = [monster[i][j][0], monster[i][j][1], tick]
  417.                                     monster[i][j] = [0, 0, 0]
  418.                                 else:
  419.                                     hp[i + 1][j] -= monsters[monster[i][j][0]][1]
  420.                                     if hp[i + 1][j] <= 0:
  421.                                         world[i + 1][j] = 0
  422.                             elif j + 1 < worldheight and monster[i][j + 1] == [0, 0]:
  423.                                 if world[i][j + 1] <= 0:
  424.                                     monster[i][j + 1] = [monster[i][j][0], monster[i][j][1], tick]
  425.                                     monster[i][j] = [0, 0, 0]
  426.                                 else:
  427.                                     hp[i][j + 1] -= monsters[monster[i][j][0]][1]
  428.                                     if hp[i][j + 1] <= 0:
  429.                                         world[i][j + 1] = 0
  430.                             elif j - 1 >= 0 and monster[i][j - 1] == [0, 0]:
  431.                                 if world[i][j - 1] <= 0:
  432.                                     monster[i][j - 1] = [monster[i][j][0], monster[i][j][1], tick]
  433.                                     monster[i][j] = [0, 0, 0]
  434.                                 else:
  435.                                     hp[i][j - 1] -= monsters[monster[i][j][0]][1]
  436.                                     if hp[i][j - 1] <= 0:
  437.                                         world[i][j - 1] = 0
  438.                         else:
  439.                             if j > 7:
  440.                                 if monster[i][j - 1] == [0, 0, 0]:
  441.                                     if world[i][j - 1] <= 0:
  442.                                         monster[i][j - 1] = [monster[i][j][0], monster[i][j][1], tick]
  443.                                         monster[i][j] = [0, 0, 0]
  444.                                     else:
  445.                                         hp[i][j - 1] -= monsters[monster[i][j][0]][1]
  446.                                         if hp[i][j - 1] <= 0:
  447.                                             world[i][j - 1] = 0
  448.                             elif j < 7:
  449.                                 if monster[i][j + 1] == [0, 0, 0]:
  450.                                     if world[i][j + 1] <= 0:
  451.                                         monster[i][j + 1] = [monster[i][j][0], monster[i][j][1], tick]
  452.                                         monster[i][j] = [0, 0, 0]
  453.                                     else:
  454.                                         hp[i][j + 1] -= monsters[monster[i][j][0]][1]
  455.                                         if hp[i][j + 1] <= 0:
  456.                                             world[i][j + 1] = 0
  457.                             else:
  458.                                 page = 3
  459.                 # todo: monster
  460.         if stillmob and winning:
  461.             page = 2
  462.             udata[1] = str(max(int(udata[1]), level + 1))
  463.             for i in range(len(towers)):
  464.                 if str(i) not in unlockedt and towers[i][1] == level:
  465.                     unlockedt.append(str(i))
  466.             for i in progress[level]:
  467.                 if i not in unlockedl:
  468.                     unlockedl.append(i)
  469.             udata[2] = ",".join(unlockedt)
  470.             udata[3] = ",".join([str(i) for i in unlockedl])
  471.             fl = open("src/userdata", "w+")
  472.             fl.write("\n".join(udata))
  473.             fl.close()
  474.             continue
  475.         if tick % 33 == 0:
  476.             countdown += 1
  477.             if countdown in levelmonst[level].keys():
  478.                 if levelmonst[level][countdown] == -1:
  479.                     winning = True
  480.                 else:
  481.                     monster[levelmonst[level][countdown][0]][levelmonst[level][countdown][1]] = [
  482.                         levelmonst[level][countdown][2], monsters[levelmonst[level][countdown][2]][0], 0]
  483.         if innpage == 1:
  484.             sc.fill((170, 170, 170))
  485.             rx = 50
  486.             for i in range(len(towers)):
  487.                 if str(i) in unlockedt:
  488.                     if thingsonhand == i + 1:
  489.                         pygame.draw.rect(sc, (200, 200, 200), (rx % 750, 110 + rx // 750 * 70, 49, 49))
  490.                     sc.blit(sfont.render(f"{towers[i][0]} {towers[i][3]}$", False, (0, 0, 0)),
  491.                             (rx % 750, 151 + rx // 750 * 70))
  492.                     sc.blit(towers[i][4], (rx % 750 + 5, 110 + rx // 750 * 70 + 5))
  493.                     if towers[i][4].get_rect().move(rx % 750 + 5, 110 + rx // 750 * 70 + 5).collidepoint(
  494.                             pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
  495.                         thingsonhand = i + 1
  496.                     rx += 120
  497.         else:
  498.             if pygame.mouse.get_pressed()[2]:
  499.                 mx, my = pygame.mouse.get_pos()
  500.                 bx, by = mx // 40, my // 40
  501.                 if coin >= towers[thingsonhand - 1][3] and world[bx][by] == 0:
  502.                     world[bx][by] = thingsonhand
  503.                     coin -= towers[thingsonhand - 1][3]
  504.                     hp[bx][by] = towers[thingsonhand - 1][2]
  505.                     time.sleep(0.1)
  506.         keys = pygame.key.get_pressed()
  507.         if keys[pygame.K_e]:
  508.             innpage = 1 if innpage == 0 else 0
  509.             time.sleep(0.3)
  510.         sc.blit(sfont.render(f"你有{coin}$/背包容量{mxcoin}$", False, (255, 155, 0)), (5, 5))
  511.         sc.blit(sfont.render(f"现在是第{countdown}刻", False, (255, 155, 0)), (5, 25))
  512.     elif page == 2:
  513.         sc.blit(font.render(f"你成功的打败了所有第{levname[level]}关的怪物!", False, (0, 0, 0)), (150, 150))
  514.         sc.blit(retbut, (200, 300))
  515.         if retbut.get_rect().move(200, 300).collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
  516.             page = 0
  517.             time.sleep(0.2)
  518.     elif page == 3:
  519.         sc.blit(font.render(f"怪物摧毁了你的家园!", False, (0, 0, 0)), (150, 150))
  520.         sc.blit(retbut, (200, 300))
  521.         if retbut.get_rect().move(200, 300).collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
  522.             page = 0
  523.             time.sleep(0.2)
  524.     for ev in pygame.event.get():
  525.         if ev.type == pygame.QUIT:
  526.             pygame.quit()
  527.             exit(0)
  528.     pygame.display.update()
  529.     while time.time() <= ticktime + 0.03:
  530.         time.sleep(0.003)
  531.  
Add Comment
Please, Sign In to add comment