2016-05-09 13 views
0

私はここで何をすべきかわからないので、私はPythonのカメが弾を撃ち、それが他のカメと衝突するとスコアが上がるようにしたいと思います。このコードを実行する以外は、弾丸を撃つとゲームがクラッシュします。これまでのところ、私は8つの移動コマンドのうちの1つを試してみました。どのようにして衝突チェックを実行できますか?

#imports turtles 
import turtle 
import random 
#Sets screen, turtle program, and colour group 
turtle.setup(1000, 800) 
screen = turtle.Screen() 
wn = turtle.Screen() 
wn.bgcolor("springgreen") 

amir = turtle.Turtle() 
amir.shape("arrow") 
amir.shapesize(1) 
amir.speed(10) 
amir.pensize (2) 
amir.color("blue") 
amir.setheading(90) 
amir.penup() 
#bullet 1 
jonafat = turtle.Turtle() 
jonafat.shape("circle") 
jonafat.shapesize(0.5) 
jonafat.speed(2) 
jonafat.pensize (1) 
jonafat.color("black") 
jonafat.penup() 
jonafat.ht() 

hossein = turtle.Turtle() 
hossein.shape("arrow") 
hossein.shapesize(1) 
hossein.speed(10) 
hossein.pensize (2) 
hossein.color("gold") 
hossein.setheading(90) 
hossein.penup() 
#bullet 
jonafat2 = turtle.Turtle() 
jonafat2.shape("circle") 
jonafat2.shapesize(0.5) 
jonafat2.speed(2) 
jonafat2.pensize (1) 
jonafat2.color("black") 
jonafat2.penup() 
jonafat2.ht() 
#scoreboard 
TT = turtle.Turtle() 
TT.ht() 
TT.penup() 
TT.goto(-500,200) 
TT.color("black") 
TT.write("0", move = False, align = "center", font = ("Arial", 20, "normal")) 
#second scoreboard 
TT = turtle.Turtle() 
TT.ht() 
TT.penup() 
TT.goto(-500,200) 
TT.color("black") 
TT.write("0", move = False, align = "center", font = ("Arial", 20, "normal")) 

x = 0 
y = 0 
amirs = 2 
hosseins = 2 
auto = 15 
vanguard = 15 
trump = 0 
time = 1 
score = 0 
panda = 295 

def up(): 
    global amir 
    global x 
    global amirs 
    global hosseins 
    amir.seth(90) 
    n = 1 
    for i in range(0, n): 
     amir.sety(amir.ycor()+10) 
     n + 1 

def down(): 
    global amir 
    global x 
    global amirs 
    global hosseins 
    amir.seth(270) 
    n = 1 
    for i in range(0, n): 
     amir.sety(amir.ycor()-10) 
     n + 1 

def left(): 
    global amir 
    global x 
    global amirs 
    global hosseins 
    amir.seth(180) 
    n = 1 
    for i in range(0, n): 
     amir.setx(amir.xcor()-10) 
     n + 1 

def right(): 
    global amir 
    global x 
    global amirs 
    global hosseins 
    amir.seth(0) 
    n = 1 
    for i in range(0, n): 
     amir.setx(amir.xcor()+10) 
     n + 1 

def up2(): 
    global hossein 
    global y 
    global hosseins 
    hossein.seth(90) 
    n = 1 
    for i in range(0, n): 
     hossein.sety(hossein.ycor()+10) 
     n + 1 

def down2(): 
    global hossein 
    global y 
    global hosseins 
    hossein.seth(270) 
    n = 1 
    for i in range(0, n): 
     hossein.sety(hossein.ycor()-10) 
     n + 1 

def left2(): 
    global hossein 
    global y 
    global hosseins 
    hossein.seth(180) 
    n = 1 
    for i in range(0, n): 
     hossein.setx(hossein.xcor()-10) 
     n + 1 

def right2(): 
    global hossein 
    global y 
    global hosseins 
    hossein.seth(0) 
    n = 1 
    for i in range(0, n): 
     hossein.setx(hossein.xcor()+10) 
     n + 1 

def collisionCheck(jonafat, hossein): 
    crash = True 
    jonafat1X = jonafat.xcor() 
    jonafat1Y = jonafat.ycor() 
    hossein2X = hossein.xcor() 
    hossein2Y = hossein.ycor() 
    jonafatPos = (int(jonafat1X), int(jonafat1Y)) 
    hosseinPos = (int(hossein2X), int(hossein2Y)) 
    if jonafatPos != hosseinPos: 
     crash = False 
    if jonafatPos == hosseinPos: 
     crash = True 
    return crash 

def clock(): 
    global time 
    time = time-1 

