Sanjin1

Untitled

Sep 14th, 2021
792
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import stuff
  2. from ursina import *
  3. from ursina.prefabs.first_person_controller import FirstPersonController
  4. from random import *
  5.  
  6. app = Ursina()
  7.  
  8. #create a player
  9. player = FirstPersonController(model = 'cube', collider = 'box', jump_height = 2, gravity = 1, speed = 5, health = 100)
  10.  
  11. #create a floor
  12. floor = Entity(collider = 'box',
  13.                model = 'plane',
  14.                scale = (100,1,100),
  15.                texture = 'white_cube',
  16.                texture_scale = (100,100),
  17.                color = color.white.tint(-0.1))
  18.  
  19. #build a target
  20. target = Entity(model = 'cube',
  21.                 collider = 'box',
  22.                 scale = (2,2,2),
  23.                 color = color.green,
  24.                 position = (30,1,30))
  25.  
  26. enemies = []
  27. enemy1 = Entity(model = 'cube',
  28.                 collider = 'box',
  29.                 scale = (2,2,2),
  30.                 color = color.red,
  31.                 position = (49,1,49),
  32.                 dx = 0,dz = 0,
  33.                 texture = 'brick')
  34. enemies.append(enemy1)
  35. enemy2 = Entity(model = 'cube',
  36.                 collider = 'box',
  37.                 scale = (2,2,2),
  38.                 color = color.red,
  39.                 position = (-49,1,49),
  40.                 dx = 0,dz = 0,
  41.                 texture = 'brick')
  42. enemies.append(enemy2)
  43. enemy3 = Entity(model = 'cube',
  44.                 collider = 'box',
  45.                 scale = (2,2,2),
  46.                 color = color.red,
  47.                 position = (-49,1,-49),
  48.                 dx = 0,dz = 0,
  49.                 texture = 'brick')
  50. enemies.append(enemy3)
  51. enemy4 = Entity(model = 'cube',
  52.                 collider = 'box',
  53.                 scale = (2,2,2),
  54.                 color = color.red,
  55.                 position = (49,1,-49),
  56.                 dx = 0,dz = 0,
  57.                 texture = 'brick')
  58. enemies.append(enemy4)
  59.  
  60. obstacles = []
  61.  
  62.  
  63. counter = 0
  64.  
  65. def update():
  66.     global counter
  67.     counter += 1
  68.     if counter % 60 == 0:
  69.         obstacle = Entity(model = 'cube',
  70.                 collider = 'box',
  71.                 scale = (2,2,2),
  72.                 color = color.yellow,
  73.                 position = (randint(-50,50) ,100, randint(-50,50)),
  74.                 dy = -0.2,
  75.                 texture = 'brick')
  76.         obstacles.append(obstacle)
  77.     for obstacle in obstacles:
  78.         if obstacle.intersects().hit == False:
  79.             obstacle.y += obstacle.dy
  80.     for enemy in enemies:    
  81.         enemy_collision = enemy.intersects()
  82.         if enemy_collision.hit and enemy_collision.entity == target:
  83.             enemy.x -= 2*enemy.dx
  84.             enemy.z -= 2*enemy.dz
  85.         elif enemy_collision.hit and enemy_collision.entity == player:
  86.             player.health -= 5
  87.             print(player.health)
  88.         elif enemy_collision.hit and enemy_collision.entity in obstacles:
  89.             enemy.x -= 2*enemy.dx
  90.             enemy.z -= 2*enemy.dz
  91.            
  92.         else:
  93.             enemy.x += enemy.dx
  94.             enemy.z += enemy.dz
  95.        
  96.         if player.x > enemy.x:
  97.             enemy.dx = 0.01
  98.         else:
  99.             enemy.dx = -0.01
  100.        
  101.     #change the enemy dy
  102.     if player.z > enemy.z:
  103.         enemy.dz = 0.01
  104.     else:
  105.         enemy.dz = -0.01
  106.    
  107.     hit_info = player.intersects()
  108.     if hit_info.hit:
  109.         if hit_info.entity == target:
  110.             player.jump_height = 20
  111.             player.gravity = 0.1
  112.         else:
  113.             player.jump_height = 2
  114.             player.gravity = 1            
  115.            
  116.     if held_keys['c']:
  117.         application.pause()
  118.         mouse.locked = False
  119.     if player.health <0:
  120.         message = Text(text = 'YOU LOSE', origin = (0,0), background = True, color=color.red)
  121.         application.pause()
  122.         mouse.locked = False
  123.  
  124. app.run()
  125.  
RAW Paste Data