dimanche 9 août 2015

Python: Randrange

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.

Aucun commentaire:

Enregistrer un commentaire