1

pygame では、ショットがゾンビに当たるたびにポイントを 1000 ずつ増加させようとしています。ゾンビの位置は zhot[XX] と zhot[YY] です。ゾンビの周りに長方形を作成し、collidepoint 関数を使用してこれを達成しようとしましたが、ショットが長方形を通過すると、その位置のすべての変化が 1000 ポイントとしてカウントされるため、1 つのゾンビを撃つと 30000 ポイントのようになります。どうすればこれを修正できますか?

for shot in shots:
    zomrect2=Rect(zhot[XX],zhot[YY],49,38)
    if zomrect2.collidepoint(shot[X],shot[Y]):     
        points+=1000 
4

3 に答える 3

1

ポイントを獲得したらbreak、for ループから抜け出す必要があります。

for shot in shots:
    zomrect2=Rect(zhot[XX],zhot[YY],49,38)
    if zomrect2.collidepoint(shot[X],shot[Y]):     
        points+=1000 
        break #no more points will be awarded, there's no point wasting computation checking the rest.
于 2012-06-09T19:28:08.603 に答える
1

投稿したループはメイン ループ内で実行され、メイン ループの反復ごとに呼び出されると思います。

ゾンビにヒットしたらリストshotから削除する必要があります。これにより、メイン ループの次の反復で衝突が再度チェックされなくなります。shots

zomrect2=Rect(zhot[XX],zhot[YY],49,38)
# make a copy of the list so we can savely remove items
# from it while iterating over the list
for shot in shots[:]: 
  if zomrect2.collidepoint(shot[X],shot[Y]):     
    points += 1000 
    shots.remove(shot) 
    # and also do whatever you have to do to 
    # erase the shot objects from your game
于 2012-06-10T18:28:16.860 に答える
0

ポイントがすでに付与されているという事実を追跡する必要があります。ポイントを付与するメソッドまたは関数がいつどのように呼び出されるかは明確ではありませんが、おそらく次のようなものです。

points_awarded = False
for shot in shots:
    zomrect2=Rect(zhot[XX],zhot[YY],49,38)
    if zomrect2.collidepoint(shot[X],shot[Y]) and not points_awarded:      
        points_awarded = True
        points+=1000 
于 2012-06-09T18:04:08.870 に答える