python似乎忽略了我的if语句的一部分

时间:2018-09-03 09:57:19

标签: python if-statement

我有一个if语句,它有3个要求。

if  (-30 < distancey1_1 < 30) and (german_startx1 < 705) and (distance1_1 < 600) :               # is german 1 in position and are the russians in range (between 30 and minus 30 doesnt work)
    print(distancey1_1)
    print('1_shoots_1')
    shoot(bulletx1, bullety1, 10, 10, yellow)

即使不满足所有3个要求,它也会以某种方式运行此if语句。

我有时将此作为输出。基本上是承认忽略-30

1 shoots 1
-152.0
1 shoots 1
-152.0
1 shoots 1
-152.0
1 shoots 1
-152.0
1 shoots 1

这是一个愚蠢的小娱乐活动的一部分,目的是让敌人从大门口的那个场景中消失。

完整代码如下。它只是使正方形向侧面移动,其中一些会射击。

import pygame
import time
import random
import math

pygame.init()

display_width = 800
display_height= 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
grey=(169,169,169)
yellow = (246, 240, 55)

gameDisplay= pygame.display.set_mode((800,600))

pygame.display.set_caption('stalingrad V0.1')

clock = pygame.time.Clock()



def spawn_soldier(thingx,thingy, thingw, thingh, colour):
    pygame.draw.rect(gameDisplay, colour,[thingx, thingy, thingw, thingh])

def shoot(bulletx, bullety, bulletw, bulleth, colour):
    pygame.draw.rect(gameDisplay, colour,[bulletx, bullety, bulletw, bulleth])



def game_loop():

# soldier constants

russian_width= 20
russian_height= 20
russian_speed = 6


russian_startx1=-300
russian_startx2=-310
russian_startx3=-322
russian_startx4=-305
russian_startx5=-306
russian_startx6=-313


russian_starty1 =display_height/2
russian_starty2 =random.randrange(0, display_height)
russian_starty3 =random.randrange(0, display_height)
russian_starty4 =random.randrange(0, display_height)
russian_starty5 =random.randrange(0, display_height)




german_width=20
german_height=20
german_speed=-1

german_startx1=780
german_startx2=820
german_startx3=789
german_startx4=835
german_startx5=780
german_startx6=820
german_startx7=789
german_startx8=835





german_start1y=display_height/2
german_start2y=random.randrange(0, display_height)
german_start3y=random.randrange(0, display_height)
german_start4y=random.randrange(0, display_height)
german_start5y=random.randrange(0, display_height)
german_start6y=random.randrange(0, display_height)
german_start7y=random.randrange(0, display_height)
german_start8y=random.randrange(0, display_height)
german_start9y=random.randrange(0, display_height)
german_start10y=random.randrange(0, display_height)
german_start11y=random.randrange(0, display_height)
german_start12y=random.randrange(0, display_height)

bulletx1 = german_startx1 - 5
bullety1 = german_start1y - 5


germancount=0
russiancount=0
bullet_speed=-10

game_exit=False

soldier1=1

