Moving a class to its own file in python












-2















I have 2 files one of them is main the other is a player class and im getting these errors:



Traceback (most recent call last):
File "C:/Users/travi/PycharmProjects/game/main.py", line 15, in <module>
player.player1 = player.player(300, 410, 32, 32)
File "C:UserstraviPycharmProjectsgameplayer.py", line 18, in __init__
self.playerImage
AttributeError: 'player' object has no attribute 'playerImage'


Heres the source code



#file 1############################################
import pygame
import player

pygame.init()

size = WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Game 2")

FPS = 60
fpsclock = pygame.time.Clock()
animation_speed = 5

player.player1 = player.player(300, 410, 32, 32)
run = True
while run:
pygame.time.delay(50)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if player.player1.active:
player.player1.frame += 1
if player.player1.frame >= 2:
player.player1.frame = 0

player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.dir]


if keys[pygame.K_LEFT] and player.player1.x > player.player1.vel:
player.player1.x -= player.player1.vel
player.player1.dir = 3
player.player1.active = True
elif keys[pygame.K_RIGHT] and player.player1.x < 640 - player.player1.width - player.player1.vel:
player.player1.x += player.player1.vel
player.player1.dir = 6
player.player1.active = True
elif keys[pygame.K_UP] and player.player1.y > player.player1.vel:
player.player1.y -= player.player1.vel
player.player1.dir = 9
player.player1.active = True
elif keys[pygame.K_DOWN] and player.player1.y < 480 - player.player1.height - player.player1.vel:
player.player1.y += player.player1.vel
player.player1.dir = 0
player.player1.active = True
else:
player.player1.active = False


screen.fill((0, 0, 0))

screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))

pygame.display.flip()
fpsclock.tick(FPS)

pygame.quit()


##file 2

import pygame

class player():
def __init__(self, x, y, width, height):
self.cells =
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.dir = 0
self.walkcount = 0
self.active = False
self.frame = 0
self.spritesheetplayer =
pygame.image.load("playerspritesheet.png").convert()
self.playerImage

def initialize(self):
for n in range(12):
width, height = (32, 32)
rect = pygame.Rect(n * width, 0, width, height)
image = pygame.Surface(rect.size).convert()
image.blit(self.spritesheetplayer, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
self.cells.append(image)
self.playerImage = self.cells[0]


the codes supposed to poll for events and move and animate the character this code was all originally in one file and once i moved some of it to another file these errors started happening how can this code be fixed so that it works










share|improve this question























  • If your going to post Python code, please make the effort to reproduce your indentation accurately. Otherwise people must guess what it is supposed to be.

    – khelwood
    Nov 25 '18 at 22:36













  • For the last line of player.__init__(), did you intend to assign something to self.playerImage?

    – wwii
    Nov 25 '18 at 22:38


















-2















I have 2 files one of them is main the other is a player class and im getting these errors:



Traceback (most recent call last):
File "C:/Users/travi/PycharmProjects/game/main.py", line 15, in <module>
player.player1 = player.player(300, 410, 32, 32)
File "C:UserstraviPycharmProjectsgameplayer.py", line 18, in __init__
self.playerImage
AttributeError: 'player' object has no attribute 'playerImage'


Heres the source code



#file 1############################################
import pygame
import player

pygame.init()

size = WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Game 2")

FPS = 60
fpsclock = pygame.time.Clock()
animation_speed = 5

player.player1 = player.player(300, 410, 32, 32)
run = True
while run:
pygame.time.delay(50)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if player.player1.active:
player.player1.frame += 1
if player.player1.frame >= 2:
player.player1.frame = 0

player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.dir]


if keys[pygame.K_LEFT] and player.player1.x > player.player1.vel:
player.player1.x -= player.player1.vel
player.player1.dir = 3
player.player1.active = True
elif keys[pygame.K_RIGHT] and player.player1.x < 640 - player.player1.width - player.player1.vel:
player.player1.x += player.player1.vel
player.player1.dir = 6
player.player1.active = True
elif keys[pygame.K_UP] and player.player1.y > player.player1.vel:
player.player1.y -= player.player1.vel
player.player1.dir = 9
player.player1.active = True
elif keys[pygame.K_DOWN] and player.player1.y < 480 - player.player1.height - player.player1.vel:
player.player1.y += player.player1.vel
player.player1.dir = 0
player.player1.active = True
else:
player.player1.active = False


