我正在 pygame 中制作一个程序,我正在尝试创建用户可以输入名字的按钮。因此,基本上,当他们单击该字母时,就会将该字母放入列表中。我使用与游戏中相同的变量重新创建,并得到了相同的错误。所以如果我们能解决这个问题,那么我就能解决我的游戏问题。我正在使用函数,因此这就是为什么您会看到需要的全局事物。这是代码。顺便说一句,我看过其他论坛,但没有一个与我的论坛相同。
import glob
unknown=[]
def bob():
global unknown
a=('a').upper()
unknown=unknown.extend(a)
unknown=','.join(unknown)
bob()
同样,这基本上就是我的代码中的内容,除了函数名称和列表名称之外,其他都是相同的。不过名字并不重要。提前致谢。
导入数学
班级奖励: def init(自身): self.first_racingpoint_index = 0
def reward_function(self, params):
################## HELPER FUNCTIONS ###################
def dist_2_points(x1, x2, y1, y2):
return abs(abs(x1-x2)**2 + abs(y1-y2)**2)**0.5
def closest_2_racing_points_index(racing_coords, car_coords):
# Calculate all distances to racing points
distances = []
for i in range(len(racing_coords)):
distance = dist_2_points(x1=racing_coords[i][0], x2=car_coords[0],
y1=racing_coords[i][1], y2=car_coords[1])
distances.append(distance)
# Get index of the closest racing point
closest_index = distances.index(min(distances))
# Get index of the second closest racing point
distances_no_closest = distances.copy()
distances_no_closest[closest_index] = 999
second_closest_index = distances_no_closest.index(
min(distances_no_closest))
return [closest_index, second_closest_index]
def dist_to_racing_line(closest_coords, second_closest_coords, car_coords):
# Calculate the distances between 2 closest racing points
a = abs(dist_2_points(x1=closest_coords[0],
x2=second_closest_coords[0],
y1=closest_coords[1],
y2=second_closest_coords[1]))
# Distances between car and closest and second closest racing point
b = abs(dist_2_points(x1=car_coords[0],
x2=closest_coords[0],
y1=car_coords[1],
y2=closest_coords[1]))
c = abs(dist_2_points(x1=car_coords[0],
x2=second_closest_coords[0],
y1=car_coords[1],
y2=second_closest_coords[1]))
# Calculate distance between car and racing line (goes through 2 closest racing points)
# try-except in case a=0 (rare bug in DeepRacer)
try:
distance = abs(-(a**4) + 2*(a**2)*(b**2) + 2*(a**2)*(c**2) -
(b**4) + 2*(b**2)*(c**2) - (c**4))**0.5 / (2*a)
except:
distance = b
return distance
# Calculate which one of the closest racing points is the next one and which one the previous one
def next_prev_racing_point(closest_coords, second_closest_coords, car_coords, heading):
# Virtually set the car more into the heading direction
heading_vector = [math.cos(math.radians(
heading)), math.sin(math.radians(heading))]
new_car_coords = [car_coords[0]+heading_vector[0],
car_coords[1]+heading_vector[1]]
# Calculate distance from new car coords to 2 closest racing points
distance_closest_coords_new = dist_2_points(x1=new_car_coords[0],
x2=closest_coords[0],
y1=new_car_coords[1],
y2=closest_coords[1])
distance_second_closest_coords_new = dist_2_points(x1=new_car_coords[0],
x2=second_closest_coords[0],
y1=new_car_coords[1],
y2=second_closest_coords[1])
if distance_closest_coords_new <= distance_second_closest_coords_new:
next_point_coords = closest_coords
prev_point_coords = second_closest_coords
else:
next_point_coords = second_closest_coords
prev_point_coords = closest_coords
return [next_point_coords, prev_point_coords]
def racing_direction_diff(closest_coords, second_closest_coords, car_coords, heading):
# Calculate the direction of the center line based on the closest waypoints
next_point, prev_point = next_prev_racing_point(closest_coords,
second_closest_coords,
car_coords,
heading)
# Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians
track_direction = math.atan2(
next_point[1] - prev_point[1], next_point[0] - prev_point[0])
# Convert to degree
track_direction = math.degrees(track_direction)
# Calculate the difference between the track direction and the heading direction of the car
direction_diff = abs(track_direction - heading)
if direction_diff > 180:
direction_diff = 360 - direction_diff
return direction_diff
#################### RACING LINE ######################
# Optimal racing line for the Spain track
# Each row: [x,y,speed,timeFromPreviousPoint]
racing_track =[[0.26173, 0.85888, 3.7, 0.06009],
[0.12154, 1.01586, 3.7, 0.05688],
[-0.01507, 1.16866, 3.7, 0.0554],
[-0.17352, 1.34589, 3.7, 0.06425],
[-0.34585, 1.539, 3.7, 0.06995],
[-0.52762, 1.74302, 3.7, 0.07385],
[-0.7163, 1.95518, 3.7, 0.07673],
[-0.91031, 2.17369, 3.7, 0.07898],
[-1.1072, 2.3957, 3.7, 0.0802],
[-1.30845, 2.62008, 3.4, 0.08865],
[-1.5142, 2.83779, 3.0, 0.09985],
[-1.7265, 3.04201, 2.6, 0.1133],
[-1.94653, 3.22611, 2.3, 0.12473],
[-2.1742, 3.38394, 2.0, 0.13851],
[-2.40831, 3.50819, 2.0, 0.13252],
[-2.64579, 3.5918, 2.0, 0.12588],
[-2.88146, 3.62836, 2.0, 0.11925],
[-3.10766, 3.61202, 2.0, 0.1134],
[-3.31311, 3.53708, 2.0, 0.10934],
[-3.47757, 3.39755, 2.2, 0.09803],
[-3.59553, 3.21443, 2.4, 0.09076],
[-3.6664, 3.00309, 2.7, 0.08256],
[-3.69308, 2.77542, 3.0, 0.07641],
[-3.6791, 2.54, 3.3, 0.07146],
[-3.63029, 2.30328, 3.4, 0.07109],
[-3.5541, 2.06907, 2.7, 0.09122],
[-3.45872, 1.83859, 2.7, 0.09238],
[-3.35184, 1.61122, 2.7, 0.09305],
[-3.24632, 1.39949, 2.7, 0.08761],
[-3.15595, 1.18389, 2.7, 0.08658],
[-3.09591, 0.96018, 2.7, 0.08579],
[-3.08414, 0.72312, 3.4, 0.06981],
[-3.10649, 0.47685, 3.7, 0.06683],
[-3.15781, 0.22296, 3.7, 0.07001],
[-3.23295, -0.03736, 3.7, 0.07323],
[-3.32688, -0.30452, 3.7, 0.07654],
[-3.4347, -0.58199, 3.7, 0.08046],
[-3.53443, -0.86691, 3.7, 0.08158],
[-3.62075, -1.15446, 3.6, 0.0834],
[-3.68998, -1.4436, 3.3, 0.09009],
[-3.73875, -1.73254, 2.9, 0.10104],
[-3.76423, -2.01885, 2.6, 0.11056],
[-3.76188, -2.2989, 2.3, 0.12176],
[-3.72864, -2.56791, 2.1, 0.12907],
[-3.6623, -2.81991, 1.8, 0.14477],
[-3.56181, -3.04775, 1.8, 0.13834],
[-3.42761, -3.24299, 1.8, 0.13162],
[-3.26178, -3.39488, 1.8, 0.12493],
[-3.07042, -3.49211, 1.8, 0.11924],
[-2.86382, -3.52042, 1.8, 0.11585],
[-2.66298, -3.45958, 2.2, 0.09539],
[-2.4823, -3.33762, 2.5, 0.0872],
[-2.32769, -3.166, 2.8, 0.0825],
[-2.20313, -2.95308, 3.2, 0.07709],
[-2.10957, -2.70793, 3.1, 0.08464],
[-2.04284, -2.44083, 2.7, 0.10197],
[-1.99508, -2.16083, 2.4, 0.11835],
[-1.92823, -1.88565, 2.1, 0.13485],
[-1.83753, -1.6302, 1.9, 0.14267],
[-1.71996, -1.40232, 1.9, 0.13496],
[-1.57493, -1.21021, 1.9, 0.12669],
[-1.40488, -1.06126, 1.9, 0.11898],
[-1.21418, -0.96411, 1.9, 0.11264],
[-1.01044, -0.93107, 1.9, 0.10863],
[-0.81019, -0.9778, 2.1, 0.09792],
[-0.62944, -1.08769, 2.3, 0.09197],
[-0.4761, -1.24794, 2.6, 0.0853],
[-0.35501, -1.44931, 2.9, 0.08103],
[-0.26811, -1.68301, 2.7, 0.09235],
[-0.21467, -1.94077, 2.2, 0.11966],
[-0.18944, -2.21432, 1.9, 0.14459],
[-0.18806, -2.49836, 1.9, 0.1495],
[-0.20128, -2.7572, 1.9, 0.13641],
[-0.18777, -3.00014, 1.9, 0.12806],
[-0.13008, -3.21329, 1.9, 0.11622],
[-0.02252, -3.38556, 1.9, 0.10689],
[0.13633, -3.5014, 2.1, 0.09362],
[0.32742, -3.56853, 2.4, 0.08439],
[0.54172, -3.5901, 2.8, 0.07692],
[0.77234, -3.57055, 3.4, 0.06807],
[1.01401, -3.51545, 3.7, 0.06699],
[1.26575, -3.48541, 3.7, 0.06852],
[1.51701, -3.47891, 3.1, 0.08108],
[1.76764, -3.49315, 2.8, 0.08965],
[2.01753, -3.52589, 2.4, 0.10501],
[2.26657, -3.57625, 2.2, 0.11549],
[2.51404, -3.64552, 1.9, 0.13525],
[2.75878, -3.68403, 1.9, 0.13039],
[2.98982, -3.68311, 1.9, 0.1216],
[3.20159, -3.64024, 1.9, 0.11372],
[3.3882, -3.55367, 1.9, 0.10827],
[3.54012, -3.41996, 1.9, 0.10651],
[3.63686, -3.23389, 2.2, 0.09532],
[3.6753, -3.0134, 2.4, 0.09326],
[3.65379, -2.77436, 2.7, 0.08889],
[3.5749, -2.53284, 3.0, 0.08469],
[3.44743, -2.30146, 3.4, 0.07769],
[3.28064, -2.08714, 3.7, 0.0734],
[3.08343, -1.89179, 3.7, 0.07502],
[2.86393, -1.71359, 3.7, 0.07641],
[2.62895, -1.54879, 3.7, 0.07757],
[2.39429, -1.36424, 3.7, 0.08068],
[2.16757, -1.1691, 3.7, 0.08085],
[1.94757, -0.96536, 3.7, 0.08104],
[1.73323, -0.75483, 3.7, 0.0812],
[1.5238, -0.53926, 3.7, 0.08123],
[1.31895, -0.32076, 3.7, 0.08095],
[1.11891, -0.10198, 3.7, 0.08012],
[0.9249, 0.11379, 3.7, 0.07842],
[0.7378, 0.32378, 3.7, 0.07601],
[0.56798, 0.51498, 3.7, 0.06912],
[0.40967, 0.69291, 3.7, 0.06437]]
# planned speed based on waypoints
# manually adjust the list for better performance, e.g. lower the speed before turning
above_three_five = [0, 1, 2, 3, 4, 5, 6, 7, 8, 32, 33, 34, 35, 36, 37, 38, 80, 81, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112]
above_three = [9, 10, 22, 23, 24, 31, 39, 53, 54, 79, 82, 95, 96]
above_two_five = [11, 21, 25, 26, 27, 28, 29, 30, 40, 41, 51, 52, 55, 66, 67, 68, 78, 83, 94]
above_two = [12, 13, 14, 15, 16, 17, 18, 19, 20, 42, 43, 50, 56, 57, 64, 65, 69, 76, 77, 84, 85, 92, 93]
below_two = [44, 45, 46, 47, 48, 49, 58, 59, 60, 61, 62, 63, 70, 71, 72, 73, 74, 75, 86, 87, 88, 89, 90, 91]
# planned speed based on waypoints
# observe which side the car is expected to run at
right_track = [28,29,30,31,32,33,57,58,59,60,61,62,63,64,65,66,67,68,69,70,81,82,83,84,85]
center_track = [112, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 27,34,35,36,37,38,39,56,71,80,86,100,101,102,103,104,105,106,107,108,109,110,111]
left_track = [i for i in range(0, 112) if i not in right_track + center_track]
# obvious sides
strong_left = [17,18,19,20,21,24,25,47,48,49,75,76,77,89,90]
strong_right =[29,30,60,61,62,63,64,82,83,84]
################## INPUT PARAMETERS ###################
# Read all input parameters
x = params['x']
y = params['y']
distance_from_center = params['distance_from_center']
is_left_of_center = params['is_left_of_center']
heading = params['heading']
progress = params['progress']
steps = params['steps']
speed = params['speed']
steering_angle = abs(params['steering_angle'])
track_width = params['track_width']
is_offtrack = params['is_offtrack']
############### OPTIMAL X,Y,SPEED,TIME ################
# Get closest indexes for racing line (and distances to all points on racing line)
closest_index, second_closest_index = closest_2_racing_points_index(
racing_track, [x, y])
# Get optimal [x, y, speed, time] for closest and second closest index
optimals = racing_track[closest_index]
optimals_second = racing_track[second_closest_index]
if steps == 1:
self.first_racingpoint_index = closest_index
################ REWARD AND PUNISHMENT ################
reward = 1e-3
# Zero reward if off track ##
if is_offtrack is True:
return reward
# Zero reward if obviously wrong direction (e.g. spin)
direction_diff = racing_direction_diff(
optimals[0:2], optimals_second[0:2], [x, y], heading)
if direction_diff > 30:
return reward
# Reward if car goes close to optimal racing line
def get_distance_reward(threshold, distance, multiplier):
distance_reward = max(0, 1 - (distance / threshold))
return distance_reward * multiplier
DIST_THRESH = track_width * 0.5
dist = dist_to_racing_line(optimals[0:2], optimals_second[0:2], [x, y])
if (distance_from_center < 0.01 * track_width):
if closest_index in center_track:
reward += get_distance_reward(DIST_THRESH, dist, 1)
elif is_left_of_center:
if closest_index in left_track:
reward += get_distance_reward(DIST_THRESH, dist, 1)
if closest_index in strong_left:
reward += get_distance_reward(DIST_THRESH, dist, 5)
else:
if closest_index in right_track:
reward += get_distance_reward(DIST_THRESH, dist, 1)
if closest_index in strong_right:
reward += get_distance_reward(DIST_THRESH, dist, 5)
def get_speed_reward(ceiling, threshold, diff):
return ceiling - diff/threshold
# Reward if speed falls within optimal range
PENALTY_RATIO = 0.9
SPEED_DIFF_NO_REWARD = 1
speed_diff = abs(optimals[2]-speed)
if speed_diff > SPEED_DIFF_NO_REWARD:
return 1e-3
if closest_index in above_three_five:
if speed >= 3.5:
reward += get_speed_reward(0.5, SPEED_DIFF_NO_REWARD, speed_diff)
if steering_angle > 3:
reward *= PENALTY_RATIO
elif closest_index in above_three:
if speed >= 3:
reward += get_speed_reward(0.5, SPEED_DIFF_NO_REWARD, speed_diff)
if steering_angle > 8:
reward *= PENALTY_RATIO
elif closest_index in above_two_five:
if speed >= 2.5:
reward += get_speed_reward(0.8, SPEED_DIFF_NO_REWARD, speed_diff)
if steering_angle > 15:
reward *= PENALTY_RATIO
elif closest_index in above_two:
if speed >= 2:
reward += get_speed_reward(1, SPEED_DIFF_NO_REWARD, speed_diff)
else:
if speed < 2:
reward += get_speed_reward(3, SPEED_DIFF_NO_REWARD, speed_diff)
# Incentive for finishing the lap in less steps ##
REWARD_FOR_FASTEST_TIME = 2000 # should be adapted to track length and other rewards
TARGET_STEPS = 112
if progress == 100:
reward += REWARD_FOR_FASTEST_TIME / (steps - TARGET_STEPS)
#################### RETURN REWARD ####################
# Always return a float value
return float(reward)
reward_object = Reward()
定义奖励函数(参数): 返回reward_object.reward_function(params)