在角色位置创建子弹

时间:2017-08-05 01:46:16

标签: python pygame

大约3个小时我一直试图创建一个从角色位置沿特定方向前进的子弹。我试图让它离开带有变量BX的角色,这里使用的是:

gameDisplay.blit(Eball,(BX + 25, y))

虽然我知道自己想要做什么,但我得到的最好的是弹出的子弹而不会使程序崩溃,但它不会向前移动,因为它与角色保持25码的距离。为了计算子弹运动的时间,我尝试使用while语句说只要所述变量[e]小于20,就运行此代码。 game_loop()的每个循环都将1添加到[e]。

while e < 20 :
    e = e + 1

[e]在尝试使用子弹功能[energyBall]的每个keydown上重置为0,并将子弹的方向设置为True,如此处所示。

elif event.key == pygame.K_d:
            e = 0
            right = True
            energyBall()

if (right == True):
        gameDisplay.blit(Eball,(BX + 25, y))
        pygame.display.update()

这里可以看到完整代码的上下文(感谢您帮助我):

import pygame
import random
import time
import math
import cmath
import sys
sys.setrecursionlimit(5000)


width=800
height=750

global x
x = 0
global y
y = 0
global e
e = 0
global x_move
x_move = 0

global y_move
y_move = 0

global enemyintx
enemyintx = random.randint(0,36)
global enemyinty
enemyinty = random.randint(0,24)
global enemyint2x
enemyint2x = random.randint(0,36)
global enemyint2y
enemyint2y = random.randint(0,24)
global enemy_x
enemy_x = 500
global enemy_y
enemy_y = 500
global enemy2_x
enemy2_x = 500
global enemy2_y
enemy2_y = 500


global right
right = False
global left
left = False
global up
up = False
global down
down = False 

global doorlocx
doorlocx = 0
global doorlocy
doorlocy = 0
global door_x
door_x = 0
global door_y
door_y = 0





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


pygame.init()
gameDisplay = pygame.display.set_mode((width,height))
pygame.display.set_caption('Encounterer V1.0')
clock = pygame.time.Clock()

plr = pygame.image.load('player.png')
door = pygame.image.load('door.png')
enemy = pygame.image.load('enemy.png')
enemy2 = pygame.image.load('enemy.png')
enemyfight = pygame.image.load('enemyfight.png')
grid = pygame.image.load('backgroundgrid.png')

GUI = pygame.image.load('GUI.png')
attbut = pygame.image.load('attbutton.png')
attbut2 = pygame.image.load('attbutton2.png')
potbut = pygame.image.load('potbutton.png')
potbut2 = pygame.image.load('potbutton2.png')
spebut = pygame.image.load('spebutton.png')
spebut2 = pygame.image.load('spebutton2.png')

Eball = pygame.image.load('energyball.png')

FBG = pygame.image.load('fightBG.png')
base = pygame.image.load('sphere.png')
cover = pygame.image.load('cover.png')

global doorintx
doorintx = random.randint(1,31)
global doorinty
doorinty = random.randint(1,23)




def door_round():
    global doorlocx
    global doorlocy


    doorlocx = doorintx * 25
    doorlocy = doorinty * 25


def enemy_round():
    global enemy_x
    global enemy_y
    global enemy2_x
    global enemy2_y

    enemy_x = enemyintx * 25
    enemy_y = enemyinty * 25

    enemy2_x = enemyint2x * 25
    enemy2_y = enemyint2y * 25


def enemy_move():
    global enemy_x
    global enemy_y
    global move_chance

    move_chance = random.randint(0,12)

    if enemy_x > 775: 
        enemy_x = enemy_x - 25

    if enemy_x < 25:
        enemy_x = enemy_x + 25

    if enemy_y > 575:
        enemy_y = enemy_y - 25

    if enemy_y < 25:
        enemy_y = enemy_y +25

    if (move_chance == 0):
        enemy_x = enemy_x + 25

    elif (move_chance == 1):
        enemy_x = enemy_x - 25

    elif (move_chance == 2):
        enemy_y = enemy_y + 25

    elif (move_chance == 3):
        enemy_y = enemy_y - 25

