Repeat the process for the set direction in clockwise or anticlockwise.
if sprite.iskeypressed("left arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "LEFT"}, ignore_index=True)
angle = anglef - fish_d
sprite.setdirection(DIRECTION=angle)
if sprite.iskeypressed("right arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "RIGHT"}, ignore_index=True)
angle = anglef + fish_d
sprite.setdirection(DIRECTION=angle)
The final code is as follows:
sprite = Sprite('Fish')
sprite1 = Sprite('Orange')
sprite2 = Sprite('Shark 2')
sprite3 = Sprite('Button3')
import random
import time
import numpy as np
import tensorflow as tf
import pandas as pd
import os
import math
curr_x = 25
curr_y = 108
shark_x=-177
shark_y=116
score=0
chance=5
fish_d=20
fish_m=25
shark_m=4
angle_f=90
angle_s=90
sprite3.say(("score: ",score ," and chance: ",chance,""))
sprite.setx(curr_x)
sprite.sety(curr_y)
sprite2.setx(shark_x)
sprite2.sety(shark_y)
sprite.setdirection(DIRECTION=angle_f)
sprite2.setdirection(DIRECTION=angle_s)
def settarget(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
x1 = random.randrange(-200, 200, t)
y1 = random.randrange(-155, 155, t)
x2 = random.randrange(-200, 200, t)
y2 = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x1)
sprite1.sety(y1)
sprite.setx(x)
sprite.sety(y)
sprite2.setx(x2)
sprite2.sety(y2)
return x, y, x1, y1, x2, y2
def settarget1(m):
x = random.randrange(-200, 200, m)
y = random.randrange(-155, 155, m)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget1(40)
if(os.path.isfile('Chase_Data.csv')):
data=pd.read_csv('Chase_Data.csv')
else:
data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": angle_f, "direction_s": angle_s, "Action": "RIGHT"}, index=[0])
while True:
# sprite2.pointto()
sprite2.spriteRequest.requestCommand("motion_pointtowards", {"TOWARDS": "Fish"})
sprite2.move(shark_m)
angle_f=sprite.direction()
angle_s=sprite2.direction()
anglef=math.floor(angle_f)
angles=math.floor(angle_s)
if sprite.iskeypressed("up arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "UP"}, ignore_index=True)
sprite.move(fish_m)
if sprite.iskeypressed("left arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "LEFT"}, ignore_index=True)
angle = anglef - fish_d
sprite.setdirection(DIRECTION=angle)
if sprite.iskeypressed("right arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "RIGHT"}, ignore_index=True)
angle = anglef + fish_d
sprite.setdirection(DIRECTION=angle)
if(score>0 and score%2==0):
data.to_csv('Chase_Data.csv',index=False)
if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
chance= chance-1
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
sprite3.say(("score: ",score ," and chance: ",chance,""))
if (chance == 0):
data.to_csv('Chase_Data.csv',index=False)
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
chance=5
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite3.say(("score: ",score ," and chance: ",chance,""))
if (score >= 50):
data.to_csv('Chase_Data.csv',index=False)
break
target_x, target_y = settarget1(40)
curr_x=math.floor(sprite.x())
curr_y=math.floor(sprite.y())
shark_x=math.floor(sprite2.x())
shark_y=math.floor(sprite2.y())
time.sleep(0.02)