2015-10-03 35 views
0

我正在製作一款可以擊落敵人並升級你的艦船的遊戲。我沒有做任何事情,因爲我的子彈有問題。我怎樣才能使船正確射擊子彈?

在我們開始之前,我想指出我目前使用python 2.7和pygame 1.9。我知道有pygame和python的更高版本,但是我開始寫這個之前我知道python 3.4有更高版本的pygame。

我還想指出,我是一個非常基本的編碼器,不知道一些先進的東西呢。

import pygame 
import time 

pygame.init() 

white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 

display_width = 800 
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('Destroyer') 

clock = pygame.time.Clock() 
FPS = 60 

font = pygame.font.SysFont(None, 25) 

shipW = 69 
shipH = 88 

#~~~~~~~~~~~~~~~~~~~Other Functions~~~~~~~~~~~~~~~~~# 



#~~~~~~~~~~~~~~~~~~~~~~The Game~~~~~~~~~~~~~~~~~~~~~# 

def game_loop(): 

    shipImg = pygame.image.load('ship1.png') 

    face = 1 
    #The direction that the ship is facing. 
    #1 = North, 2 = East, 3 = South, 4 = West 

    x = (display_width * 0.45)#location of ship - x axis 
    y = (display_height * 0.45) #location of ship - y axis 

    x_change = 0 #change in location of ship - x axis 
    y_change = 0 #change in locations of ship - y axis 

    #Start of bullets 

    #1 

    lGun = x + 9 #location of left gun 
    rGun = x + 59 #location of right gun 

    yBul = 0 #change in location of bullet - y axis 
    xBul = 0 #change in loaction of bullet - x axis 

    fire = 0       #this will record the direction of ship when bullet is fired 

    #End of bullets 

    ybul_change = 0 
    xbul_change = 0 

    gameExit = False 

    while not gameExit: 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT:  #the next four lines are for rotating the ship left 
        shipImg = pygame.transform.rotate(shipImg,90) 
        face -= 1 
        face = (face - 1) % 4 
        face += 1 
       elif event.key == pygame.K_RIGHT: #the next four lines are for rotating the ship right 
        shipImg = pygame.transform.rotate(shipImg,-90) 
        face -= 1 
        face = (face + 1) % 4 
        face += 1 
       elif event.key == pygame.K_UP: 
        if face == 1: 
         y_change = -5 
         x_change = 0 
        elif face == 2: 
         x_change = 5 
         y_change = 0 
        elif face == 3: 
         y_change = 5 
         x_change = 0 
        elif face == 4: 
         x_change = -5 
         y_change = 0 
       elif event.key == pygame.K_DOWN: 
        if face == 1: 
         y_change = 5 
         x_change = 0 
        elif face == 2: 
         x_change = -5 
         y_change = 0 
        elif face == 3: 
         y_change = -5 
         x_change = 0 
        elif face == 4: 
         x_change = 5 
         y_change = 0 
       elif event.key == pygame.K_s: 
        y_change = 0 
        x_change = 0 
       elif event.key == pygame.K_PAGEUP: 
        if face == 1: 
         x_change = -5 
         y_change = 0 
        elif face == 2: 
         y_change = -5 
         x_change = 0 
        elif face == 3: 
         x_change = 5 
         y_change = 0 
        elif face == 4: 
         y_change = 5 
         x_change = 0 
       elif event.key == pygame.K_PAGEDOWN: 
        if face == 1: 
         x_change = 5 
         y_change = 0 
        elif face == 2: 
         y_change = 5 
         x_change = 0 
        elif face == 3: 
         x_change = -5 
         y_change = 0 
        elif face == 4: 
         y_change = -5 
         x_change = 0 
       elif event.key == pygame.K_f: 
        fire = face     #record direction of fire 
        if fire == 1 or fire == 3: #if facing north or south 
         xbul_change = 0   #no movement - x axis 
         xBul = 0    #not facing east or west, location is 0 
         lGun = x + 9   #location of left gun - x axis 
         rGun = x + 59   #location of right gun - x axis 
         if fire == 1:   #if facing north 
          ybul_change = -5 #bullets go up 
          yBul = y - 10  #location of bullet when firing 
         else: 
          ybul_change = 5  #bullets go down 
          yBul = y + 88  #location of bullet when firing - y axis 
        else: #facing east or west 
         ybul_change = 0   #no movement - y axis 
         yBul = 0    #not facing north or south,location is 0 
         lGun = y + 59   #location of left gun - y axis 
         rGun = y + 9   #location of right gun - y axis 
         if fire == 2:   #if facing west 
          xbul_change = 5  #bullets are supposed to move right but they actually move down 
          xBul = x + 88  #location of bullets, i don't know what is wrong with this but it doesn't work 
         else: 
          xbul_change = -5 #bullets are supposed to move left but they actually move up 
          xBul = x - 10  #location of bullets, i don't know what is wrong with this but it doesn't work 



     gameDisplay.fill(green) 

     gameDisplay.blit(shipImg,(x,y)) 

     if fire == 1 or fire == 3:         #if facing north or south 
      pygame.draw.rect(gameDisplay, red, [lGun, yBul, 2, 10]) 
      pygame.draw.rect(gameDisplay, red, [rGun, yBul, 2, 10]) 
     elif fire == 2 or fire == 4:         #if facing east or west 
      pygame.draw.rect(gameDisplay, blue, [lGun, xBul, 10, 2]) #the bullets are not drawing in the correct position and i don't know why 
      pygame.draw.rect(gameDisplay, blue, [rGun, xBul, 10, 2]) #the bullets are not drawing in the correct position and i don't know why 


     yBul += ybul_change    #add the change in location of bullet to current location - y axis 
     xBul += xbul_change    #add the change in location of bullet to current location - x axis 

     y += y_change     #add the change in location of the ship to the current location - y axis 
     x += x_change     #add the change in location of the ship to the current location - x axis 

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


game_loop() 
pygame.quit() 
quit() 

船將朝上或朝下時,從兩個槍正確射擊子彈,然而,面對向左或向右時,子彈仍然彼此相鄰,而不是彼此喜歡槍頂也可以向上或向下移動而不是向左或向右移動。

這裏是我的船的鏈接:http://tinypic.com/r/2zszjv4/8

如果其他鏈接不起作用:http://i61.tinypic.com/2zszjv4.jpg

我不知道什麼是錯的代碼,我重寫了代碼兩次,但我無法弄清楚問題,請幫忙。

回答

0

問題在於以下部分。 只是交換xBul的和lGun的位置,你會好的

elif fire == 2 or fire == 4:         #if facing east or west 
     pygame.draw.rect(gameDisplay, blue, [ xBul,lGun, 10, 2]) #the bullets are not drawing in the correct position and i don't know why 
     pygame.draw.rect(gameDisplay, blue, [ xBul,rGun, 10, 2]) #the bullets are not drawing in the correct position and i don't know why 
+0

謝謝!我想直到凌晨1點才幫我想想,當我編碼的時候,它現在起作用了。 – Ruarri

+0

發生了這種情況。我能夠了解。 – aaveg