Reputation: 95
I am new to pygame, and I was expecting the display be cyan and the rectangle to be drawn. The window appears but not in cyan and without the rectangle?
I believe it has something to do with the order or spacing. Everything was working before I added the rect.
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('''example''')
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
gameDisplay.fill(cyan)
gameExit = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Upvotes: 0
Views: 51
Reputation: 16711
When initializing a pygame screen, a surface is returned which you can fill and should fill continuously in your while loop. I suggest you use a surface object for the rectangle as well. Just change your code like so:
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Example') # shouldn't use triple quotes
gameExit = True
surf = pygame.Surface((200, 75)) # takes tuple of width and height
rect = surf.get_rect()
rect.center = (400, 300)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
gameDisplay.fill(cyan) # continuously paint screen cyan
surf.fill(soft_pink) # continuously paint rectangle
gameDisplay.blit(surf, rect) # position rectangle at position
Remember continuous rendering is the underlying secret to game development, and objects are painted in the order you specify. So in this case you'd actually see the rectangle because it's painted after the screen is.
Upvotes: 0
Reputation: 33193
You should be very careful with your formatting for Python code. Testing your code and fixing up the formatting for the while loop reveals the problem:
C:\src\python\pygame1>python buggy.py
Traceback (most recent call last):
File "buggy.py", line 16, in <module>
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
TypeError: function takes at most 4 arguments (5 given)
If you just replace the pygame.draw.rect
call with the correct number of parameters it shows a cyan window. I tested the following replacement line:
pygame.draw.rect(gameDisplay,soft_pink,(389,200,300,70))
Upvotes: 3