Python:Randrange
我正在做一场有6名防守队员的足球比赛。我把这个密码设置成随机的,让他们朝着四分卫移动 我想知道的是,是否有更好的方法来做到这一点。我知道必须有一种方法可以在没有那么多if语句的情况下循环,但我对python非常陌生 我只包括与玩家移动相关的部分,只是为了让代码更容易阅读Python:Randrange,python,loops,if-statement,pygame,Python,Loops,If Statement,Pygame,我正在做一场有6名防守队员的足球比赛。我把这个密码设置成随机的,让他们朝着四分卫移动 我想知道的是,是否有更好的方法来做到这一点。我知道必须有一种方法可以在没有那么多if语句的情况下循环,但我对python非常陌生 我只包括与玩家移动相关的部分,只是为了让代码更容易阅读 import pygame import random x = 215 y = 223 step = 50 frame_count = 0 side = True hike = True p1d1x = 265 p1d1y
import pygame
import random
x = 215
y = 223
step = 50
frame_count = 0
side = True
hike = True
p1d1x = 265
p1d1y = 174
p1d2x = 265
p1d2y = 224
p1d3x = 265
p1d3y = 274
p1d4x = 465
p1d4y = 174
p1d5x = 365
p1d5y = 224
p1d6x = 565
p1d6y = 274
p2d1x = 415
p2d1y = 174
p2d2x = 415
p2d2y = 224
p2d3x = 415
p2d3y = 274
p2d4x = 215
p2d4y = 174
p2d5x = 315
p2d5y = 224
p2d6x = 115
p2d6y = 274
(I draw all my players using the x and y variables above)
(I use my frame_count which is built into my counter or game timer to move players at an appropriate speed. The rest is all in my main loop)
frame_count += 1
defense = random.randrange(0,6,1)
if side == True and frame_count == 45:
if defense == 0:
if p1d1x > x:
p1d1x -= step
if p1d1y > y:
p1d1y -= step
if p1d1x < x:
p1d1x += step
if p1d1y < y:
p1d1y += step
elif defense == 1:
if p1d2x > x:
p1d2x -= step
if p1d2y > y:
p1d2y -= step
if p1d2x < x:
p1d2x += step
if p1d2y < y:
p1d2y += step
elif defense == 2:
if p1d3x > x:
p1d3x -= step
if p1d3y > y:
p1d3y -= step
if p1d3x < x:
p1d3x += step
if p1d3y < y:
p1d3y += step
elif defense == 3:
if p1d4x > x:
p1d4x -= step
if p1d4y > y:
p1d4y -= step
if p1d4x < x:
p1d4x += step
if p1d4y < y:
p1d4y += step
elif defense == 4:
if p1d5x > x:
p1d5x -= step
if p1d5y > y:
p1d5y -= step
if p1d5x < x:
p1d5x += step
if p1d5y < y:
p1d5y += step
elif defense == 5:
if p1d6x > x:
p1d6x -= step
if p1d6y > y:
p1d6y -= step
if p1d6x < x:
p1d6x += step
if p1d6y < y:
p1d6y += step
elif side == False and frame_count == 45:
if defense == 0:
if p2d1x > x:
p2d1x -= step
if p2d1y > y:
p2d1y -= step
if p2d1x < x:
p2d1x += step
if p2d1y < y:
p2d1y += step
elif defense == 1:
if p2d2x > x:
p2d2x -= step
if p2d2y > y:
p2d2y -= step
if p2d2x < x:
p2d2x += step
if p2d2y < y:
p2d2y += step
elif defense == 2:
if p2d3x > x:
p2d3x -= step
if p2d3y > y:
p2d3y -= step
if p2d3x < x:
p2d3x += step
if p2d3y < y:
p2d3y += step
elif defense == 3:
if p2d4x > x:
p2d4x -= step
if p2d4y > y:
p2d4y -= step
if p2d4x < x:
p2d4x += step
if p2d4y < y:
p2d4y += step
elif defense == 4:
if p2d5x > x:
p2d5x -= step
if p2d5y > y:
p2d5y -= step
if p2d5x < x:
p2d5x += step
if p2d5y < y:
p2d5y += step
elif defense == 5:
if p2d6x > x:
p2d6x -= step
if p2d6y > y:
p2d6y -= step
if p2d6x < x:
p2d6x += step
if p2d6y < y:
p2d6y += step
导入pygame
随机输入
x=215
y=223
步长=50
帧计数=0
侧=真
徒步旅行=真的
p1d1x=265
p1d1y=174
p1d2x=265
p1d2y=224
p1d3x=265
p1d3y=274
p1d4x=465
p1d4y=174
p1d5x=365
p1d5y=224
p1d6x=565
p1d6y=274
p2d1x=415
p2d1y=174
p2d2x=415
p2d2y=224
p2d3x=415
p2d3y=274
p2d4x=215
p2d4y=174
p2d5x=315
p2d5y=224
p2d6x=115
p2d6y=274
(我使用上面的x和y变量绘制所有玩家)
(我使用内置在计数器或游戏计时器中的帧计数以适当的速度移动玩家。其余的都在我的主循环中)
帧计数+=1
防御=随机。随机范围(0,6,1)
如果side==True且frame_count==45:
如果防御=0:
如果p1d1x>x:
p1d1x-=步骤
如果p1d1y>y:
p1d1y-=阶跃
如果p1d1xx:
p1d2x-=步进
如果p1d2y>y:
p1d2y-=阶跃
如果p1d2xx:
p1d3x-=步骤
如果p1d3y>y:
p1d3y-=阶跃
如果p1d3xx:
p1d4x-=步骤
如果p1d4y>y:
p1d4y-=步骤
如果p1d4xx:
p1d5x-=步进
如果p1d5y>y:
p1d5y-=阶跃
如果p1d5xx:
p1d6x-=步进
如果p1d6y>y:
p1d6y-=步进
如果p1d6xx:
p2d1x-=步进
如果p2d1y>y:
p2d1y-=步进
如果p2d1xx:
p2d2x-=步进
如果p2d2y>y:
p2d2y-=步进
如果p2d2xx:
p2d3x-=步进
如果p2d3y>y:
p2d3y-=阶跃
如果p2d3xx:
p2d4x-=步骤
如果p2d4y>y:
p2d4y-=步骤
如果p2d4xx:
p2d5x-=阶跃
如果p2d5y>y:
p2d5y-=阶跃
如果p2d5xx:
p2d6x-=步进
如果p2d6y>y:
p2d6y-=步进
如果p2d6x
这基本上就是在我将四分卫设置为球员移动的箭头键时移动我的电脑球员的一切。例如,你可以使用
字典来存储你的球员在两支球队中的位置-
team1 = {'p1d1':(265,174),'p1d2':(265,224),...}
与第2组相同-
team2 = {'p2d1':(415,174),'p2d2':(415,224),...}
然后可以循环字典键和项来创建变量,例如-
for player, position in team1:
#Do your logic to draw them
for player, position in team2:
#Do your logic to draw them
if side == True and frame_count == 45:
key_to_move = random.choice(list(team1.keys()))
(x1, y1) = team1[key_to_move]
if x1 > x:
x1 -= step
elif x1 < x:
x1 += step
if y1 > y:
y1 -= step
elif y1 < y:
y1 += step
team1[key_to_move] = (x1,y1)
.
.
.
#Same for team2
然后,您可以使用random.choice()
从字典中选择一个随机键,该键将是要移动的播放器,然后移动它,例如-
for player, position in team1:
#Do your logic to draw them
for player, position in team2:
#Do your logic to draw them
if side == True and frame_count == 45:
key_to_move = random.choice(list(team1.keys()))
(x1, y1) = team1[key_to_move]
if x1 > x:
x1 -= step
elif x1 < x:
x1 += step
if y1 > y:
y1 -= step
elif y1 < y:
y1 += step
team1[key_to_move] = (x1,y1)
.
.
.
#Same for team2
如果side==True且frame_count==45:
key-to-move=random.choice(列表(team1.keys()))
(x1,y1)=团队1[按键移动]
如果x1>x:
x1-=阶跃
elif x1y:
y1-=阶跃
elif y1
我稍微改变了逻辑,使用了if..elif
,因为有时候如果球员离四分卫太近,你可以在同一步中移动+=
和-=
,因为你会移动球员(p1d1x
在开始时大于x
),这样p1d1x
就会小于x,然后再把他移到x上面
虽然上面的代码也做同样的事情,但是在两个步骤中,你可能需要考虑保持一些数字,这样如果玩家在四分卫的某个范围(小范围)中不会发生移动。
使用元组列表代替每个坐标的变量。这将大大缩短代码的长度
编辑1
为了让它更短,我用了
编辑2
只是通过使用一个函数把它收紧了一点。我不知道