def move(): 
    global auto 
    global vanguard 
    global score 
    global time 
    wn.ontimer(clock,1000) 
    angle = amir.heading() 
    jonafat.ht() 
    jonafat.speed(10) 
    jonafat.setpos(amir.xcor(), amir.ycor()) 
    if angle == 0: 
     trump = 1. 
     time = time-1 
     jonafat.showturtle() 
     jonafat.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat.goto(amir.xcor()+300, amir.ycor()) 
      n + 1 
     infLoop = 1 
     while infLoop == 1: 
       if collisionCheck(jonafat, hossein) == True: 
        infLoop = 0 
        break 
        score = score + 1 
        TT.clear() 
        TT.write(score, move = False, align = "center", font = ("Arial", 20, "normal")) 

       if time == 0: 
        break 
        infLoop = 0 


    if angle == 90: 
     jonafat.showturtle() 
     jonafat.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat.goto(amir.xcor(),amir.ycor()+300) 
      n + 1 
    if angle == 180: 
     jonafat.showturtle() 
     jonafat.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat.goto(amir.xcor()-300, amir.ycor()) 
      n + 1 
    if angle == 270: 
     jonafat.showturtle() 
     jonafat.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat.goto(amir.xcor(), amir.ycor()-300) 
      n + 1 


def move2(): 
    angle2 = hossein.heading() 
    jonafat2.ht() 
    jonafat2.speed(10) 
    jonafat2.setpos(hossein.xcor(), hossein.ycor()) 
    if angle2 == 0: 
     jonafat2.showturtle() 
     jonafat2.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat2.goto(hossein.xcor()+300, hossein.ycor()) 
      n + 1 
    if angle2 == 90: 
     jonafat2.showturtle() 
     jonafat2.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat2.goto(hossein.xcor(), hossein.ycor()+300) 
      n + 1 
    if angle2 == 180: 
     jonafat2.showturtle() 
     jonafat2.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat2.goto(hossein.xcor()-300, hossein.ycor()) 
      n + 1 
    if angle2 == 270: 
     jonafat2.showturtle() 
     jonafat2.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat2.goto(hossein.xcor(), hossein.ycor()-300) 
      n + 1 








wn.onkeypress(up, "w") 
wn.onkeypress(down, "s") 
wn.onkeypress(left, "a") 
wn.onkeypress(right, "d") 
wn.onkeypress(up2, "Up") 
wn.onkeypress(down2, "Down") 
wn.onkeypress(left2, "Left") 
wn.onkeypress(right2, "Right") 
wn.onkeypress(move2, "Shift_R") 

wn.onkeypress(move, "space") 

wn.listen() 

これは私が彼らの位置が同じであれば、彼らは視覚的に衝突するように、2つのオブジェクトが互いに接近して渡すことができますが、それは、あなただけのチェックを表示さ

def collisionCheck(jonafat, hossein): 
    crash = True 
    jonafat1X = jonafat.xcor() 
    jonafat1Y = jonafat.ycor() 
    hossein2X = hossein.xcor() 
    hossein2Y = hossein.ycor() 
    jonafatPos = (int(jonafat1X), int(jonafat1Y)) 
    hosseinPos = (int(hossein2X), int(hossein2Y)) 
    if jonafatPos != hosseinPos: 
     crash = False 
    if jonafatPos == hosseinPos: 
     crash = True 
    return crash 

def clock(): 
    global time 
    time = time-1 

def move(): 
    global auto 
    global vanguard 
    global score 
    global time 
    wn.ontimer(clock,1000) 
    angle = amir.heading() 
    jonafat.ht() 
    jonafat.speed(10) 
    jonafat.setpos(amir.xcor(), amir.ycor()) 
    if angle == 0: 
     trump = 1. 
     time = time-1 
     jonafat.showturtle() 
     jonafat.speed(2) 
     n = 1 
     for i in range(0, n): 
      jonafat.goto(amir.xcor()+300, amir.ycor()) 
      n + 1 
     infLoop = 1 
     while infLoop == 1: 
       if collisionCheck(jonafat, hossein) == True: 
        infLoop = 0 
        break 
        score = score + 1 
        TT.clear() 
        TT.write(score, move = False, align = "center", font = ("Arial", 20, "normal")) 

       if time == 0: 
        break 
        infLoop = 0 

答えて

0

とのトラブルを持っているが、一部であり、互いに正確に同じ位置にはありません。

衝突を適切にチェックするには、ディメンションを考慮する必要があります。彼らが衝突するかどうかを確認する最も簡単な方法は、彼らの位置の間の距離を取って、それが特定の値よりも小さいかどうかを確認することです。より良いオプションが存在しますが。

0

の代わりに、このすべて:

def collisionCheck(jonafat, hossein): 
    crash = True 
    jonafat1X = jonafat.xcor() 
    jonafat1Y = jonafat.ycor() 
    hossein2X = hossein.xcor() 
    hossein2Y = hossein.ycor() 
    jonafatPos = (int(jonafat1X), int(jonafat1Y)) 
    hosseinPos = (int(hossein2X), int(hossein2Y)) 
    if jonafatPos != hosseinPos: 
     crash = False 
    if jonafatPos == hosseinPos: 
     crash = True 
    return crash 

