0
votes

I'm fairly new to python, and I can't figure out whats wrong. I'm trying to get a bullet to appear and move, but it won't do that, it's being stuck to the player. My code is kind of messy, but here it is:

screen = pygame.display.set_mode((512,512))
pygame.display.set_caption('The Necromancer')

background = pygame.image.load('textures/background/background1.png')
titlecard = pygame.image.load('textures/title/titlecard0.png')
chars = pygame.image.load('textures/char/chardown1.png')
skeleton = pygame.image.load('textures/char/chardown2.png')

up = 0
down = 0
right = 0
left = 0
x = 64
y = 64
selected = 0
titletime = 0
level = 0
facing = 'down'
dir = "down"
ballx = x
bally = y
ballstage = 0
spacetimer = 0
ballon = 0

global ballon
global ballstage
global dir
global ballx
global bally
global facing
global x
global y

class char(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.imagedown = pygame.image.load('textures/char/chardown1.png').convert_alpha()
        self.imagedown = pygame.transform.scale(self.imagedown, (64,64))
        self.imageup = pygame.image.load('textures/char/charup1.png').convert_alpha()
        self.imageup = pygame.transform.scale(self.imageup, (64,64))
        self.imageleft = pygame.image.load('textures/char/charleft1.png').convert_alpha()
        self.imageleft = pygame.transform.scale(self.imageleft, (64,64))
        self.imageright = pygame.image.load('textures/char/charright1.png').convert_alpha()
        self.imageright = pygame.transform.scale(self.imageright, (64,64))

        self.rect = screen.get_rect()

    def draw(self):

        if facing == 'up':
            screen.blit(self.imageup, (x,y))
        if facing == 'down':
            screen.blit(self.imagedown, (x,y))
        if facing == 'left':
            screen.blit(self.imageleft, (x,y))
        if facing == 'right':
            screen.blit(self.imageright, (x,y))
        pygame.display.update()

class ball(pygame.sprite.Sprite):

    def __init__(self, surface, bax, bay):
        #pygame.sprite.Sprite.__init__(self)
        self.surface = surface
        self.x = bax
        self.y = bay
        self.image = pygame.image.load('textures/proj/purpball1.png').convert_alpha()
        self.image = pygame.transform.scale(self.image, (64,64))
        self.rect = screen.get_rect()
        return

    def update(self):

        ballstage += 1
        global ballstage

        if ballstage == 20:
            self.image = pygame.image.load('textures/proj/purpball1.png').convert_alpha()
        if ballstage == 40:
            self.image = pygame.image.load('textures/proj/purpball2.png').convert_alpha()
        if ballstage == 60:
            self.image = pygame.image.load('textures/proj/purpball3.png').convert_alpha()
        if ballstage == 80:
            self.image = pygame.image.load('textures/proj/purpball4.png').convert_alpha()
        self.image = pygame.transform.scale(self.image, (64,64))
        if ballstage == 100:
            ballstage = 0
        if dir == 'down':
            self.y += 1
        if dir == 'up':
            self.y -= 1
        if dir == 'right':
            self.x += 1
        if dir == 'left':
            self.x -= 1

        self.surface.blit(self.image, (self.x, self.y))
        return

ballGroup = []

pygame.display.update()

while True: #Update

    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    pygame.display.update()

    while level == 1:

        randx = random.randrange(0, 512, 64)
        randy = random.randrange(0, 512, 64)

        if dir == 'down':
            bally += 1
        if dir == 'up':
            bally -= 1
        if dir == 'right':
            ballx += 1
        if dir == 'left':
            ballx -= 1

        if x <= -1:
            x = 0

        if y <= -1:
            y = 0

        if x >= 449:
            x = 448

        if y >= 449:
            y = 448

        screen.blit(background, (0,0))
        #screen.blit(skeleton, (randx,randy))
        for ball in ballGroup:
            Ball = ball(screen, 64,64).update()
        char().draw()   
        skeleton = pygame.transform.scale(skeleton, (64, 64))
        background = pygame.transform.scale(background,(512,512))
        pygame.display.update()

        if left != 0:
            x += 1
            left -= 8
        if right != 0:
            x -= 1
            right -= 8
        if up != 0:
            y -= 1
            up -= 8
        if down != 0:
            y += 1
            down -= 8
        for event in pygame.event.get():

            if event.type == KEYDOWN:

                if event.key == K_d:

                    #char = pygame.image.load('textures/char/charright1.png')
                    facing = 'right'
                    left += 512

                if event.key == K_a:

                    facing = 'left'
                    #char = pygame.image.load('textures/char/charleft1.png')
                    right += 512

                if event.key == K_s:

                    facing = 'down'
                    #char = pygame.image.load('textures/char/chardown1.png')
                    down += 512

                if event.key == K_w:

                    facing = 'up'
                    #char = pygame.image.load('textures/char/charup1.png')
                    up += 512

                if event.key == K_SPACE:

                    if facing == 'down':
                        dir = 'down'
                    if facing == 'up':
                        dir = 'up'
                    if facing == 'left':
                        dir = 'left'
                    if facing == 'right':
                        dir = 'right'
                    #ballx = x
                    #bally = y
                    #spacetimer = 500
                    #ballon = 1
                    ballGroup.append(ball)

        if spacetimer != 0:
            spacetimer -= 1
            ballon = 1

        else:
            ballon = 0



    while level == 0:
    #title screen       
        titletime += 1
        screen.blit(titlecard, (0,0))               
        pygame.display.update()

        if selected == -1:
            selected = 0

        if selected == 0:
            titlecard = pygame.image.load('textures/title/titlecard0.png')

        if selected == 1:
            titlecard = pygame.image.load('textures/title/titlecard1.png')

        if selected == 2:
            titlecard = pygame.image.load('textures/title/titlecard2.png')

        if selected == 3:
            selected = 2

        if selected == 4:
            selected = 5

        for event in pygame.event.get():

            if event.type == KEYDOWN:

                if event.key == K_w:

                    selected -=1

                if event.key == K_s:

                    selected +=1

                if event.key == K_RETURN:

                    if selected == 0:

                        level = 1

                    if selected == 1:

                        titlecard = pygame.image.load('textures/title/credits.png')
                        selected = 4

                    if selected == 2:

                        sys.exit()

                    if selected == 5:

                        selected = 1

pygame.display.update()

if name == 'main': main()

1

1 Answers

0
votes

Sorry, but it's hard to help you with this problem because the code is a bit much and a little to messy. I cannot try it myself either without changing it, especially since you load many images that's on your computer. Try to make it more manageable so we easier can pinpoint the problem. You can follow these steps:

  1. Use functions to divide your code to smaller and more manageable chunks.
  2. Be consistent in your writing! Sometimes you have a new line between the if-statements and sometimes you don't. Try to put chunks of related code together.
  3. Make sure you give us the code necessary. Remove everything that's unnecessary, like the code you've commented out, and add necessary code as which modules you import. This makes it easier for us to test and look for errors.
  4. Your variables at the top is all global. You don't have to explicitly say they're global after you've defined them. Try use an IDLE as PyCharm (community version is free). You'll get notified of errors or redundancies.
  5. Follow PEP8 as long as it doesn't make the code less readable.

As for trying to answer your question. Nowhere in your code exists a bullet... Do you mean ball? In that case you do something peculiar which I've don't quite understand.

for ball in ballGroup:
    Ball = ball(screen, 64,64).update()

I think you meant to do ball.update(), because know you're creating new balls at position 64, 64 every game loop. This might be the problem. Also, name variables with lowercase and classes starting with uppercase. It's quite important to follow these conventions if you want others to be able to easily read and understand your code.

Here's a good tutorial to learn the basic mechanics and structure of making a pygame game.