def enemy2_move():
    global enemy2_x
    global enemy2_y
    global move_chance2

    move_chance2 = random.randint(0,12)

    if enemy2_x > 775: 
        enemy2_x = enemy2_x - 25

    if enemy2_x < 25:
        enemy2_x = enemy2_x + 25

    if enemy2_y > 575:
        enemy2_y = enemy2_y - 25

    if enemy2_y < 25:
        enemy2_y = enemy2_y +25

    if (move_chance2 == 0):
        enemy2_x = enemy2_x + 25

    elif (move_chance2 == 1):
        enemy2_x = enemy2_x - 25

    elif (move_chance2 == 2):
        enemy2_y = enemy2_y + 25

    elif (move_chance2 == 3):
        enemy2_y = enemy2_y - 25





def player(x,y):
    gameDisplay.blit(plr,(x,y))

def energyBall(self):
    global e
    global right
    global left
    global down
    global up
    global x
    global y
    global BX
    global BY

    while e < 20 :
        e = e + 1
        if (right == True):
            gameDisplay.blit(Eball,(BX + 25, y))
            pygame.display.update()

        elif (left == True):
            gameDisplay.blit(Eball,(BX - 25, y))
            pygame.display.update()



        elif (down == True):
            gameDisplay.blit(Eball,(x, BY + 25))
            pygame.display.update()


        elif (up == True):
            gameDisplay.blit(Eball,(x, BY - 25))
            pygame.display.update()
        main_loop() 
        energyBall()





def mouseloc():
    mouse = pygame.mouse.get_pos()
    if 13+99 > mouse[0] > 13 and 613 + 19 > mouse[1] > 613:
        gameDisplay.blit(attbut2,(13,613))

    elif 121+99 > mouse[0] > 121 and 613 + 19 > mouse[1] > 613:
        gameDisplay.blit(potbut2,(121,613))

    elif 229+99 > mouse[0] > 121 and 613 + 19 > mouse[1] > 613:
        gameDisplay.blit(spebut2,(229,613))



def game_loop():
    door_round()
    enemy_round()
    enemy_move()
    enemy2_move()
    x_move = 0
    y_move = 0
    global x
    x = 50
    global y
    y = 50


    crashed = False

    while not crashed:
        global e

        e = e + 1
        print(e)
        global BX
        BX = x
        global BY
        BY = y
        global doorlocx
        global doorlocy
        global enemy_x
        global enemy_y
        global enemy2_x
        global enemy2_y
        global right
        global left
        global down
        global up



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

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_move = -25

            elif event.key == pygame.K_RIGHT:
                x_move = 25

            elif event.key == pygame.K_DOWN:
                y_move = 25

            elif event.key == pygame.K_UP:
                y_move = -25


            elif event.key == pygame.K_d:
                e = 0
                right = True
                energyBall()

            elif event.key == pygame.K_a:
                e = 0
                left = True
                energyBall()

            elif event.key == pygame.K_s:
                e = 0
                down = True
                energyBall()

            elif event.key == pygame.K_w:
                e = 0
                up = True
                energyBall()



        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_move = 0


            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_move = 0


        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_a:
                BX = x
                gameDisplay.blit(cover,(BX,BY))
                pygame.display.update()



            if event.key == pygame.K_w or event.key == pygame.K_s:
                BY = y
                gameDisplay.blit(cover,(BX,BY))
                pygame.display.update()




        if x > width or x < 0:
            x = 0

        if y > 575 or y < 0:
            y = 0



        if x == doorlocx and y == doorlocy:

            global doorintx
            doorintx = random.randint(0,31)
            global doorinty
            doorinty = random.randint(0,23)

            global enemyintx
            enemyintx = random.randint(0,36)
            global enemyinty
            enemyinty = random.randint(0,24)
            global enemyint2x
            enemyint2x = random.randint(0,36)
            global enemyint2y
            enemyint2y = random.randint(0,24)

            enemy_x = enemyintx * 25
            enemy_y = enemyinty * 25

            enemy2_x = enemyint2x * 25
            enemy2_y = enemyint2y * 25

            doorlocx = doorintx * 25
            doorlocy = doorinty * 25

        if x == enemy_x and y == enemy_y or x == enemy2_x and y == enemy2_y :
            print(" CONTACT ")
            fight()


        x += x_move
        y += y_move
        enemy_move()
        enemy2_move()
        gameDisplay.fill(black)
        gameDisplay.blit(grid,(0,0))

        gameDisplay.blit(GUI,(0,600))
        gameDisplay.blit(attbut,(13,613))
        gameDisplay.blit(potbut,(121,613))
        gameDisplay.blit(spebut,(229,613))
        mouseloc()
        gameDisplay.blit(door,(doorlocx,doorlocy))
        gameDisplay.blit(enemy,(enemy_x,enemy_y))
        gameDisplay.blit(enemy2,(enemy2_x,enemy2_y))
        player(x,y)
        pygame.display.update()
        clock.tick(10)