screen.fill((0, 0, 0))

screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))

pygame.display.flip()
fpsclock.tick(FPS)

pygame.quit()


##file 2

import pygame

class player():
def __init__(self, x, y, width, height):
self.cells =
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.dir = 0
self.walkcount = 0
self.active = False
self.frame = 0
self.spritesheetplayer =
pygame.image.load("playerspritesheet.png").convert()
self.playerImage

def initialize(self):
for n in range(12):
width, height = (32, 32)
rect = pygame.Rect(n * width, 0, width, height)
image = pygame.Surface(rect.size).convert()
image.blit(self.spritesheetplayer, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
self.cells.append(image)
self.playerImage = self.cells[0]


the codes supposed to poll for events and move and animate the character this code was all originally in one file and once i moved some of it to another file these errors started happening how can this code be fixed so that it works










share|improve this question























  • If your going to post Python code, please make the effort to reproduce your indentation accurately. Otherwise people must guess what it is supposed to be.

    – khelwood
    Nov 25 '18 at 22:36













  • For the last line of player.__init__(), did you intend to assign something to self.playerImage?

    – wwii
    Nov 25 '18 at 22:38
















-2












-2








-2








I have 2 files one of them is main the other is a player class and im getting these errors:



Traceback (most recent call last):
File "C:/Users/travi/PycharmProjects/game/main.py", line 15, in <module>
player.player1 = player.player(300, 410, 32, 32)
File "C:UserstraviPycharmProjectsgameplayer.py", line 18, in __init__
self.playerImage
AttributeError: 'player' object has no attribute 'playerImage'


Heres the source code



#file 1############################################
import pygame
import player

pygame.init()

size = WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Game 2")

FPS = 60
fpsclock = pygame.time.Clock()
animation_speed = 5

player.player1 = player.player(300, 410, 32, 32)
run = True
while run:
pygame.time.delay(50)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if player.player1.active:
player.player1.frame += 1
if player.player1.frame >= 2:
player.player1.frame = 0

player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.dir]


if keys[pygame.K_LEFT] and player.player1.x > player.player1.vel:
player.player1.x -= player.player1.vel
player.player1.dir = 3
player.player1.active = True
elif keys[pygame.K_RIGHT] and player.player1.x < 640 - player.player1.width - player.player1.vel:
player.player1.x += player.player1.vel
player.player1.dir = 6
player.player1.active = True
elif keys[pygame.K_UP] and player.player1.y > player.player1.vel:
player.player1.y -= player.player1.vel
player.player1.dir = 9
player.player1.active = True
elif keys[pygame.K_DOWN] and player.player1.y < 480 - player.player1.height - player.player1.vel:
player.player1.y += player.player1.vel
player.player1.dir = 0
player.player1.active = True
else:
player.player1.active = False


screen.fill((0, 0, 0))

screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))

pygame.display.flip()
fpsclock.tick(FPS)

pygame.quit()


##file 2

import pygame

class player():
def __init__(self, x, y, width, height):
self.cells =
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.dir = 0
self.walkcount = 0
self.active = False
self.frame = 0
self.spritesheetplayer =
pygame.image.load("playerspritesheet.png").convert()
self.playerImage

def initialize(self):
for n in range(12):
width, height = (32, 32)
rect = pygame.Rect(n * width, 0, width, height)
image = pygame.Surface(rect.size).convert()
image.blit(self.spritesheetplayer, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
self.cells.append(image)
self.playerImage = self.cells[0]


the codes supposed to poll for events and move and animate the character this code was all originally in one file and once i moved some of it to another file these errors started happening how can this code be fixed so that it works










share|improve this question














I have 2 files one of them is main the other is a player class and im getting these errors:



Traceback (most recent call last):
File "C:/Users/travi/PycharmProjects/game/main.py", line 15, in <module>
player.player1 = player.player(300, 410, 32, 32)
File "C:UserstraviPycharmProjectsgameplayer.py", line 18, in __init__
self.playerImage
AttributeError: 'player' object has no attribute 'playerImage'


Heres the source code



#file 1############################################
import pygame
import player

pygame.init()

size = WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Game 2")

FPS = 60
fpsclock = pygame.time.Clock()
animation_speed = 5

player.player1 = player.player(300, 410, 32, 32)
run = True
while run:
pygame.time.delay(50)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if player.player1.active:
player.player1.frame += 1
if player.player1.frame >= 2:
player.player1.frame = 0

player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.dir]


