英文:
How to set ships randomly using pygame
问题
我想使用Pygame设置船只,就像图像中那样。但是我只从#right(在长循环中)获得坐标。
如何修复这个问题?
英文:
I want to set ships like in image using pygame.
However I got coordinates only from #right(in long for loop).
How can I fix this?
for i in range(1,6):
while True:
x,y = random.randrange(14,24,1),random.randrange(1,11,1)
angle = ship_angle[random.randrange(0,4,1)]
X,Y = x+i*math.cos(angle),y+i*math.sin(angle)
if (14 <= X <= 24) and (1 <= Y <= 11) and (not((X,Y) in ai_ships)):
#down
if (x == X) and (y < Y):
for d in range(i):
ai_ships.add((x*TILE,(y+d)*TILE))
break
#up
if (x == X) and (y > Y):
for d in range(i):
ai_ships.add((x*TILE,(y-d)*TILE))
break
#right
if (y == Y) and (x < X):
for d in range(i):
ai_ships.add(((x+d)*TILE,y*TILE))
break
#left
if (y == Y) and (x > X):
for d in range(i):
ai_ships.add(((x-d)*TILE,y*TILE))
break
else:
continue
答案1
得分: 1
I don't know if this is the only problem, but math.cos(), sin()
uses radians, so you want to use, for example:
angle = math.radians(ship_angle[random.randrange(0,4,1)])
# ^^^^^^^^^^^^ Add this
X,Y = x+i*math.cos(angle),y+i*math.sin(angle)
This will convert your degrees to radians before calling the sin()
and cos()
functions.
英文:
I don't know if this is the only problem, but math.cos(), sin()
uses radians, so you want to use, for example:
angle = math.radians(ship_angle[random.randrange(0,4,1)])
# ^^^^^^^^^^^^ Add this
X,Y = x+i*math.cos(angle),y+i*math.sin(angle)
This will convert your degrees to radians before calling the sin()
and cos()
functions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论