game_loop()  
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

我可能真的很蠢,但对我来说,似乎你总是给出显示球的相同位置。

您的变量BX(或所有主要的BY)在看似打印函数调用的情况下被添加25(或减去),但您不保存结果。

你有类似的东西:

while e < 20 :  # 20 iterations without any external code getting called ?
    e = e + 1
    if (right == True):  # When ball goes right.
        gameDisplay.blit(Eball,(BX + 25, y))  # Display ball at current location + 25
        pygame.display.update()  # Now we display, BX isn't worth the same anymore.

    elif (left == True):
        gameDisplay.blit(Eball,(BX - 25, y))
        pygame.display.update()

您可能想要做的是:

while e < 20 :  # 20 iterations without any external code getting called ?
    e = e + 1
    if (right == True):  # When ball goes right.
        BX += 25  # BX gets augmented.
        gameDisplay.blit(Eball,(BX, y))  # Display ball at current augmented location.
        pygame.display.update()  # Now we display, BX is still worth the initial BX.

    elif (left == True):
        BX -= 25  # Left means bx needs to be lowered
        gameDisplay.blit(Eball,(BX, y)) # Display ball at current lowered location.
        pygame.display.update()

等等,代码中的任何地方。 另外,偷看会说有这么多变量是全局的虽然它在python中没什么用,但你可以尝试使用更多的类,就像一个类能量球可能会很好。

编辑类似的东西(可能不符合pep8):

class EnergyBall(object):
    def __init__(self, initx, inity, speed, image, gdf):
        self.P = [initx, inity]
        self.v = speed
        self.image = image
        self.display = gdf  # The function in the module you see to be using.

    def frame(self, orientation, axis):
        """
        Orientation should be a 1 or -1 valued integer.
        Axis 0 = x, 1 = y
        returns new ball position for collision check or something.
        """
        # Set new position given speed, axis and orientation.
        self.P[axis] += self.v * orientation

        # Set the display using the function passed as parameter earlier.
        self.display(self.image, self.P[0], self.P[1])
        return self.P

我会让你弄清楚如何使用这样的东西,好像根据你的实现可能会有所不同。传递显示模块功能的手柄是一个很好的提示,允许对象自己处理。 (最后一个参数);)

注意:我没有在这台机器上运行python,我所做的代码可能完全没有了。 (或部分)(或不适合您的python实现)

PS:虽然这绝对只是我的意见,但我绝对建议你尝试使用另一种语言而不是python来开始编程。它让你有可能快速尝试很多东西,但你最终做了尝试重试,有时忘记删除未工作的解决方案以恢复到初始状态。 (只是一个可能的问题,我没有开始尝试重试程序本身是错误的。)

这对您目前的情况意味着您需要对代码进行一些认真的清理。您添加了没有理由存在的函数调用等。

如果您真的想使用这种原型方法进行编程,我建议您使用像Git(https://git-scm.com/)这样的版本控制系统,以便轻松访问以前版本中代码的外观以及与可以帮助您的人轻松分享。

现在,你可以在工作的第一个小时后去,应用我提到的简单补丁,它会起作用。在这里,你正在讨论你之前自己放入代码中的东西并忘记删除(例如,循环,对main_loop的调用以及对EnergyBall的递归调用)。