Python set class variable after calling - python

I'm making a game using the pygame module and I have a player class:
class Player(pygame.sprite.Sprite):
def __init__(self, name, position, axsis, movment, idle, walk = None, jump = None):
pygame.sprite.Sprite.__init__(self)
self.name = name
self.idle = idle
self.walk = walk
self.jump = jump
self.image = self.idle[0]
self.movment = movment
self.left, self.right = axsis
self.pos = vec(position[0],position[1])
I am adding my characters using json data type and trying to add animations after calling the class but i can't do it
Sample code
class Game():
def __init__(self,json_file):
self.player_attribute = json_file
def character(self):
self.npc = []
for i in self.player_attribute:
self.npc.append(Player(i['name'],
i['position'],
i['axsis'],
i['movment']))
self.animation()
return self.npc
def add_animation(self):
for i in self.npc:
i.idle = "images\ghost.png"
def main_loop()
self.character
when i try this i get an error
self.image = self.idle[0]
TypeError: init() missing 1 required positional argument: 'idle'
how can i add the variables of the class after calling the class

It is not clear what the 'idle' parameter is supposed to represent in your code. However, the reason for the exception is that you are not passing any argument for 'idle' when constructing the Player object. You need something like:
self.npc.append(Player(i['name'],
i['position'],
i['axsis'],
i['movment'],
i['idle']))
You can either do that or alternatively you can pass a default argument to the Player constructor so that, when initializing you do not need to explicitly pass idle:
class Player(pygame.sprite.Sprite):
def __init__(self, name, position, axsis, movment, idle=[1], walk = None, jump = None):
You can modify its content at a later time, however I suggest you are careful what you instantiate that object attribute as, because it might come bite you back later (type error or value error).
If it were me, I would move this out of init, or not build the object until all values and their types are known (see Builder Design Pattern).
Hope this helped :) Cheers!

Related

Questions about python module

