1

this is my code and I don't find any error in it.

from ursina import *

game = Ursina()

camera.orthographic=True
camera.fov=8.2

No need to care about these borders entities

border1 = Entity(model='quad', scale=(15, 0.2), color = color.white, position=(0, 4), collider='box')         
border2 = Entity(model='quad', scale=(15, 0.2), color = color.white, position=(0, -4), collider='box')      
border3 = Entity(model='quad', scale=(0.2, 8), color=color.white, position=(-7.2, 0), collider='box')       
border4 = Entity(model='quad', scale=(0.2, 8), color=color.white, position=(7.2, 0), collider='box')    
border5 = Entity(model='quad', scale=(0.2,8), color=color.white, position=(0,0), collider='box')

These player1 and player2 entities' collision doesn't work

player1 = Entity(model='cube', scale=(.3,.3,2), color=color.blue, position=(3.5,0,0), collider='box')     
player2 = Entity(model='cube', scale=(.3,.3,2) , color=color.red, position=(-3.5,0,0), collider='box')


player1_counter=0
player2_counter=0


f_counter = 0

update part..

def update():     
      

player1 moving system

      if held_keys['up arrow'] and player1.y < 3.5:    
          player1.y += 2 * time.dt    
      if held_keys['down arrow'] and player1.y > -3.5:     
          player1.y -= 2 * time.dt    
      if held_keys['right arrow'] and player1.x < 6.7:    
          player1.x += 2 * time.dt    
      if held_keys['left arrow'] and player1.x > 0.5:    
          player1.x -= 2 * time.dt

player2 moving system

      if held_keys['z'] and player2.y < 3.5:    
          player2.y += 2 * time.dt    
      if held_keys['s'] and player2.y > -3.5:    
          player2.y -= 2 * time.dt    
      if held_keys['d'] and player2.x < -.5:    
          player2.x += 2 * time.dt    
      if held_keys['q'] and player2.x > -6.7:    
          player2.x -= 2 * time.dt

 
    
      foods=[]     
      global f_counter      
      if f_counter == 0:       
          f_counter += 1        
          random_generator=random.Random()         
          x_p=random_generator.random() * 14 - 7       
          y_p=random_generator.random() * 7 - 3.5        
          

Same goes for this food entity

          food = Entity(model = 'cube',scale = (.2 ,.2, 2),color = color.black,position = 
          (x_p, y_p,0),collider = 'box')

  
          foods.append(food)       

          hit_info = food.intersects()       
          if hit_info.hit:        
              destroy(food)      
              f_counter -= 1      


game.run()   
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Please update your question with what exactly isn't working, i.e. your expected and actual results. Also, it's better to provide a [Minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that only contains the code needed to reproduce the problem. – Jan Wilamowski Jul 14 '21 at 03:36

1 Answers1

0

The problem is that you create food when f_counter is zero and then want to check for collision in the same if block, with the same condition, even though you immediately changed f_counter. Later, when the player moves the squares and expects a collision, it isn't actually checked.

So you have to separate those two situations: move the creation of the food out of the update() method and only check the collision when f_counter > 0. In fact, you don't need a separate food counter if you save all the foods to a list:

random_generator = random.Random()
foods = []

def make_food():
    x_p = random_generator.random() * 14 - 7
    y_p = random_generator.random() * 7 - 3.5
    food = Entity(model='cube', scale=(.2, .2, 2),
                  color=color.black, position=(x_p, y_p, 0), collider='box')
    foods.append(food)

make_food()

def update():
    # movement code...

    for food in foods:
        hit_info = food.intersects()
        if hit_info.hit:
            destroy(food)
Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23