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
import pygame
from pygame.locals import *
pygame.init()
running = pygame.display.get_active()
screen = pygame.display.set_mode((859, 561))
scene = pygame.image.load("background.jpg").convert()
scenewidth = screen.get_width()
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
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
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
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()
player1 = player1()
player2 = player2()
fireballs = []
while running == True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
keys = pygame.key.get_pressed()
for fireball in fireballs:
if fireball.x < scenewidth and fireball.x > 0:
fireball.x += fireball.facing
else:
fireballs.remove(fireball)
if keys[pygame.K_LEFT] and player1.x > player1.speed:
player1.walking = True
player1.left = True
player1.right = False
player1.x -= player1.speed
if keys[pygame.K_a] and player2.x > player2.speed:
player2.walking = True
player2.left = True
player2.right = False
player2.x -= player2.speed
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
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
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
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
if keys[pygame.K_DOWN] and player1.walking == False:
player1.crouching = True
else:
player1.crouching = False
if keys[pygame.K_s] and player2.walking == False:
player2.crouching = True
else:
player2.crouching = False
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'