if语句没有注册变更

时间:2016-12-30 15:22:15

标签: python pygame

if语句没有意识到car1num和car2num正在改变而只是忽略它们会破坏汽车切换和转向汽车。

确定在game_loop()下面的代码中它表示" car2num = 2 "和" car1num = 1 "后来就说了

    for event in pygame.event.get():
        if crashed == 0:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RCTRL:
                    car2num += 1
                    if car2num == 4:
                        car2num = 1
                if event.key == pygame.K_LCTRL:
                    car1num += 1
                    if car1num == 4:
                        car1num = 1

所以每当你按下RCRTL时,car2num会上升,每次你按下LCRTL,car1num会上升甚至更晚( car1num car2num 都有一个) p>

        if car2num == 1:
            car2IMG = pygame.image.load('textures\car1.png')
        if car2num == 2:
            car2IMG = pygame.image.load('textures\car2.png')
        if car2num == 3:
            car2IMG = pygame.image.load('textures\car3.png')

那么car1 / 2num的数量决定了这辆车。

但它将始终保持原来设置为" car2num = 2 "和" car1num = 1 "。

感谢您提前提供帮助,并对不起来感到遗憾。

完整代码

import pygame
import os
from random import randint
from pygame.locals import *
import time
import random
gameDisplay = pygame.display.set_mode((800, 600), pygame.FULLSCREEN)
pygame.init()
infoObject = pygame.display.Info()
diswidth = 800
dishight = 600
cap = int(randint(0,4))

if cap == 1:
    caption = 'Light Drive - beat your friends'
elif cap == 2:
    caption = "Light Drive - don't crash"
elif cap == 3:
    caption = 'Light Drive - drive car drive!!!'
elif cap == 4:
    caption = 'Light Drive - BRUM BRUM'
else:
    caption = 'Light Drive - error'

car1IMG = pygame.image.load('textures\car1.png')
car2IMG = pygame.image.load('textures\car2.png')
car3IMG = pygame.image.load('textures\car3.png')
car4IMG = pygame.image.load('textures\car4.png')
car5IMG = pygame.image.load('textures\car5.png')
car0IMG = pygame.image.load('textures\car0.png')
onewinIMG = pygame.image.load('textures\Player-One.png')
twowinIMG = pygame.image.load('textures\Player-Two.png')
track = pygame.image.load('textures\TRACK.png')
def car1 (x1,y1):
    gameDisplay.blit(car1IMG,(x1,y1))
def car2 (x2,y2):
    gameDisplay.blit(car2IMG,(x2,y2))
def onewin ():
    gameDisplay.blit(onewinIMG,(((diswidth/2)-251),((dishight/2)-102.5)))
def twowin ():
    gameDisplay.blit(twowinIMG,(((diswidth/2)-267),((dishight/2)-102.5)))



def car3 (x3,y3):
    gameDisplay.blit(car3IMG,(x3,y3))

def car4 (xx4,yy4):
    gameDisplay.blit(car4IMG,(x4,y4))

def car5 (xx5,yy5):
    gameDisplay.blit(car5IMG,(x5,y5))



black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
blue = (0,0,255)
yellow = (255,255,0)
orange = (255,112,0)
purple = (255,0,255)
cyan = (0,255,255)
grey = (112,112,112)

pygame.display.set_caption(caption)

clock = pygame.time.Clock()

def text(texts, color):
    font = pygame.font.Font('fonts\HWAndy.ttf',72)
    wintext = font.render(texts, True, color)
    gameDisplay.blit(wintext, (((diswidth/2)-200), (dishight/2)))
    gameDisplay.blit(wintext,)


