Pygame surfaces and their Rects
- by Jaka Novak
I am trying to understand how pygame surfaces work. I am confused about Rect position of Surface object.
If I try blit surface on screen at some position then Surface is drawn at right position,
but Rect of the surface is still at position (0, 0)...
I tried write my own surface class with new rect, but i am not sure if is that right solution.
My goal is that i could move surface like image with rect.move() or something like that.
If there is any solution to do that i would be happy to read it.
Thanks for answer and time for reading this awful English
If helps i write some code for better understanding my problem.
(run it first, and then uncomment two lines of code and run again to see the diference):
import pygame
from pygame.locals import *
class SurfaceR(pygame.Surface):
def __init__(self, size, position):
pygame.Surface.__init__(self, size)
self.rect = pygame.Rect(position, size)
self.position = position
self.size = size
def get_rect(self):
return self.rect
def main():
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Screen!?")
clock = pygame.time.Clock()
fps = 30
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
surface = pygame.Surface((70,200))
surface.fill(red)
surface_re = SurfaceR((300, 50), (100, 300))
surface_re.fill(blue)
while True:
for event in pygame.event.get():
if event.type == QUIT:
return 0
screen.blit(surface, (100,50))
screen.blit(surface_re, surface_re.position)
#pygame.draw.rect(screen, white, surface.get_rect())
#pygame.draw.rect(screen, white, surface_re.get_rect())
pygame.display.update()
clock.tick(fps)
if __name__ == "__main__":
main()