Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ursina import *
- from random import *
- bullets = []
- bullets2 = []
- bullets3 = []
- invaders = []
- hit_info1 = None
- MenuButtons = []
- Levels = []
- Powers = []
- InvaderAmount = 10
- Level = 1
- score = 0
- TotalScore = 0
- ScoreNeeded = 50
- invaderSpeed = 0.2
- speed = 4
- shootSpeed = 2
- time1 = 0
- count = 20
- move = False
- doubleP = True
- h = True
- c = True
- first = True
- try:
- os.mkdir(os.getcwd() + "/Memory")
- file = open("Memory/data.txt", "w+")
- file.close()
- except:
- pass
- def Wait():
- pass
- def WriteData():
- global invaders, bullets,score,text, move, InvaderAmount, Level, TotalScore, doubleP
- file = open("Memory/data.txt", "w+")
- file.truncate()
- file.write(
- f"""
- Level={Level},
- TotalScore={TotalScore},
- """
- )
- file.close()
- application.quit()
- def ReadData():
- try:
- global invaders, bullets,score,text, move, InvaderAmount, Level, TotalScore,ScoreNeeded, h
- file = open("Memory/data.txt", "r")
- text = file.read()
- file.close()
- Level = int(text.split(",")[0].split("=")[1])
- print(Level)
- TotalScore = int(text.split(",")[1].split("=")[1])
- if Level == 1:
- InvaderAmount = 10
- elif Level == 2:
- InvaderAmount = 12
- elif Level >= 3:
- InvaderAmount = 15
- if Level == 1:
- speed = 4
- elif Level >= 2:
- speed = 4
- if Level == 1:
- invaderSpeed = 0.8
- if level >= 2:
- invaderSpeed = 1.2
- if Level >= 3:
- ScoreNeeded = 100
- except:
- pass
- def ChangeLevel(n):
- global Level
- Level = n
- def Invasion(start):
- global invaders, move
- if start == True:
- move = True
- for invader in invaders:
- invader.enabled = False
- invaders.remove(invader)
- for i in range(InvaderAmount):
- invader = Invader()
- invaders.append(invader)
- def Enable(*en):
- for i in en:
- i.enabled = True
- def Enable1(eg, **k):
- for i in eg:
- i.enabled = True
- for i in k:
- i.enabled = True
- def Close():
- for i in Levels:
- i.enabled = False
- MenuButton.enabled = False
- CloseButton.enabled = False
- RestartButton.enabled = False
- LevelButton.enabled = False
- def StartB():
- global move, h
- move = True
- StartButton.enabled = False
- h = True
- def Pause():
- global move
- if move:
- move=False
- else:
- move=True
- def update():
- global invaders, bullets,score,text, move, InvaderAmount, Level, TotalScore
- global ScoreNeeded, shootSpeed, doubleP, h, count
- if move:
- player.x += held_keys['right arrow'] * time.dt * speed
- player.x -= held_keys['left arrow'] * time.dt * speed
- if h:
- if score >= ScoreNeeded:
- Level += 1
- ChangeLevel(Level)
- move = False
- TotalScore += score
- WText = Text(text='You Win!',origin=(0,0),scale=2,color=color.gray,background=True)
- MenuButton.enabled = True
- LevelButton.enabled = True
- RestartButton.enabled = True
- CloseButton.enabled = True
- h = False
- for power in Powers:
- if move:
- power.y -= time.dt * 2
- hit_info = power.intersects()
- if hit_info.entity == player:
- doubleP = True
- for invader in invaders:
- if move:
- invader.y -= time.dt*invaderSpeed
- if invader.y < -4:
- b = Entity(model='quad', scale=60, color=color.gray, collider='box')
- b.z = -0.01
- player.y = 10
- move = False
- Text(text='You lost! Reload the game!',origin=(0,0),scale=2,color=color.yellow,background=True)
- MenuButton.enabled = True
- for power in Powers:
- info = power.intersects()
- if info.hit:
- if info.entity == player:
- doubleP = True
- for bullet in bullets3:
- if move:
- bullet.y += 0.25
- hit_info1 = bullet.intersects()
- if hit_info1.hit:
- if hit_info1.entity in invaders:
- rand = randint(1,10)
- if rand == 1:
- powerup = PowerUp()
- Powers.append(powerup)
- del(bullet)
- score += 1
- TotalScore += 1
- text.y = 10
- text=Text(text=f"Score: {score}",position=(-0.65,0.4),origin=(0,0),scale=2,color=color.yellow,background=True)
- Audio('assets/explosion_sound.wav')
- if hit_info1.entity in invaders:
- hit_info1.entity.x = randint(-500,500)*0.01
- hit_info1.entity.y = randint(80,400)*0.01
- for bullet in bullets2:
- if move:
- bullet.y += 0.25
- hit_info1 = bullet.intersects()
- if hit_info1.hit:
- rand = randint(1,10)
- if hit_info1.entity in invaders:
- if rand == 1:
- powerup = PowerUp()
- Powers.append(powerup)
- del(bullet)
- score += 1
- TotalScore += 1
- text.y = 10
- count += 1
- text=Text(text=f"Score: {score}",position=(-0.65,0.4),origin=(0,0),scale=2,color=color.yellow,background=True)
- Audio('assets/explosion_sound.wav')
- if hit_info1.entity in invaders:
- hit_info1.entity.x = randint(-500,500)*0.01
- hit_info1.entity.y = randint(80,400)*0.01
- for bullet in bullets:
- if move:
- bullet.y += 0.25
- hit_info = bullet.intersects()
- if hit_info.hit:
- if hit_info.entity in invaders:
- rand = randint(1,10)
- if rand == 1:
- powerup = PowerUp()
- Powers.append(powerup)
- Audio('assets/explosion_sound.wav')
- del(bullet)
- score += 1
- TotalScore += 1
- text.y = 10
- text=Text(text=f"Score: {score}",position=(-0.65,0.4),origin=(0,0),scale=2,color=color.yellow,background=True)
- if hit_info.entity in invaders:
- hit_info.entity.x = randint(-500,500)*0.01
- hit_info.entity.y = randint(80,400)*0.01
- class PowerUp(Entity):
- def __init__(self):
- super().__init__()
- self.model='cube'
- self.texture='fist.png'
- self.scale = 0.6
- self.position = (randint(-500,500)*0.01,randint(80,400)*0.01,-0.1)
- self.collider = 'box'
- self.power = "double"
- class Invader(Entity):
- def __init__(self):
- super().__init__()
- self.model='quad'
- self.texture='alien.png'
- self.scale = 0.6
- self.position = (randint(-500,500)*0.01,randint(80,400)*0.01,-0.1)
- self.collider = 'box'
- self.dy = -0.15
- class Player(Entity):
- def __init__(self):
- super().__init__()
- self.model = 'cube'
- self.color = color.white
- self.scale = (0.75,1.5,0)
- self.position = (0,-3.5,-0.1)
- self.texture = 'alien.png'
- self.collider = "box"
- self.dx = 0.5
- class Bullet(Entity):
- def __init__(self, x, y):
- super().__init__()
- self.model ="cube"
- self.color = color.orange
- self.scale=(y)
- self.position = Vec3(x, player.y + 1,player.z)
- self.collider = 'box'
- self.dy = 0.8
- class Bullet2(Entity):
- def __init__(self, x, y):
- super().__init__()
- self.model ="cube"
- self.color = color.orange
- self.scale=(y)
- self.position = Vec3(x + 0.5, player.y + 1,player.z)
- self.collider = 'box'
- self.dy = 0.8
- def input(key):
- global bullets, doubleP, time1
- if move:
- if key == "space":
- if doubleP:
- time1 += 1
- #if time1 >= 10:
- #doubleP = False
- #time1 = 0
- #print("false")
- bullet = Bullet(player.x,(0.25,0.5,0))
- bullets.append(bullet)
- bullet2 = Bullet2(player.x + 0.25/2,(0.25,0.5,0))
- bullets2.append(bullet2)
- bullet3 = Bullet2(player.x - 1,(0.25,0.5,0))
- bullets3.append(bullet3)
- else:
- bullet = Bullet(player.x,(0.25,0.5,0))
- bullets.append(bullet)
- app = Ursina()
- ReadData()
- player = Player()
- Invasion(False)
- Sky(texture="assets/space.jpg")
- LevelButton4 = Button(text="4", text_color=color.light_gray,background=True,radius=0.3)
- LevelButton4.x = -0.5
- LevelButton4.y = 0.3
- LevelButton4.fit_to_text()
- LevelButton4.enabled = False
- LevelButton4.on_click = lambda: ChangeLevel(4)
- LevelButton3 = Button(text="3", text_color=color.light_gray,background=True,radius=0.3)
- LevelButton3.x = -0.6
- LevelButton3.y = 0.3
- LevelButton3.fit_to_text()
- LevelButton3.enabled = False
- LevelButton3.on_click = lambda: ChangeLevel(3)
- LevelButton2 = Button(text="2", text_color=color.light_gray,background=True,radius=0.3)
- LevelButton2.x = -0.7
- LevelButton2.y = 0.3
- LevelButton2.fit_to_text()
- LevelButton2.enabled = False
- LevelButton2.on_click = lambda: ChangeLevel(2)
- LevelButton1 = Button(text="1", text_color=color.light_gray,background=True,radius=0.3)
- LevelButton1.x = -0.8
- LevelButton1.y = 0.3
- LevelButton1.fit_to_text()
- LevelButton1.enabled = False
- LevelButton1.on_click = lambda: ChangeLevel(1)
- Levels.append(LevelButton1)
- Levels.append(LevelButton2)
- Levels.append(LevelButton3)
- Levels.append(LevelButton4)
- LevelButton = Button(text="Levels", text_color=color.light_gray,background=True)
- LevelButton.y = -0.225
- LevelButton.fit_to_text()
- LevelButton.enabled = False
- LevelButton.on_click = lambda: Enable1(Levels)
- RestartButton = Button(text="Restart", text_color=color.light_gray,background=True)
- RestartButton.y = -0.1757
- RestartButton.fit_to_text()
- RestartButton.enabled = False
- RestartButton.on_click = lambda: Invasion(False)
- CloseButton = Button(text="Close", text_color=color.light_gray,background=True)
- CloseButton.y = -0.274
- CloseButton.fit_to_text()
- CloseButton.enabled = False
- CloseButton.on_click = lambda: Close()
- MenuButton = Button(text="Main Menu",text_color=color.light_gray,scale=(0.4,0.1,0),radius=0.3, background=True)
- MenuButton.y = -0.103
- MenuButton.enabled = False
- LevelsText = Button(text="Levels", text_color=color.light_gray,scale=(0.4,0.1,0), radius=0.3,background=True)
- LevelsText.y = 0.375
- LevelsText.fit_to_text()
- PauseButton = Button(text="Pause",text_color=color.light_gray,scale=(0.4,0.1,0),radius=0.3, background=True)
- PauseButton.y = 0.475
- PauseButton.on_click = lambda: Pause()
- StartButton = Button(text='START', text_color=color.light_gray, radius=0.5,background=True)
- StartButton.on_click = lambda: StartB()
- ExitButton = Button(text="Exit",text_color=color.yellow,color=color.white,radius=0.3, background=True)
- ExitButton.y = 0.475
- ExitButton.x = -0.855
- ExitButton.fit_to_text(radius=0.3)
- ExitButton.on_click = lambda: WriteData()
- GButton = Button(text="Give Up",text_color=color.yellow,color=color.white,scale=.125/2, background = True)
- GButton.x = 0.830
- GButton.y = 0.475
- GButton.fit_to_text(radius=0.3)
- GButton.on_click = lambda: Enable(MenuButton, LevelButton, RestartButton,CloseButton)
- text = Text('')
- window.title = "space"
- window.fullscreen = True
- window.borderless = True
- window.exit_button.visible = False
- window.fps_counter.enabled = True
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment