Im trying to build a window class which resembles a class which sprites live in. In the window class i want to have the following things:
- set_background()
- set_size()
- add_sprite()
- remove_sprite()
In the sprite class i want the following methods:
- draw_sprite()
For now i will have one sprite, but i would eventually like to have a list of sprites.
I've tried running what I have in a main class by calling these methods on its instances.
window = Window(200,200)
sprite = Sprite(Window)
window.set_Background()
sprite.draw_sprite()
Heres my code:
Sprite class:
import pygame
pygame.init()
class Sprite(object):
def __init__(self, World =None, sprite=[]):
self.Window = window
def draw_sprite(self,sprite,x,y):
sprite=pygame.image.load(sprite)
self.World.window.blit(sprite,(x,y))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
Window class:
import pygame,sys,os from pygame.locals import *
from Window import *
class Panel:
def __init__(self, width=None,height=None):
self.width= width
self.height = height
self.foreground=pygame.Surface((width, height))
self.background= pygame.Surface((width, height))
self.rect= self.foreground.get_rect()
def clear(self):
self.foreground.blit(self.background,(0,0))
def set_background(self, image=None):
if sky is not None:
bg = Window.draw_world(self,image)
self.background.blit(bg,(width,height))
class Window(Panel):
pygame.init()
def __init__(self,width,height):
self.window = pygame.display.set_mode((width,height))
self.width=width
self.height=height
Panel.__init__(self, width, height)
self._foreground = self.window
self.set_background()
def draw_world(self,image):
image=pygame.image.load(image)
for x in range(0,(290/image.get_width()+1)):
for y in range(0,(230/image.get_height()+1)):
self.background.blit(image,(x*200,y*200))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
def window_setSize(width,height):
self.width=width
self.heght =height
main class:
from Sprite import *
from window import *
sprite = Sprite()
window=Window()
sprite.draw_sprite("sprite.png",100,200)
window.set_background("bg.png")
Has anyone got any ideas why the sprite is displaying but my background isnt? It just shows a black background.
Im using python 3 and pygame 3.3 Thanks
You have event loop in
draw_sprite()
so it is running to the end of game andwindow.set_background
is never executed.Your code is incorrect constructed.
I try to correct it and send code later.
by the way: see: pygame.sprite.Sprite.
EDIT:
Simple example how to organize code.
Now it is in one file. In Pygame is always one Window so there is no need to make Panel+Widnow. You have one event loop in
run()
. All code is inside Window (create Sprite, changing background, drawing etc.).I add sprites to (python) list and draw all sprites from list - player is exception - so I can remove only last sprite from list :/ If you need something better see pygame.sprite.Sprite() and pygame.sprite.Group().
Use
Arrows
to move red ball,Space
to pause game,ESC
to quit.At the end I attached my bitmaps.
ball1.png ball2.png ball3.png
background.jpg
screenshot