Reputation: 75
im relatively new to coding and after understanding some basics in python i'm trying to apply oop in pygame I have this code and I can't figure out why the rectangle won't appear
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("EXAMPLE")
clock = pygame.time.Clock()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
FPS = 30
class Puddles(pygame.sprite.Sprite):
puddle_width = 100
puddle_height = 20
def __init__(self,color,x,y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.color = color
self.image = pygame.Surface((Puddles.puddle_width,Puddles.puddle_height))
self.image.fill(self.color)
self.rect = self.image.get_rect()
self.rect.x = self.x # i think problem's here because if I type specific integers for x and y the tile will appear
self.rect.y = self.y
all_sprites = pygame.sprite.Group()
puddle1 = Puddles(red, 400, 600)
all_sprites.add(puddle1)
gameExit = False
while not gameExit:
clock.tick(FPS)
for event in pygame.event.get():
print event
if event.type == pygame.QUIT:
gameExit = True
all_sprites.update()
screen.fill(white)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
quit()
any ideas? thanks in advance:)
Upvotes: 1
Views: 645
Reputation: 20488
puddle1 = Puddles(red, 400, 600)
The y position of the puddle is below the screen height, so it's outside of the screen. ;) Try to change it for example to puddle1 = Puddles(red, 400, 200)
.
Also, lines 43-46 should be indented, so that they're in the while loop.
Upvotes: 1