0

I have a ton of irrelevant code, so I am going to try to skip to just the important bits after commenting out the unimportant tidbits I am left with:

import pygame
from pygame.locals import *
from characters import Character

class Game(object):
  def __init__(self):
    self.__running = True
    self.__display = None
    self.size = self.width, self.height = 640, 480

  def on_init(self):
    pygame.init()
    self.clock = pygame.time.Clock()
    self.__display = pygame.display.set_mode(self.size, pygame.HWSURFACE |
        pygame.DOUBLEBUF)
    pygame.mouse.set_visible(False)
    self.hero = Character()
    self.hrgrp = pygame.sprite.Group(self.hero)
    self.__running = True

  def on_render(self):
    self.hrgrp.draw(self.__display)
    pygame.display.update()

  def on_cleanup(self):
    pygame.quit()

  def on_execute(self):
    if self.on_init() == False:
      self.__running = False

    while(self.__running):
      self.on_render()
    self.on_cleanup()

if __name__ = "__main__":
  game = Game()
  game.on_execute()

If you are wondering:

self.hero = Character()

only contains

Class Character(pygame.sprite.Sprite):
  def __init__(self):
    pygame.image.load("path.png")    
    self.rect = self.sprite.get_rect()

The error returned is:

File "/home/thegilberts/Documents/Python/game/game/game.py", line 70, in on_render
self.hrgrp.draw(self.__display)
File "/usr/local/lib/python3.5/dist-packages/pygame-1.9.4.dev0-py3.5-linux-x86_64.egg/pygame/sprite.py", line 475, in draw
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
TypeError: argument 1 must be pygame.Surface, not str

I have no clue why. It is definitely a surface, not a string. In fact, I use self.__display in many other places to create menus and things but it just will not let me draw this sprite group.

1 Answers1

0

I figured it out. Inside the Character() class, it is very important that self.image be the pygame.image.load() Turns out I was missing some code from the Character() class that I posted because I didn't think it was important. If anyone else encounters this error, the improper code was:

Class Character(pygame.sprite.Sprite)
  def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.image = "image.png"
  self.sprite = pygame.image.load(self.image)
  self.rect = self.sprite.get_rect()

To correct the issue, I swapped the self.image with self.sprite in all cases and it worked.