I have a program that is simply made to move an image around. I try to state the self.rect as part of a load_png() call, but it simply does not like it. THe reason I think this will work is from http://www.pygame.org/docs/tut/tom/games6.html, saying that this should work:
def __init__(self, side):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_png('bat.png')
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.side = side
self.speed = 10
self.state = "still"
self.reinit()
Here is my code, which according to the pygame tutorial from its own website, should work:
def _init_(self):
pygame.sprite.Sprite._init_(self)
self.state = 'still'
self.image = pygame.image.load('goodGuy.png')
self.rect = self.image.get_rect()
screen = pygame.display.getSurface()
And it gives me this error:
Traceback (most recent call last):
File "C:\Python25\RPG.py", line 37, in <module>
screen.blit(screen, Guy.rect, Guy.rect)
AttributeError: 'goodGuy' object has no attribute 'rect'
If you guys need all of my code, comment blew and I will edit it.
You don't have a load_png function defined.
You need to create the pygame image object before you can access its rect property.
self.image = pygame.image.load(file)
Then you can assign the rect value using
self.rect = self.image.get_rect()
Or you could create the load_png function as per the example you linked.
There is no load_png function built-in to python or pygame. I imagine that the tutorial you are referring to defined it manually somewhere. What you want is pygame.image.load(filename) and then you can call get_rect() on the returned Surface object. The complete code would be as follows:
self.image = pygame.image.load('bat.png')
self.rect = self.image.get_rect()
Your second problem is that you've defined the function _init_, but you need double underscores: __init__.
Also, you need to post the code where the error is actually occurring.
Related
So I am learning to code on python with pygame. I am currently following a series of video tutorials for coding games.
I have followed the tutors code to the letter and checked and double checked (to the point of even checking his base code file.
But when I try to run the game I get this error:
File "/home/dev/PycharmProjects/Game5_Catch_Cookie/modules/hero.py", line 16, in init
self.rect = self.image_getrect()
so line 16 in the hero class set up code looks like:
class Hero(pygame.sprite.Sprite):
def __init__(self, images, position=[375, 520], **kwargs):
pygame.sprite.Sprite.__init__(self)
self.images_right = images[:5]
self.images_left = images[5:]
self.images = self.images_right.copy()
self.image = self.images[0]
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image_getrect() <------------- Line 16
self.rect.left, self.rect.top = position
self.direction = 'right'
self.speed = 8
self.switch_frame_count = 0
self.switch_frame_freq = 1
self.frame_index =0
I'm not sure what to do about this or how to fix it so the game I'm trying to learn to code works properly.
Instead of
self.rect = self.image_getrect()
You need to write
self.rect = self.image.get_rect()
I'm currently in the process of writing a larger program in python. It is a simple game, but I've got an Error. Can someone help me?
Error
Traceback (most recent call last):
File "C:/Users/kkuja/Desktop/game.py", line 36, in <module>
MainWindow.MainLoop()
File "C:/Users/kkuja/Desktop/game.py", line 17, in MainLoop
self.chicken_sprites.draw(self.screen)
File "C:\Users\kkuja\AppData\Local\Programs\Python\Python35\lib\site-packages\pygame\sprite.py", line 475, in draw
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
AttributeError: 'Chicken' object has no attribute 'rect'
Code
import os, sys
import pygame
class Game:
def __init__(self, width=640, height=480):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode([self.width, self.height])
def MainLoop(self):
self.ChickenLoad();
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
self.chicken_sprites.draw(self.screen)
pygame.display.flip()
def ChickenLoad(self):
self.chicken = Chicken()
self.chicken_sprites = pygame.sprite.Group(self.chicken)
class Chicken(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("chic.jpg")
if __name__ == "__main__":
MainWindow = Game()
MainWindow.MainLoop()
Thanks in advance!
In the function self.chicken_sprites.draw(self.screen) in your code chicken.rect is trying to be accessed, but you did not define it.
If you refer to the official documentation you can find this piece of code:
class Block(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
You do not set self.rect in your Chicken, it should look like this.
class Chicken(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("chic.jpg")
self.rect = self.image.get_rect(); #here rect is created
I keep getting this error whilst loading my program global name 'load_jpeg' is not defined
when running my class code.
class Hero:
def __init__(self,x,y):
self.x=x
self.y=y
self.width=70
self.height=70
self.image = pygame.image.load('ezio.jpg')
self.rect = self.image.get_rect()
The fact that pygame.blit() did not work is very clear and same with load_jpeg. First, the load_jpeg error. Just like how Martijn Pieters said, you can't just create a function that Python did not originally have. Maybe you could have wrote a specific function with that name, but that is not the case. Second, the pygame.blit(). To use this function, you need two arguments inside of it. To be able to do that, you might want to change your class:
class Hero(pygame.sprite.Sprite):
def __init__(self, location, image_file):
pygame.sprite.Sprite.__init__(self)
self.rect.top, self.rect.left = location #The equivalent as self.x and self.y
self.width = 70
self.height = 70
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
And add this line (if you haven't done this already and outside your class and before your while loop):
Heroes = Hero([100, 100], 'ezio.jpg')
This creates an variable that can be used for the pygame.blit() function. It is usually more better to do self.rect.top, self.rect.left = location than defining self.x and self.y. It is usually more of a PyGame style with rects. It looks like you are going to __init__ this into a sprite and define self. After you have done this, pygame.blit() should work. Saying that the variable name of the class is x, you must always do it in this order:
pygame.blit(x.image, x.rect)
With x.image first, then x.rect after. In your case, that line should look like:
pygame.blit(Heroes.image, Heroes.rect) #Assuming the variable name is Heroes
This answer should cancel your error and make the pygame.blit() function working again. I hope this helps you!
I am trying to create a pacman game with pygame but I have run into a few problems. It it saying that the 'Food' has no image.
This is my pacman game [code redacted].
The problem is this area here something is wrong and it tells me that food has no attribute image
class Food(pygame.sprite.Sprite):
def __init__(self,x,y,color):
pygame.sprite.Sprite.__init__(self)
pygame.image = pygame.Surface([7,7])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
def update (self,player):
collidePlayer = pygame.sprite.spritecollide(self,player,False)
if collidePlayer:
food.remove
Removing all the irrelevant bits, do you see the difference between the following Sprite subclasses' __init__ methods?
class Wall(pygame.sprite.Sprite):
def __init__(self,x,y,width,height, color):#For when the walls are set up later
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height]) # <-------------
self.image.fill(color)
class player(pygame.sprite.Sprite):
def __init__ (self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([13,13]) # <-------------
self.image.fill(white)
class Food(pygame.sprite.Sprite)
def __init__(self,x,y,color):
pygame.sprite.Sprite.__init__(self)
pygame.image = pygame.Surface([7,7]) # <----- this one is different!
self.image.fill(color)
The reason you're getting an error saying that self doesn't have an image attribute is because you didn't set self.image, you stored the image in the pygame module itself.
PS: The lines which look like
food.remove
seem suspicious to me. If remove is a method, it'd be called by food.remove(), and food.remove wouldn't do anything.
Good day,
I have like 15 images I need to be buttons. I have buttons working with a Box() (Box - looks like this)
class Box(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((35, 30))
self.image = self.image.convert()
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.centerx = 25
self.rect.centery = 505
self.dx = 10
self.dy = 10
I am trying to make the buttons work with image sprites. So I attempted to copy the class style of the box and do the same for my Icons.. code looks like this...
class Icons(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/airbrushIC.gif").convert()
self.rect = self.image.get_rect()
self.rect.x = 25
self.rect.y = 550
the code in the main()
rect = image.get_rect()
rect.x = 25
rect.y = 550
ic1 = Icons((screen.get_rect().x, screen.get_rect().y))
screen.blit(ic1.image, ic1.rect)
pygame.display.update()
This code produces a positional (accepts 1 argument but 2 are there) error or an image is not referenced error (inside the Icon class).
I'm unsure if this is the right way to go about this anyways.. I know for sure that I need to load all the images (as sprites)... store them in an array... and then have my mouse check if it is clicking one of the items in the array using a for loop.
Thanks.
You are trying to pass an argument into Icons(), but your __init__() method takes no arguments. If you wanted to pass those onto the Sprite() constructor, then you probably wanted something like:
class Icons(pygame.sprite.Sprite):
def __init__(self, *args):
pygame.sprite.Sprite.__init__(self, *args)
...
This accepts any number of extra arguments (*args) using the star operator, then passes them into the sprite constructor.