1

这是我的代码,我没有发现任何错误。

from ursina import *

game = Ursina()

camera.orthographic=True
camera.fov=8.2

无需关心这些边界实体

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')

这些 player1 和 player2 实体的碰撞不起作用

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

更新部分..

def update():     
      

player1移动系统

      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移动系统

      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        
          

这个食品实体也是如此

          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()   
4

1 回答 1

0

问题是您在f_counter为零时创建食物,然后想要在if相同的条件下检查同一块中的碰撞,即使您立即更改了f_counter。稍后,当玩家移动方格并预计会发生碰撞时,实际上并没有检查它。

因此,您必须将这两种情况分开:将食物的创建移出update()方法,仅在 时检查碰撞f_counter > 0。事实上,如果将所有食物保存到一个列表中,则不需要单独的食物柜台:

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)
于 2021-07-22T09:41:25.240 回答