のは、@ JoshuaWaringのアドバイスの利点だけでなく、カメのdistance()方法を取ると、より多くのような何かを思い付くしてみましょう:

PERSONAL_SPACE = 10 # assign this to be how close is too close 

def collisionCheck(jonafat, hossein): 
    return jonafat.distance(hossein) < PERSONAL_SPACE 

のはの残りの部分を手直ししてみましょう最低限再生可能なゲームを生成するコード:

# imports turtles 
from turtle import Turtle, Screen 

PERSONAL_SPACE = 15 # assign this to be how close is too close 
FIRE_DISTANCE = 300 
FONT = ('Arial', 36, 'normal') 

# Sets screen, turtle program, and color group 
screen = Screen() 
screen.setup(1000, 800) 
screen.bgcolor('springgreen') 

def collisionCheck(bullet, player): 
    return bullet.distance(player) < PERSONAL_SPACE 

def up1(): 
    player1.seth(90) 
    player1.forward(10) 

def down1(): 
    player1.seth(270) 
    player1.forward(10) 

def left1(): 
    player1.seth(180) 
    player1.forward(10) 

def right1(): 
    player1.seth(0) 
    player1.forward(10) 

def clock1(): 
    global time1, score1 

    if collisionCheck(bullet1, player2): 
     score1 += 1 
     TT1.undo() 
     TT1.write(score1, align='center', font=FONT) 
     time1 = 1 
    else: 
     bullet1.forward(1) 

    time1 -= 1 

    if time1: 
     screen.ontimer(clock1, 10) 
    else: 
     bullet1.hideturtle() 

def fire1(): 
    global time1 

    bullet1.setpos(player1.pos()) 
    bullet1.setheading(player1.heading()) 
    bullet1.showturtle() 

    time1 = FIRE_DISTANCE 
    screen.ontimer(clock1, 100) 

screen.onkeypress(up1, 'w') 
screen.onkeypress(down1, 's') 
screen.onkeypress(left1, 'a') 
screen.onkeypress(right1, 'd') 
screen.onkeypress(fire1, 'space') 

def up2(): 
    player2.seth(90) 
    player2.forward(10) 

def down2(): 
    player2.seth(270) 
    player2.forward(10) 

def left2(): 
    player2.seth(180) 
    player2.forward(10) 

def right2(): 
    player2.seth(0) 
    player2.forward(10) 

def clock2(): 
    global time2, score2 

    if collisionCheck(bullet2, player1): 
     score2 += 1 
     TT2.undo() 
     TT2.write(score2, align='center', font=FONT) 
     time2 = 1 
    else: 
     bullet2.forward(1) 

    time2 -= 1 

    if time2: 
     screen.ontimer(clock2, 10) 
    else: 
     bullet2.hideturtle() 

def fire2(): 
    global time2 

    bullet2.setpos(player2.pos()) 
    bullet2.setheading(player2.heading()) 
    bullet2.showturtle() 

    time2 = FIRE_DISTANCE 
    screen.ontimer(clock2, 100) 

screen.onkeypress(up2, 'Up') 
screen.onkeypress(down2, 'Down') 
screen.onkeypress(left2, 'Left') 
screen.onkeypress(right2, 'Right') 
screen.onkeypress(fire2, '0') 

player1 = Turtle('arrow') 
player1.speed('fastest') 
player1.color('blue') 
player1.setheading(180) 
player1.penup() 
player1.forward(10) 

# bullet 1 
bullet1 = Turtle('circle', visible=False) 
bullet1.shapesize(0.5) 
bullet1.speed('fastest') 
bullet1.color('black', 'blue') 
bullet1.penup() 

player2 = Turtle('arrow') 
player2.speed('fastest') 
player2.color('gold') 
player2.setheading(0) 
player2.penup() 
player2.forward(10) 

# bullet 2 
bullet2 = Turtle('circle', visible=False) 
bullet2.shapesize(0.5) 
bullet2.speed('fastest') 
bullet2.color('black', 'gold') 
bullet2.penup() 

# scoreboard 
TT1 = Turtle(visible=False) 
TT1.penup() 
TT1.goto(-450, 300) 
TT1.color('blue') 
TT1.write('0', align='center', font=FONT) 

# second scoreboard 
TT2 = Turtle(visible=False) 
TT2.penup() 
TT2.goto(450, 300) 
TT2.color('gold') 
TT2.write('0', align='center', font=FONT) 

time1 = 0 
score1 = 0 

time2 = 0 
score2 = 0 

screen.listen() 

screen.mainloop() 

追加の作業が必要です。つまり、複数のショットを正しく処理できない(または無効にする)などです。

enter image description here

関連する問題