Abdullahafzalkhan

Spacers

Feb 19th, 2022
1,335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.87 KB | None | 0 0
  1. from ursina import *
  2. from random import *
  3.  
  4. bullets = []
  5. bullets2 = []
  6. bullets3 = []
  7. invaders = []
  8. hit_info1 = None
  9. MenuButtons = []
  10. Levels = []
  11. Powers = []
  12. InvaderAmount = 10
  13. Level = 1
  14. score = 0
  15. TotalScore = 0
  16. ScoreNeeded = 50
  17. invaderSpeed = 0.2
  18. speed = 4
  19. shootSpeed = 2
  20. time1 = 0
  21. count = 20
  22. move = False
  23. doubleP = True  
  24. h = True
  25. c = True
  26. first = True
  27. try:
  28.     os.mkdir(os.getcwd() + "/Memory")
  29.     file = open("Memory/data.txt", "w+")
  30.     file.close()
  31. except:
  32.     pass
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. def Wait():
  40.     pass
  41.  
  42. def WriteData():
  43.     global invaders, bullets,score,text, move, InvaderAmount, Level, TotalScore, doubleP
  44.     file = open("Memory/data.txt", "w+")
  45.     file.truncate()
  46.     file.write(
  47. f"""
  48. Level={Level},
  49. TotalScore={TotalScore},
  50. """
  51. )
  52.     file.close()
  53.     application.quit()
  54. def ReadData():
  55.     try:
  56.         global invaders, bullets,score,text, move, InvaderAmount, Level, TotalScore,ScoreNeeded, h
  57.         file = open("Memory/data.txt", "r")
  58.         text = file.read()
  59.         file.close()
  60.         Level = int(text.split(",")[0].split("=")[1])
  61.         print(Level)
  62.         TotalScore = int(text.split(",")[1].split("=")[1])
  63.         if Level == 1:
  64.             InvaderAmount = 10
  65.         elif Level == 2:
  66.             InvaderAmount = 12
  67.         elif Level >= 3:
  68.             InvaderAmount = 15
  69.         if Level == 1:
  70.             speed = 4
  71.         elif Level >= 2:
  72.             speed = 4
  73.         if Level == 1:
  74.             invaderSpeed = 0.8
  75.         if level >= 2:
  76.             invaderSpeed = 1.2
  77.         if Level >= 3:
  78.             ScoreNeeded = 100
  79.     except:
  80.         pass
  81.  
  82. def ChangeLevel(n):
  83.     global Level
  84.     Level = n
  85.  
  86. def Invasion(start):
  87.     global invaders, move
  88.     if start == True:
  89.         move = True
  90.     for invader in invaders:
  91.         invader.enabled = False
  92.         invaders.remove(invader)
  93.  
  94.     for i in range(InvaderAmount):
  95.         invader = Invader()
  96.         invaders.append(invader)
  97.  
  98. def Enable(*en):
  99.     for i in en:
  100.         i.enabled = True
  101.  
  102. def Enable1(eg, **k):
  103.     for i in eg:
  104.         i.enabled = True
  105.     for i in k:
  106.         i.enabled = True
  107.  
  108. def Close():
  109.     for i in Levels:
  110.         i.enabled = False
  111.     MenuButton.enabled = False
  112.     CloseButton.enabled = False
  113.     RestartButton.enabled = False
  114.     LevelButton.enabled = False
  115. def StartB():
  116.     global move, h
  117.     move = True
  118.     StartButton.enabled = False
  119.     h = True
  120.  
  121. def Pause():
  122.     global move
  123.     if move:
  124.         move=False
  125.     else:
  126.         move=True
  127.  
  128. def update():
  129.     global invaders, bullets,score,text, move, InvaderAmount, Level, TotalScore
  130.     global ScoreNeeded, shootSpeed, doubleP, h, count
  131.     if move:
  132.         player.x += held_keys['right arrow'] * time.dt * speed
  133.         player.x -= held_keys['left arrow'] * time.dt * speed
  134.  
  135.     if h:
  136.         if score >= ScoreNeeded:
  137.             Level += 1
  138.             ChangeLevel(Level)
  139.             move = False
  140.             TotalScore += score
  141.             WText = Text(text='You Win!',origin=(0,0),scale=2,color=color.gray,background=True)
  142.             MenuButton.enabled =  True
  143.             LevelButton.enabled = True
  144.             RestartButton.enabled = True
  145.             CloseButton.enabled = True
  146.             h = False
  147.  
  148.     for power in Powers:
  149.         if move:
  150.             power.y -= time.dt * 2
  151.             hit_info = power.intersects()
  152.             if hit_info.entity ==  player:
  153.                 doubleP = True
  154.     for invader in invaders:
  155.         if move:
  156.             invader.y -= time.dt*invaderSpeed
  157.         if invader.y < -4:
  158.             b = Entity(model='quad', scale=60, color=color.gray, collider='box')
  159.             b.z = -0.01
  160.             player.y = 10
  161.             move = False
  162.             Text(text='You lost! Reload the game!',origin=(0,0),scale=2,color=color.yellow,background=True)
  163.             MenuButton.enabled = True
  164.  
  165.     for power in Powers:
  166.         info = power.intersects()
  167.         if info.hit:
  168.             if info.entity == player:
  169.                 doubleP = True
  170.  
  171.     for bullet in bullets3:
  172.         if move:
  173.             bullet.y += 0.25
  174.             hit_info1 = bullet.intersects()
  175.             if hit_info1.hit:              
  176.                 if hit_info1.entity in invaders:
  177.                     rand = randint(1,10)
  178.                     if rand == 1:
  179.                         powerup = PowerUp()
  180.                         Powers.append(powerup)
  181.                     del(bullet)
  182.                     score += 1
  183.                     TotalScore += 1
  184.                     text.y = 10
  185.                     text=Text(text=f"Score: {score}",position=(-0.65,0.4),origin=(0,0),scale=2,color=color.yellow,background=True)
  186.                
  187.                 Audio('assets/explosion_sound.wav')
  188.                 if hit_info1.entity in invaders:
  189.                     hit_info1.entity.x = randint(-500,500)*0.01
  190.                     hit_info1.entity.y = randint(80,400)*0.01
  191.    
  192.    
  193.     for bullet in bullets2:
  194.         if move:
  195.             bullet.y += 0.25
  196.             hit_info1 = bullet.intersects()
  197.             if hit_info1.hit:
  198.                 rand = randint(1,10)  
  199.                 if hit_info1.entity in invaders:
  200.                     if rand == 1:
  201.                         powerup = PowerUp()
  202.                         Powers.append(powerup)  
  203.                     del(bullet)
  204.                     score += 1
  205.                     TotalScore += 1
  206.                     text.y = 10
  207.                     count += 1
  208.                     text=Text(text=f"Score: {score}",position=(-0.65,0.4),origin=(0,0),scale=2,color=color.yellow,background=True)
  209.  
  210.                 Audio('assets/explosion_sound.wav')
  211.                 if hit_info1.entity in invaders:
  212.                     hit_info1.entity.x = randint(-500,500)*0.01
  213.                     hit_info1.entity.y = randint(80,400)*0.01
  214.  
  215.     for bullet in bullets:
  216.         if move:
  217.             bullet.y += 0.25
  218.             hit_info = bullet.intersects()
  219.             if hit_info.hit:
  220.                 if hit_info.entity in invaders:
  221.  
  222.                     rand = randint(1,10)
  223.                     if rand == 1:
  224.                         powerup = PowerUp()
  225.                         Powers.append(powerup)                
  226.                     Audio('assets/explosion_sound.wav')
  227.                     del(bullet)
  228.                     score += 1
  229.                     TotalScore += 1
  230.                     text.y = 10
  231.                     text=Text(text=f"Score: {score}",position=(-0.65,0.4),origin=(0,0),scale=2,color=color.yellow,background=True)
  232.  
  233.                 if hit_info.entity in invaders:
  234.                     hit_info.entity.x = randint(-500,500)*0.01
  235.                     hit_info.entity.y = randint(80,400)*0.01
  236.  
  237. class PowerUp(Entity):
  238.     def __init__(self):
  239.         super().__init__()
  240.         self.model='cube'
  241.         self.texture='fist.png'
  242.         self.scale = 0.6
  243.         self.position = (randint(-500,500)*0.01,randint(80,400)*0.01,-0.1)
  244.         self.collider = 'box'
  245.         self.power = "double"
  246.  
  247. class Invader(Entity):
  248.     def __init__(self):
  249.         super().__init__()
  250.         self.model='quad'
  251.         self.texture='alien.png'
  252.         self.scale = 0.6
  253.         self.position = (randint(-500,500)*0.01,randint(80,400)*0.01,-0.1)
  254.         self.collider = 'box'
  255.         self.dy = -0.15
  256.  
  257. class Player(Entity):
  258.     def __init__(self):
  259.         super().__init__()
  260.         self.model = 'cube'
  261.         self.color = color.white
  262.         self.scale = (0.75,1.5,0)
  263.         self.position = (0,-3.5,-0.1)
  264.         self.texture = 'alien.png'
  265.         self.collider = "box"
  266.         self.dx = 0.5
  267.  
  268. class Bullet(Entity):
  269.     def __init__(self, x, y):
  270.         super().__init__()
  271.         self.model ="cube"
  272.         self.color = color.orange
  273.         self.scale=(y)
  274.         self.position = Vec3(x, player.y + 1,player.z)
  275.         self.collider = 'box'
  276.         self.dy = 0.8
  277. class Bullet2(Entity):
  278.     def __init__(self, x, y):
  279.         super().__init__()
  280.         self.model ="cube"
  281.         self.color = color.orange
  282.         self.scale=(y)
  283.         self.position = Vec3(x + 0.5, player.y + 1,player.z)
  284.         self.collider = 'box'
  285.         self.dy = 0.8
  286.  
  287. def input(key):
  288.     global bullets, doubleP, time1
  289.     if move:
  290.         if key == "space":
  291.             if doubleP:
  292.                 time1 += 1
  293.                 #if time1 >= 10:
  294.                     #doubleP = False
  295.                     #time1 = 0
  296.                     #print("false")
  297.                 bullet = Bullet(player.x,(0.25,0.5,0))
  298.                 bullets.append(bullet)
  299.                 bullet2 = Bullet2(player.x + 0.25/2,(0.25,0.5,0))
  300.                 bullets2.append(bullet2)
  301.                 bullet3 = Bullet2(player.x - 1,(0.25,0.5,0))
  302.                 bullets3.append(bullet3)
  303.             else:
  304.                 bullet = Bullet(player.x,(0.25,0.5,0))
  305.                 bullets.append(bullet)
  306.            
  307. app = Ursina()
  308. ReadData()
  309. player = Player()
  310. Invasion(False)
  311. Sky(texture="assets/space.jpg")
  312. LevelButton4 = Button(text="4", text_color=color.light_gray,background=True,radius=0.3)
  313. LevelButton4.x = -0.5
  314. LevelButton4.y = 0.3
  315. LevelButton4.fit_to_text()
  316. LevelButton4.enabled = False
  317. LevelButton4.on_click = lambda: ChangeLevel(4)
  318.  
  319. LevelButton3 = Button(text="3", text_color=color.light_gray,background=True,radius=0.3)
  320. LevelButton3.x = -0.6
  321. LevelButton3.y = 0.3
  322. LevelButton3.fit_to_text()
  323. LevelButton3.enabled = False
  324. LevelButton3.on_click = lambda: ChangeLevel(3)
  325.  
  326. LevelButton2 = Button(text="2", text_color=color.light_gray,background=True,radius=0.3)
  327. LevelButton2.x = -0.7
  328. LevelButton2.y = 0.3
  329. LevelButton2.fit_to_text()
  330. LevelButton2.enabled = False
  331. LevelButton2.on_click = lambda: ChangeLevel(2)
  332.  
  333. LevelButton1 = Button(text="1", text_color=color.light_gray,background=True,radius=0.3)
  334. LevelButton1.x = -0.8
  335. LevelButton1.y = 0.3
  336. LevelButton1.fit_to_text()
  337. LevelButton1.enabled = False
  338. LevelButton1.on_click = lambda: ChangeLevel(1)
  339.  
  340. Levels.append(LevelButton1)
  341. Levels.append(LevelButton2)
  342. Levels.append(LevelButton3)
  343. Levels.append(LevelButton4)
  344.  
  345. LevelButton = Button(text="Levels", text_color=color.light_gray,background=True)
  346. LevelButton.y = -0.225
  347. LevelButton.fit_to_text()
  348. LevelButton.enabled = False
  349. LevelButton.on_click = lambda: Enable1(Levels)
  350.  
  351. RestartButton = Button(text="Restart", text_color=color.light_gray,background=True)
  352. RestartButton.y = -0.1757
  353. RestartButton.fit_to_text()
  354. RestartButton.enabled = False
  355. RestartButton.on_click = lambda: Invasion(False)
  356.  
  357. CloseButton = Button(text="Close", text_color=color.light_gray,background=True)
  358. CloseButton.y = -0.274
  359. CloseButton.fit_to_text()
  360. CloseButton.enabled = False
  361. CloseButton.on_click = lambda: Close()
  362.  
  363. MenuButton = Button(text="Main Menu",text_color=color.light_gray,scale=(0.4,0.1,0),radius=0.3, background=True)
  364. MenuButton.y = -0.103
  365. MenuButton.enabled = False
  366.  
  367. LevelsText = Button(text="Levels", text_color=color.light_gray,scale=(0.4,0.1,0), radius=0.3,background=True)
  368. LevelsText.y = 0.375
  369. LevelsText.fit_to_text()
  370.  
  371. PauseButton = Button(text="Pause",text_color=color.light_gray,scale=(0.4,0.1,0),radius=0.3, background=True)
  372. PauseButton.y = 0.475
  373. PauseButton.on_click = lambda: Pause()
  374.  
  375. StartButton = Button(text='START', text_color=color.light_gray, radius=0.5,background=True)
  376. StartButton.on_click = lambda: StartB()
  377.  
  378. ExitButton = Button(text="Exit",text_color=color.yellow,color=color.white,radius=0.3, background=True)
  379. ExitButton.y = 0.475
  380. ExitButton.x = -0.855
  381. ExitButton.fit_to_text(radius=0.3)
  382. ExitButton.on_click = lambda: WriteData()
  383.  
  384. GButton = Button(text="Give Up",text_color=color.yellow,color=color.white,scale=.125/2, background = True)
  385. GButton.x = 0.830
  386. GButton.y = 0.475
  387. GButton.fit_to_text(radius=0.3)
  388. GButton.on_click = lambda: Enable(MenuButton, LevelButton, RestartButton,CloseButton)
  389.  
  390. text = Text('')
  391. window.title = "space"
  392. window.fullscreen = True
  393. window.borderless = True
  394. window.exit_button.visible = False
  395. window.fps_counter.enabled = True
  396.  
  397.  
  398.  
  399. app.run()
Advertisement
Add Comment
Please, Sign In to add comment