def game_loop():
    rotation2 = 1
    rotation1 = 1
    car2num = 2
    car1num = 1
    fullscreen = False
    x1 = ((diswidth * 0.5)-32)
    x2 = ((diswidth * 0.5)-32)
    y1 = ((76))
    y2 = ((44))
    x2c = 0
    x1c = 0
    y2c = 0
    y1c = 0
    car0 = 0
    car3 = 0
    car4 = 0
    car5 = 0
    y3 = (dishight + 300)
    y4 = (dishight + 300)
    y5 = (dishight + 300)
    y0 = (dishight + 300)
    car1IMG = pygame.image.load('textures\car1.png')
    car2IMG = pygame.image.load('textures\car2.png')
    crashed = 0
    gameExit = False
    winner = 0
    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if crashed == 0:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RCTRL:
                        **car2num += 1**
                    if event.key == pygame.K_LCTRL:
                        **car1num += 1**
                    if event.key == pygame.K_LEFT:
                        x2c = (0-(diswidth * 0.005))
                        rotation2 = 3
                    if event.key == pygame.K_RIGHT:
                        x2c = (diswidth * 0.005)
                        rotation2 = 1
                    if event.key == pygame.K_UP:
                        y2c = (0-(dishight * 0.005))
                        rotation2 = 0
                    if event.key == pygame.K_DOWN:
                        y2c = (dishight * 0.005)
                        rotation2 = 2
                    if event.key == pygame.K_a:
                        x1c = (0-(diswidth * 0.005))
                        rotation1 = 3
                    if event.key == pygame.K_d:
                        x1c = (diswidth * 0.005)
                        rotation1 = 1
                    if event.key == pygame.K_w:
                        y1c = (0-(dishight * 0.005))
                        rotation1 = 0
                    if event.key == pygame.K_s:
                        y1c = (dishight * 0.005)
                        rotation1 = 2
                    if event.key == pygame.K_ESCAPE:
                        gameExit = True
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT:
                        x2c = 0
                    if event.key == pygame.K_RIGHT:
                        x2c = 0
                    if event.key == pygame.K_UP:
                        y2c = 0
                    if event.key == pygame.K_DOWN:
                        y2c = 0
                    if event.key == pygame.K_a:
                        x1c = 0
                    if event.key == pygame.K_d:
                        x1c = 0
                    if event.key == pygame.K_w:
                        y1c = 0
                    if event.key == pygame.K_s:
                        y1c = 0
        car = random.randrange(0,100)
        if car == 0:
            car0 = 1
        if car == 1:
            car3 = 1
        if car == 2:
            car4 = 1
        if car == 3:
            car5 = 1

        if crashed == 0:
            x2 = x2 + x2c
            x1 = x1 + x1c
            y2 = y2 + y2c
            y1 = y1 + y1c
        gameDisplay.fill(grey)
        gameDisplay.blit(track,(0,0))

        if crashed < 60 and crashed > 0:
            crashed = (crashed + 1)
        **if rotation2 == 0:
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3.png')
        if rotation2 == 1:
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1 right.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2 right.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3 right.png')
        if rotation2 == 2:
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1 back.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2 back.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3 back.png')
        if rotation2 == 3:
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1 left.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2 left.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3 left.png')
        if rotation1 == 0:
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3.png')
        if rotation1 == 1:
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1 right.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2 right.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3 right.png')
        if rotation1 == 2:
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1 back.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2 back.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3 back.png')
        if rotation1 == 3:
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1 left.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2 left.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3 left.png')
        if x1 > (diswidth - 194):
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1crashed.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2crashed.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 2
        if x1 < 130:
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1crashed.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2crashed.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 2
        if y1 > (dishight - 94):
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1crashed.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2crashed.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 2
        if y1 < 30:
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1crashed.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2crashed.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 2
        if x2 > (diswidth - 194):
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1crashed.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2crashed.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 1
        if x2 < 130:
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1crashed.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2crashed.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 1
        if y2 > (dishight - 94):
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1crashed.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2crashed.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 1
        if y2 < 30:
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1crashed.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2crashed.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 1
        if x2 > 185 and x2 < 550 and y2 > 85 and y2 < 450:
            if car2num == 1:
                car2IMG = pygame.image.load('textures\car1crashed.png')
            if car2num == 2:
                car2IMG = pygame.image.load('textures\car2crashed.png')
            if car2num == 3:
                car2IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 1
        if x1 > 195 and x1 < 540 and y1 > 95 and y1 < 440:
            if car1num == 1:
                car1IMG = pygame.image.load('textures\car1crashed.png')
            if car1num == 2:
                car1IMG = pygame.image.load('textures\car2crashed.png')
            if car1num == 3:
                car1IMG = pygame.image.load('textures\car3crashed.png')
            if crashed == 0:
                crashed = 1
                winner = 2**


        car1(x1,y1)
        car2(x2,y2)
        if crashed == 60:
            crashed = 0
            gameDisplay.fill(black)
            if winner == 1:
                onewin()
                pygame.display.update()
                time.sleep(2)
            if winner == 2:
                twowin()
                pygame.display.update()
                time.sleep(2)
            winner = 0
            x1 = ((diswidth * 0.5)-32)
            x2 = ((diswidth * 0.5)-32)
            y1 = ((76))
            y2 = ((44))
            x2c = 0
            x1c = 0
            y2c = 0
            y1c = 0
            rotation2 = 1
            rotation1 = 1
            car1IMG = pygame.image.load('textures\car1.png')
            car2IMG = pygame.image.load('textures\car2.png')
        pygame.display.update()
        clock.tick(60)
game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:1)

您有

等功能
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": “1”,
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::12345678:root"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::AAAAAAA/*”
},
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin          Access Identity ABCDEFGHI”
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::AAAAAAA/*”
}
]
}

将blit全局图像def car1 (x1,y1): gameDisplay.blit(car1IMG,(x1,y1)) 。然后在car1IMG函数中编写game_loop,创建一个具有相同名称的 local 变量。因此,car1IMG = pygame.image.load('textures\car1.png')函数中的所有内容将始终使用局部变量而不是全局变量,除非您指定game_loop是全局变量。就这样:

car1IMG