2016-12-30 94 views
-5

if語句沒有意識到car1num和car2num的變化,只是忽略了它們打破了汽車的轉換和轉向。if語句沒有註冊變量

代碼略低於game_loop OK()它說: 「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時間上升,甚至更晚(有一兩個car1numcar2num

 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() 
+5

我有*絕對*不知道你想要什麼或你的問題是什麼。你的描述和代碼(轉儲)都不能解釋什麼是錯的 – UnholySheep

+3

你確實需要提供一個[MCVE]並清楚地說明預期行爲與真實行爲之間的差異。否則,沒有人能夠幫助你。 – ImportanceOfBeingErnest

+1

你有幾個downvotes,因爲你的問題目前很難理解。這不是個人的,它只是一個指標,這個特定的問題需要根據[問]指導方針進行編輯。您需要解釋代碼中的位置以及發生的情況。我們希望提供幫助,所以不要被貶低和評論所壓倒。衝電氣? :) –

回答

0

你具備的功能,如

def car1 (x1,y1): 
    gameDisplay.blit(car1IMG,(x1,y1)) 

將位塊傳輸的全球形象car1IMG。然後在您的game_loop函數中,您編寫了car1IMG = pygame.image.load('textures\car1.png'),它創建了一個本地變量具有相同的名稱。因此,除非您指定car1IMG是全局變量,否則game_loop函數中的所有內容都將使用局部變量而不是全局變量。因此:

def game_loop(): 
    gloabl car1IMG 
    ...