Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I recently started following more or less a pygame tutorial series (https://www.youtube.com/playlist?list=PLzMcBGfZo4-lp3jAExUCewBfMx3UZFkh5) where, on the projectile episode (5th), I'm currently stuck because my projectile.draw(screen) doesn't work (TypeError: draw() missing 1 required positional argument: 'screen') like my player ones above who are similar and work and I don't know why. If I give a variable argument before screen (which is pygame.display.set_mode) it says the name isn't defined (even for self) and it works with a random integer for exemple but then I get another error inside the draw function where AttributeError: 'int' object has no attribute 'fbcount' but I already said self.fbcount = 0. I'm kinda new to python and I'm probably not understanding the basics of classes and OOP so I come here to get some help and maybe some advices on how to improve the code :x

Python
import pygame
from pygame.locals import *
pygame.init()
running = pygame.display.get_active()

#Screen settings
screen = pygame.display.set_mode((859, 561))
scene = pygame.image.load("background.jpg").convert()
scenewidth = screen.get_width()

#skins
r_stand = pygame.image.load("rstand.png")
l_stand = pygame.transform.flip(r_stand, True, False)
r_walk = [pygame.image.load("rwalk1.png"), pygame.image.load("rwalk2.png"), pygame.image.load("rwalk3.png"), pygame.image.load("rwalk4.png"), pygame.image.load("rwalk5.png"), pygame.image.load("rwalk6.png")]
l_walk = [pygame.image.load("lwalk1.png"), pygame.image.load("lwalk2.png"), pygame.image.load("lwalk3.png"), pygame.image.load("lwalk4.png"), pygame.image.load("lwalk5.png"), pygame.image.load("lwalk6.png")]
r_jump = [pygame.image.load("rjump1.png"), pygame.image.load("rjump2.png"), pygame.image.load("rjump3.png")]
l_jump = [pygame.image.load("ljump1.png"), pygame.image.load("ljump2.png"), pygame.image.load("ljump3.png")]
r_crouch = [pygame.image.load("rcrouch1.png"), pygame.image.load("rcrouch2.png"), pygame.image.load("rcrouch3.png")]
l_crouch = [pygame.image.load("lcrouch1.png"), pygame.image.load("lcrouch2.png"), pygame.image.load("lcrouch3.png")]
r_fireball = [pygame.image.load("rfireball1.jpg"), pygame.image.load("rfireball2.jpg"), pygame.image.load("rfireball3.jpg"), pygame.image.load("rfireball4.jpg"), pygame.image.load("rfireball5.jpg"), pygame.image.load("rfireball6.jpg"), pygame.image.load("rfireball7.jpg"), pygame.image.load("rfireball8.jpg"), pygame.image.load("rfireball9.jpg"), pygame.image.load("rfireball10.jpg")]
l_fireball = [pygame.image.load("lfireball1.jpg"), pygame.image.load("lfireball2.jpg"), pygame.image.load("lfireball3.jpg"), pygame.image.load("lfireball4.jpg"), pygame.image.load("lfireball5.jpg"), pygame.image.load("lfireball6.jpg"), pygame.image.load("lfireball7.jpg"), pygame.image.load("lfireball8.jpg"), pygame.image.load("lfireball9.jpg"), pygame.image.load("lfireball10.jpg")]

class player1():
    def __init__(self):
        self.x = 79
        self.y = 432
        self.speed = 0.1
        self.width = 15
        self.right = False
        self.left = False
        self.jumping = False
        self.jumpvar = 100
        self.jumpcount = 0
        self.walking = False
        self.walkCount = 0
        self.crouching = False
        self.crouchcount = 0
        self.hitbox = (self.x, self.y, 16, 45)
        self.fbcount = 0

    def draw(self, screen):
        if self.walking == False and self.jumping == False and self.crouchcount == 0:
            if self.left == False:
                screen.blit(r_stand, (self.x, self.y))
            if self.left == True:
                screen.blit(l_stand, (self.x, self.y))
            self.hitbox = (self.x, self.y, 16, 45)
            self.height = 45

#WALKING ANIMATION

        if self.walking == True and self.jumping == False and self.crouchcount == 0:
            if self.right == True:
                screen.blit(r_walk[self.walkCount//300], (self.x, self.y))
            if self.left == True:
                screen.blit(l_walk[self.walkCount//300], (self.x, self.y))
            self.walkCount += 1
            if self.walkCount >= 1800:
                self.walkCount = 0
            self.walking = False
            self.hitbox = (self.x, self.y, 16, 45)
            self.height = 45

#JUMPING ANIMATION

        if self.jumping == True:
            if self.left == False:
                screen.blit(r_jump[self.jumpcount//534], (self.x, self.y))
            if self.left == True:
                screen.blit(l_jump[self.jumpcount//534], (self.x, self.y))
            self.jumpcount += 1
            if self.jumpcount >= 1602:
                self.jumpcount = 0
            self.hitbox = (self.x, self.y, 16, 45)
            self.height = 45

#CROUCHING ANIMATION

        if self.jumping == False and self.crouchcount != 0:
            if self.left == False:
                screen.blit(r_crouch[self.crouchcount//100], (self.x, self.y + (self.crouchcount//100 * 7)))
            if self.left == True:
                screen.blit(l_crouch[self.crouchcount//100], (self.x, self.y + (self.crouchcount//100 * 7)))
            self.hitbox = (self.x, self.y + 13, 19, 33)
            self.height = 33
        if self.crouchcount < 299 and self.crouching == True and self.jumping == False:
                self.crouchcount += 1
        if self.crouchcount > 0 and self.crouching == False and self.jumping == False:
                self.crouchcount -= 1
        pygame.draw.rect(screen, (255,0,0), self.hitbox, 2)

class player2(player1):
    def __init__(self):
        super().__init__()
        self.x = 780

class projectile():
    def __init__(self, x, y, facing):
        self.x = x
        self.y = y
        self.facing = facing
        self.fbcount = 0

    def draw(self, screen):
        if player1.left == False or player2.left == False:
            screen.blit(r_fireball[self.fbcount//100], (self.x, self.y))
        if player1.left == True or player2.left == True:
            screen.blit(l_fireball[self.fbcount//100], (self.x, self.y))
        self.fbcount += 1

def UpdateScreen():
    screen.blit(scene, (0,0))
    player1.draw(screen)
    player2.draw(screen)
    for fireball in fireballs:
        projectile.draw(screen)
    pygame.display.flip()

#MAIN LOOP

player1 = player1()
player2 = player2()
fireballs = []
while running == True:

#SETUP QUIT

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    keys = pygame.key.get_pressed()

#SETUP FIREBALLS

    for fireball in fireballs:
        if fireball.x < scenewidth and fireball.x > 0:
            fireball.x += fireball.facing
        else:
            fireballs.remove(fireball)

#LEFT PLAYER 1

    if keys[pygame.K_LEFT] and player1.x > player1.speed:
            player1.walking = True
            player1.left = True
            player1.right = False
            player1.x -= player1.speed

#LEFT PLAYER 2

    if keys[pygame.K_a] and player2.x > player2.speed:
            player2.walking = True
            player2.left = True
            player2.right = False
            player2.x -= player2.speed

#RIGHT PLAYER 1

    if keys[pygame.K_RIGHT] and player1.x < scenewidth - player1.width - player1.speed:
            player1.walking = True
            player1.right = True
            player1.left = False
            player1.x += player1.speed

#LEFT PLAYER 2

    if keys[pygame.K_d] and player2.x < scenewidth - player2.width - player2.speed:
            player2.walking = True
            player2.right = True
            player2.left = False
            player2.x += player2.speed

#JUMP PLAYER 1
    if not(player1.jumping):
        if keys[pygame.K_UP]:
            player1.jumping = True
            player1.jumpcount = 0
    else:
        if player1.jumpvar >= -100:
            neg = 1
            if player1.jumpvar < 0:
                neg =- 1
            player1.y -= ((player1.jumpvar/100) ** 2) * 0.3 * neg
            player1.jumpvar -= 0.125
        else:
            player1.jumping = False
            player1.jumpvar = 100

#JUMP PLAYER 2

    if not(player2.jumping):
        if keys[pygame.K_w]:
            player2.jumping = True
            player2.jumpcount = 0
    else:
        if player2.jumpvar >= -100:
            neg = 1
            if player2.jumpvar < 0:
                neg =- 1
            player2.y -= ((player2.jumpvar/100) ** 2) * 0.3 * neg
            player2.jumpvar -= 0.125
        else:
            player2.jumping = False
            player2.jumpvar = 100

#CROUCH PLAYER 1

    if keys[pygame.K_DOWN] and player1.walking == False:
        player1.crouching = True
    else:
        player1.crouching = False

#CROUCH PLAYER 2

    if keys[pygame.K_s] and player2.walking == False:
        player2.crouching = True
    else:
        player2.crouching = False

#FIREBALL PLAYER 1

    if keys[pygame.K_KP1]:
        if player1.left:
            facing = -1
        else:
            facing = 1
        if len(fireballs) < 5:
            fireballs.append(projectile(player1.x + player1.width//2, player1.y + player1.height//2, facing))

    UpdateScreen()


What I have tried:

-put projectile.draw(8, screen) for example, no more error about this but then error aboout screen.blit(r_fireball[self.fbcount//100], (self.x, self.y)) where AttributeError: 'int' object has no attribute 'fbcount'
Posted
Updated 26-Feb-20 3:59am
v2

1 solution

Python
for fireball in fireballs:
    projectile.draw(screen)

Your call to projectile above is to an object that does not exist. As far as I can see fireballs is an array of projectile objects, so the correct call would be:
Python
for fireball in fireballs:
    fireball.draw(screen)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900