Reputation: 49
I'm making a football game that has 6 defense players. I have this code set to randomly make all of them move towards the quarterback.
All I'm wondering is if there is a better way to do this. I know there has to be a way to just loop this without so many if statements but I'm very new to python.
I'm only including parts that pertain to the players moving just to make the code a lot easier to read.
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
That's basically everything that moves my computer players while I have the quarterback set to the arrow keys for player movement.
Upvotes: 2
Views: 225
Reputation: 6597
Use a list of tuples instead of a variable for every coordinate. This will dramatically shorten the length of your code.
EDIT 1
To make it even shorter, I used ternary expressions.
EDIT 2
Just tightened it up a little bit more by using a function. I don't think it can get much shorter, but I'll see what I can do ;)
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)
Upvotes: 1
Reputation: 90889
You can use a dictionary
to store the positions of your players in both team, example -
team1 = {'p1d1':(265,174),'p1d2':(265,224),...}
Same for team2
-
team2 = {'p2d1':(415,174),'p2d2':(415,224),...}
Then you can loop over the dictionary keys and items to create the variables , Example -
for player, position in team1:
#Do your logic to draw them
for player, position in team2:
#Do your logic to draw them
Then you can use random.choice()
to select a random key from your dictionary, which would be the player to move, and move it , Example -
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
I changed the logic a bit to use if..elif
, because sometimes if the player is too close to the quarterback, you can end up moving +=
and -=
in the same step, since you would move the player (p1d1x
being greater than x
at starting) such that p1d1x
becomes less than x , and then again move him above x.
Though the above code also does same thing, but in two steps, you may want to consider keeping some number such that move does not happen if the player is in a certain range (small range) of the quarterback.
Upvotes: 0