if keys[pygame.K_LEFT] and player.player1.x > player.player1.vel:
player.player1.x -= player.player1.vel
player.player1.dir = 3
player.player1.active = True
elif keys[pygame.K_RIGHT] and player.player1.x < 640 - player.player1.width - player.player1.vel:
player.player1.x += player.player1.vel
player.player1.dir = 6
player.player1.active = True
elif keys[pygame.K_UP] and player.player1.y > player.player1.vel:
player.player1.y -= player.player1.vel
player.player1.dir = 9
player.player1.active = True
elif keys[pygame.K_DOWN] and player.player1.y < 480 - player.player1.height - player.player1.vel:
player.player1.y += player.player1.vel
player.player1.dir = 0
player.player1.active = True
else:
player.player1.active = False


screen.fill((0, 0, 0))

screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))

pygame.display.flip()
fpsclock.tick(FPS)

pygame.quit()


##file 2

import pygame

class player():
def __init__(self, x, y, width, height):
self.cells =
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.dir = 0
self.walkcount = 0
self.active = False
self.frame = 0
self.spritesheetplayer =
pygame.image.load("playerspritesheet.png").convert()
self.playerImage

def initialize(self):
for n in range(12):
width, height = (32, 32)
rect = pygame.Rect(n * width, 0, width, height)
image = pygame.Surface(rect.size).convert()
image.blit(self.spritesheetplayer, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
self.cells.append(image)
self.playerImage = self.cells[0]


the codes supposed to poll for events and move and animate the character this code was all originally in one file and once i moved some of it to another file these errors started happening how can this code be fixed so that it works







python pygame






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 22:34









TravisPeterson444TravisPeterson444

44




44













  • If your going to post Python code, please make the effort to reproduce your indentation accurately. Otherwise people must guess what it is supposed to be.

    – khelwood
    Nov 25 '18 at 22:36













  • For the last line of player.__init__(), did you intend to assign something to self.playerImage?

    – wwii
    Nov 25 '18 at 22:38





















  • If your going to post Python code, please make the effort to reproduce your indentation accurately. Otherwise people must guess what it is supposed to be.

    – khelwood
    Nov 25 '18 at 22:36













  • For the last line of player.__init__(), did you intend to assign something to self.playerImage?

    – wwii
    Nov 25 '18 at 22:38



















If your going to post Python code, please make the effort to reproduce your indentation accurately. Otherwise people must guess what it is supposed to be.

– khelwood
Nov 25 '18 at 22:36







If your going to post Python code, please make the effort to reproduce your indentation accurately. Otherwise people must guess what it is supposed to be.

– khelwood
Nov 25 '18 at 22:36















For the last line of player.__init__(), did you intend to assign something to self.playerImage?

– wwii
Nov 25 '18 at 22:38







For the last line of player.__init__(), did you intend to assign something to self.playerImage?

– wwii
Nov 25 '18 at 22:38














1 Answer
1






active

oldest

votes


















1














You have to set the initial value for playerImage, so for example in your init function self.playerImage = None






share|improve this answer
























  • the code works thanks

    – TravisPeterson444
    Nov 26 '18 at 1:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472694%2fmoving-a-class-to-its-own-file-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You have to set the initial value for playerImage, so for example in your init function self.playerImage = None






share|improve this answer
























  • the code works thanks

    – TravisPeterson444
    Nov 26 '18 at 1:36
















1














You have to set the initial value for playerImage, so for example in your init function self.playerImage = None






share|improve this answer
























  • the code works thanks

    – TravisPeterson444
    Nov 26 '18 at 1:36














1












1








1







You have to set the initial value for playerImage, so for example in your init function self.playerImage = None






share|improve this answer













You have to set the initial value for playerImage, so for example in your init function self.playerImage = None







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 22:37









vi_ralvi_ral

859




859













  • the code works thanks

    – TravisPeterson444
    Nov 26 '18 at 1:36



















  • the code works thanks

    – TravisPeterson444
    Nov 26 '18 at 1:36

















the code works thanks

– TravisPeterson444
Nov 26 '18 at 1:36





the code works thanks

– TravisPeterson444
Nov 26 '18 at 1:36




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472694%2fmoving-a-class-to-its-own-file-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'