Python: Randrang

2024-04-19 23:28:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个有6名防守队员的足球比赛。我把这个密码设置成随机的让他们都朝四分卫移动。在

我只想知道有没有更好的方法。我知道必须有一种方法可以在没有那么多if语句的情况下循环这个过程,但我对python还是很陌生的。在

我只包含了与玩家移动相关的部分,只是为了让代码更容易阅读。在

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

这基本上就是所有能移动我的电脑玩家的东西,而我的四分卫被设置为箭头键来移动玩家。在


Tags: ifstepcountframeelifdefensep1d2xp1d4y
2条回答

你可以使用dictionary来存储你的球员在两个队中的位置,例如-

team1 = {'p1d1':(265,174),'p1d2':(265,224),...}

team2-

^{pr2}$

然后可以循环字典键和项来创建变量,例如-

for player, position in team1:
    #Do your logic to draw them
for player, position in team2:
    #Do your logic to draw them

然后您可以使用random.choice()从字典中选择一个随机键,它将是要移动的播放器,然后移动它,例如-

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

我稍微改变了一下逻辑使用if..elif,因为有时候如果玩家离四分卫太近,你可以在同一步中移动+=和{},因为你会移动玩家(p1d1x在开始时大于x),这样{}会小于x,然后再次将他移动到x之上

虽然上面的代码也做了同样的事情,但是在两个步骤中,你可以考虑保留一些数字,这样当球员在四分卫的某个范围内(小范围内),就不会发生移动。在

对每个坐标使用元组列表,而不是变量。这将大大缩短代码的长度。在

编辑1
为了使它更短,我使用了ternary expressions。在

编辑2
用一个函数把它再紧一点。我不认为它能变短,但我会看看我能做些什么;)

import pygame
from random import randrange

x = 215 
y = 223
step = 50
frame_count = 0
side = True
hike = True

p1 = [(265, 274), (265, 224), (265, 274), (465, 174), (365, 224), (565, 274)]
p2 = [(415, 174), (415, 224), (415, 274), (215, 174), (315, 224), (115, 274)]

frame_count += 1

def move(p):
    defense = randrange(len(p))
    if frame_count == 45:
        if p[defense][0] != x:
            p[defense][0] += step if p[defense][0] < x else -step
        if p[defense][1] != y:
            p[defense][1] += step if p[defense][1] < y else -step

move(p1 if side else p2)

相关问题 更多 >