while not game_exit:
    gameDisplay.fill(white)

    ###
    #below = spawn germans
    ###

    spawn_soldier(german_startx1, german_start1y, german_width, german_height, grey)   # gerry 1

    spawn_soldier(german_startx2, german_start2y, german_width, german_height, grey)   # gerry 2

    spawn_soldier(german_startx3, german_start3y, german_width, german_height, grey)   # gerry 3

    spawn_soldier(german_startx4, german_start4y, german_width, german_height, grey)   # gerry 4


    ###
    # below = move germans into position
    ###


    if german_startx1 > 700:
        german_startx1 += german_speed

    if german_startx2 > 700:
        german_startx2 += german_speed

    if german_startx3 > 700:
        german_startx3 += german_speed

    if german_startx4 > 700:
        german_startx4 += german_speed


        #####
        # below = spawn russians and move them right
        ####


    spawn_soldier(russian_startx1, russian_starty1, russian_width, russian_height, red) # rusky 1
    if russian_startx1 < display_width+90:       #russian moves to the right until off screen
        russian_startx1 += russian_speed         
    if russian_startx1 > 810:       # once its gone beyond the screen give me a fresh russian from the left    
        russian_starty1 = random.randrange(0, display_height)
        russian_startx1 = -30
        print('reset rusky 1')

    spawn_soldier(russian_startx2, russian_starty2, russian_width, russian_height, red) # rusky 2
    if russian_startx2 < display_width+90:       
        russian_startx2 += russian_speed         
    if russian_startx2 > 830:       
        russian_starty2 = random.randrange(0, display_height)
        russian_startx2 = -30
        print('reset rusky 2')

        ###
        # below = distance between each russian and each german. if touching german dies and respawns
        ###

    distancex1_1 = russian_startx1 - german_startx1
    distancey1_1 = russian_starty1 - german_start1y
    distance1_1 = math.sqrt((distancex1_1)**2+(distancey1_1)**2)
    if distance1_1 < 10:
        german_startx1= 780

    distancex1_2 = russian_startx1 - german_startx2
    distancey1_2 = russian_starty1 - german_start2y
    distance1_2 = math.sqrt((distancex1_2)**2+(distancey1_2)**2)
    if distance1_2 < 10:
        german_startx2= 820

    distancex1_3 = russian_startx1 - german_startx3
    distancey1_3 = russian_starty1 - german_start3y
    distance1_3 = math.sqrt((distancex1_3)**2+(distancey1_3)**2)
    if distance1_3 < 10:
        german_startx3= 789

    distancex1_4 = russian_startx1 - german_startx4
    distancey1_4 = russian_starty1 - german_start4y
    distance1_4 = math.sqrt((distancex1_4)**2+(distancey1_4)**2)
    if distance1_4 < 10:
        german_startx4= 780

    distancex1_5 = russian_startx1 - german_startx5
    distancey1_5 = russian_starty1 - german_start5y
    distance1_5 = math.sqrt((distancex1_5)**2+(distancey1_5)**2)
    if distance1_5 < 10:
        german_startx5= 7999

    distancex1_6 = russian_startx1 - german_startx6
    distancey1_6 = russian_starty1 - german_start6y
    distance1_6 = math.sqrt((distancex1_6)**2+(distancey1_6)**2)
    if distance1_6 < 10:
        german_startx6= 789

    distancex2_1 = russian_startx2 - german_startx1
    distancey2_1 = russian_starty2 - german_start1y
    distance2_1 = math.sqrt((distancex2_1)**2+(distancey2_1)**2)
    if distance2_1 < 10:
        german_startx1= 780

    distancex2_2 = russian_startx2 - german_startx2
    distancey2_2 = russian_starty2 - german_start2y
    distance2_2 = math.sqrt((distancex2_2)**2+(distancey2_2)**2)
    if distance2_2 < 10:
        german_startx2= 820

    distancex2_3 = russian_startx2 - german_startx3
    distancey2_3 = russian_starty2 - german_start3y
    distance2_3 = math.sqrt((distancex2_3)**2+(distancey2_3)**2)
    if distance2_3 < 10:
        german_startx3= 789

    distancex2_4 = russian_startx2 - german_startx4
    distancey2_4 = russian_starty2 - german_start4y
    distance2_4 = math.sqrt((distancex2_4)**2+(distancey2_4)**2)
    if distance2_4 < 10:
        german_startx4= 780

    distancex2_5 = russian_startx2 - german_startx5
    distancey2_5 = russian_starty2 - german_start5y
    distance2_5 = math.sqrt((distancex2_5)**2+(distancey2_5)**2)
    if distance2_5 < 10:
        german_startx5= 7999

    distancex1_6 = russian_startx1 - german_startx6
    distancey1_6 = russian_starty1 - german_start6y
    distance1_6 = math.sqrt((distancex1_6)**2+(distancey1_6)**2)

    if distance1_6 < 10:
        german_startx6= 789

        ###
        # below = shooting
        ###

    print(distancey1_1)

    if  (-30 < distancey1_1 < 30) and (german_startx1 < 705) and (distance1_1 < 600) :               # is german 1 in position and are the russians in range (between 30 and minus 30 doesnt work)
        print(distancey1_1)
        print('1_shoots_1')
        shoot(bulletx1, bullety1, 10, 10, yellow)

        bulletx1 += bullet_speed
        if bulletx1 < -100:                                                               #firing again
            bulletx1 = german_startx1 -5
        if distancey1_1 > 0:                                                              # vertical aiming
            print('aim up')
            bullety1 += 1
        if distancey1_1< 0:
            bullety1 += -1
            print('aim down')

        disx1_1 = russian_startx1 - bulletx1                                                #distance between bullet and target
        disy1_1 = russian_starty1 - bullety1
        if -10< disx1_1 < 10 and -10< disy1_1 <10:
            bulletx1 =-10
            russian_startx1 = -30
            russian_starty1 =random.randrange(0, display_height)




#####change numbers
    if german_startx1 < 705 and distance1_2 < 600 and  distancey1_2 <30 :               # is german 1 in position and are the russians in range

        print('1 shoots 1')
        shoot(bulletx1, german_start1y, 10, 10, yellow)

        bulletx1 += bullet_speed
        if bulletx1 < 100:
            bulletx1 = german_startx1 - 5










    pygame.display.update()
    clock.tick(40)


game_loop()
pygame.quit()
quit()

我怎样才能让它仅在满足所有3个条件时才执行if语句?

1 个答案:

答案 0 :(得分:0)

听起来不礼貌,但 仍然会导致故障

>>>(-30 < -158 < 30 ) and True and True
>>>False