我怎样才能让这艘船正确射击子弹?

时间:2015-10-03 01:44:39

标签: python python-2.7 pygame

我正在创建一个船舶游戏,你可以击落敌人并升级你的船。我没有做任何事情,因为我对子弹有问题。

在我们开始之前,我想指出我目前正在使用python 2.7和pygame 1.9。我知道有更高版本的pygame和python,但我开始写这个之前我知道有更高版本的pygame for python 3.4。

我还想指出,我是一个非常基本的编码员,并且还不了解一些高级的东西。

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

我不知道代码有什么问题,我重写了两次代码,但我无法解决问题,请帮忙。

1 个答案:

答案 0 :(得分: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