It is my first time learning PyQt5.
While I read code related to PyQt5, I have a question some modules.
Is 'isStarted' a variable or in a module?
def __init__(self):
super().__init__()
self.isStarted = False
self.isPaused = False
self.nextMove = None
self.lastShape = Shape.shapeNone
def __init__(self):
super().__init__()
self.isStarted = False
self.isPaused = False
self.nextMove = None
self.lastShape = Shape.shapeNone
Whenever there is an =, rest assured that anything on the left side is a variable. In this case, you are making isStarted variable an attribute of self which is the class in which your code is in. So you can call it from other classes like: print(App.isStarted) (of course, replace App with whatever your class is named.

Keyevents with self - Python

I have this bit of code here:
from tkinter import *
class player():
def __init__(self, xcoor = 0, ycoor = 0):
self.xcoor = xcoor
def leftKey(self, event):
self.xcoor += 1
print("Right key pressed")
def rightKey(self, event):
self.ycoor += 1
print("Left key pressed")
world = Tk()
p1 = player()
world.bind('<Left>', player.leftKey)
world.bind('<Right>', player.rightKey)
world.mainloop()
When I run this and try the keys, whether right or left, I get this error:
TypeError: leftKey() missing 1 required positional argument: 'event'
Exception in Tkinter callback
I think the error is because (self, event) is wrong, but how do I fix that? I want it such that if an object of this class is created, its xcoor and ycoor will change when calling this function via keybindings.
Bind to the player object’s methods instead:
world.bind('<Left>', p1.leftKey)
world.bind('<Right>', p1.rightKey)
Otherwise, player.leftKey and player.rightKey will refer to the unbound methods that still expect a player object as the first argument (self). By referencing the methods from the player object, that argument is implicitely set. This is the same behavior you get when you just do p1.leftKey(evt) which is really just the same as player.leftKey(p1, evt).

how to reference an attribute before it exists python

I'm writing a game in pygame, and I have a class that makes a heads up display with stats for each building when you click on it. the class variable current_hud starts as None, but when a building is clicked, its value becomes the building object so that when the draw function is called it only draws the hud of the selected building.
The problem is, I'm trying to create an instance of my "Text" class in the hud, but I'm getting the AttributeError: 'NoneType' object has no attribute 'name' because self.current_hud isn't an object yet, so self.current_hud.name isn't an attribute yet. How do I reference an attribute that doesn't exist yet? is waiting to instantiate the hud class until after the building is clicked on really the only option?
class Hud:
current_hud = None
def __init__(self,x,y):
self.x = x
self.y = y
self.name_text = Text(x,y,str(self.current_hud.name), (0,0,0), 36)
def draw(self):
if Hud.current_hud == self:
self.square = pygame.Rect((self.x,self.y),(440,400))
pygame.draw.rect(screen,(255,255,255),self.square)
self.name_text.x = self.x + 10
self.name_text.y = self.y + 20
sorry if this is convoluted, but it's a little difficult to explain. The full code is here if you want to run it and see how it all works: https://github.com/hailfire006/economy_game/blob/master/economy_game.py
You can take the name as an option to the Hud class and set it prior to instantiating the Text class.
class Hud:
current_hud = None
def __init__(self,x,y,name):
self.x = x
self.y = y
self.current_hud.name = name
self.name_text = Text(x,y,str(self.current_hud.name), (0,0,0), 36)
def draw(self):
if Hud.current_hud == self:
self.square = pygame.Rect((self.x,self.y),(440,400))
pygame.draw.rect(screen,(255,255,255),self.square)
self.name_text.x = self.x + 10
self.name_text.y = self.y + 20
In your full code example it looks like when you instantiate a building you have its name and can then pass that along to the Hud on instantiation.
class Building:
def __init__(self,posX,posY,color,name):
self.hud = Hud(0,0,name)

collision detection error:AttributeError: type object 'Ship_laser' has no attribute 'sprites'

I am trying to write a game using a model, but i get the error:
"File "C:\Python27\lib\site-packages\pygame\sprite.py", line 1514, in spritecollide
for s in group.sprites():AttributeError: type object 'Ship_laser' has no attribute 'sprites'"
when running the script.If i dont call my collision function the script runs, so in that function is the mistake,but i dont understand where's the mistake.Here is the code of the function:
def collisions():
for enemy_ship in classes.Enemy_ship.List:
enemy_laser = pygame.sprite.spritecollide(enemy_ship, classes.Ship_laser, True)
if len(enemy_laser) > 0:
for hit in enemy_laser:
enemy_ship.health -= 25
for laser in classes.Ship_laser.List:
if pygame.sprite.spritecollide(laser, enemy_ship, True):
laser.destroy()
If it's needed i am posting the the Ship_laser class from my classes.py file
class Ship_laser(pygame.sprite.Sprite):
allsprites = pygame.sprite.Group()
def __init__(self, x, y, image_string):
pygame.sprite.Sprite.__init__(self)
Ship_laser.allsprites.add(self)
self.image = pygame.image.load(image_string)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.velx, self.vely = 0, 0
#staticmethod
def laser_movement(SCREENWIDTH, SCREENHEIGHT):
for laser in Ship_laser.List:
laser.rect.x += laser.velx
laser.rect.y += laser.vely
def destroy(self):
Ship_laser.List.remove(self)
del self
Considering that the Laser.ship class it's inheriting the pygame.sprite.Sprite class i dont understand the error.This is my first game.Please help
I can't say I am an expert with pygame, nor do I fully understand how you have built your classes. When I hear ship laser, I think of a single instance that belongs to a ship, yet in your class you define "allsprites" which is mutable type defined to a class instance (shared by all members of the class).
But given this, allsprites will be the same mutable object shared among ever ship_laser, almost like the yellowpages of your class. When you call pygame.sprite.spritecollide (based on pygame docs) it is looking for the sprite.Group and hence, you should pass it the group lookup (the yellowpages aka allsprites) rather than the reference to the class. That should sort your issues. So, here is your code change:
enemy_laser = pygame.sprite.spritecollide(enemy_ship, classes.Ship_laser, True)
to
enemy_laser = pygame.sprite.spritecollide(enemy_ship, classes.Ship_laser.allsprites, True)

How can I prevent my python game from reiterating and instead carry on?

I've made a simple game using pygame and livewires, where a sprite has to avoid falling mushrooms. The number of mushrooms falling at a certain time is meant to increase as the score increases. Here is what I mean:
from livewires import games,color
import random
games.init(screen_width=633,screen_height=479,fps=50)
class Stick_Man(games.Sprite):
def update(self):
self.x=games.mouse.x
if self.left<0:
self.left=0
if self.right>games.screen.width:
self.right=games.screen.width
self.check_collision()
def check_collision(self):
if self.overlapping_sprites:
self.over_message()
def over_message(self):
b=games.Message(value="Game Over", size=100, color=color.red,x=games.screen.width/2,y=games.screen.height/2,lifetime=250,after_death=games.screen.quit)
games.screen.add(b)
class Mushroom(games.Sprite):
score=0
start=200
score_required=100
level=1
total_score=0
speed=1
mushroom=games.load_image("mushroom.jpg")
x_position=random.randrange(640)
#staticmethod
def next_level():
indicate='Level ', + Mushroom.level, ' cleared'
message=games.Message(value=indicate,size=50,color=color.red,x=games.screen.width/2,y=games.screen.height/2, lifetime=150)
games.screen.add(message)
Mushroom().score_required+=50
Mushroom().score-=Mushroom.score_required
Mushroom().start-=150
Mushroom().speed+=5
Mushroom().level+=1
if Mushroom().start==20:
Mushroom().start+=10
def __init__(self):
super(Mushroom,self).__init__(image=Mushroom.mushroom,x=games.mouse.x,y=0)
def update(self):
self.dy=Mushroom.speed
self.check()
self.check2()
def check(self):
if self.bottom==games.screen.height:
self.destroy()
Mushroom.score+=50
Mushroom.total_score+=Mushroom.score
if Mushroom().score==Mushroom.score_required:
self.next_level()
def check2(self):
if self.top==Mushroom.start:
self.duplicate()
def duplicate(self):
new_mush=Mushroom()
games.screen.add(new_mush)
background_image=games.load_image("background.jpg", transparent=False)
games.screen.background=background_image
stickman_image=games.load_image("stickman.png", transparent=True)
stickman=Stick_Man(image=stickman_image,left=1,bottom=480)
games.screen.add(stickman)
games.mouse.is_visible=False
b=Mushroom()
c=Mushroom()
a=Mushroom()
games.screen.add(b)
games.screen.add(a)
games.screen.add(c)
games.screen.event_brab=True
games.screen.mainloop()
The code is pretty self explanatory and whenever one of the mushrooms is equal to start, then a new object is created thus meaning a new mushroom comes in. However, what happens is that code doesn't function properly a second time and the mushrooms don't get faster spawn much faster either. Also, when the game first starts, the minute the first mushroom hits the bottom it says level one cleared, when it should be after two mushrooms. The sprite is just a red mushroom and also a stickman which can be found on g images if you want to simulate.
So my question is how do i make the object's STATS carry on from where it left off whenever another mushroom appears and also display the message at the right time
Your problem is in all of the lines that look like this:
Mushroom().score_required+=50
There are a number of problems here, which all together add up to make this have no useful effect:
Mushroom() creates a new Mushroom instance (which goes away as soon as this line is done).
Assigning (including update-assigning) to an attribute through an instance always creates or updates an instance attribute, even if there was a class attribute of the same name.
The += operator doesn't mutate immutable values like integers in-place (because that would be impossible); a += b is effectively the same as a = a + b.*
So, when you put that together, what you're doing is creating a new value equal to Mushroom.score_required + 50, then assigning that value to a new instance attribute of a temporary instance (which immediately goes away). This has no effect on the class attribute, or on any of the other instances.
You have a related, but different, problem in the lines like this:
x_position=random.randrange(640)
Unless you want all of the mushrooms to have the same x_position, this should not be a class attribute, but an instance attribute, and you're going to run into all kinds of strange problems.
Storing game stats as class attributes of a random class is a strange thing to do. There are ways you could make that work, but there's no good reason to even try. Class attributes are useful for constants that all instances of the class might as well share, but they're not useful as a substitute for global variables.
A better design would be something like this:
class Game(object):
def __init__(self):
self.score = 0
self.start = 200
self.score_required = 100
self.level = 1
self.total_score = 0
def next_level(self):
indicate = 'Level ', + Mushroom.level, ' cleared'
message = games.Message(value=indicate, size=50, color=color.red,
x=games.screen.width/2, y=games.screen.height/2,
lifetime=150)
games.screen.add(message)
self.score_required += 50
self.score -= self.score_required
self.start -= 150
self.speed += 5
self.level += 1
if self.start == 20:
self.start += 10
def update_score(self, n):
game.score += n
game.total_score += game.score
if self.score == self.score_required:
self.next_level()
class Mushroom(games.Sprite):
mushroom=games.load_image("mushroom.jpg")
def __init__(self, game):
self.x_position=random.randrange(640)
self.game = game
super(Mushroom,self).__init__(image=Mushroom.mushroom,x=games.mouse.x,y=0)
def update(self):
self.dy=Mushroom.speed
self.check()
self.check2()
def check(self):
if self.bottom == games.screen.height:
self.destroy()
game.update_score(50)
def check2(self):
if self.top == Mushroom.start:
self.duplicate()
def duplicate(self):
games.screen.add(Mushroom(self.game))
game = Game()
games.screen.add(Mushroom(game))
games.screen.add(Mushroom(game))
games.screen.add(Mushroom(game))
games.screen.event_brab=True
* That's not completely true. In fact, a = a + b is equivalent to a = a.__add__(b), while a += b is equivalent to a = a.__iadd__(b) if such a method exists, falling back to __add__ only if it doesn't. For mutable objects like lists, this makes a big difference, because __iadd__ can change self in-place and then return it, meaning you end up assigning the same object back to a that was already there. But for immutable objects, there's no difference.

Categories

Resources