The screen wont fill and it says there is no fill cmd for screen, what did I do wrong?
import pygame
pygame.init()
screen = pygame.display
set_mode((0, 0))
white = (255, 255, 255)
Square =(0, 0, 0)
loop = True
while loop:
screen.fill(white)
pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)
pygame.display.update()
There are some syntax errors in your code:
screen = pygame.display
set_mode((0, 0))
screen = pygame.display.set_mode((0, 0))
pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)
pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))
More than that, the event loop is missing. This can cause your application to freeze. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
Correct program:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
white = (255, 255, 255)
Square = (0, 0, 0)
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
screen.fill(white)
pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))
pygame.display.update()
Related
I want to start my main game loop with surface.fill(WHITE) to repaint my display white, but every surface from the last frame still appears.
To demonstrate, the following example blits a red surface on a green surface. The blit is created by an event trigger every 1 second. However, the red surface does not disappear when it is not being triggered by the event.
import pygame as pg
pg.init()
GREEN, WHITE, RED = (60, 120, 60), (255,255,255), (120, 60, 60)
screen = pg.display.set_mode((200, 200))
box = pg.Surface((150, 150))
box.fill(GREEN)
running = True
EVENT_SECOND_TIMER = pg.event.custom_type()
pg.time.set_timer(EVENT_SECOND_TIMER, 1000)
while running:
screen.fill(WHITE)
for event in pg.event.get():
# Draw circle every second
if event.type == EVENT_SECOND_TIMER:
surf = pg.Surface((50,50))
surf.fill(RED)
box.blit(surf, (50, 50))
screen.blit(box, (25, 25))
pg.display.update()
If you want to draw something permanently, you have to draw it in the application loop. If you want to see surf instead of box for an image, you have to set a state variable and draw the scene depending on the state. Reset the state variable after drawing the scene:
import pygame as pg
pg.init()
GREEN, WHITE, RED = (60, 120, 60), (255,255,255), (120, 60, 60)
screen = pg.display.set_mode((200, 200))
clock = pg.time.Clock()
box = pg.Surface((150, 150))
box.fill(GREEN)
surf = pg.Surface((50,50))
surf.fill(RED)
EVENT_SECOND_TIMER = pg.event.custom_type()
pg.time.set_timer(EVENT_SECOND_TIMER, 1000)
running = True
while running:
clock.tick(60)
draw_surf = False
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == EVENT_SECOND_TIMER:
draw_surf = True
screen.fill(WHITE)
screen.blit(box, (25, 25))
if draw_surf:
screen.blit(surf, (50, 50))
pg.display.update()
pg.quit()
I started a basic project with pygame and tried to set up the basics.
import pygame
BLACK = (0, 0, 0)
WHITE = (200, 200, 200)
SIZE = width, height = 500, 700
def main() -> None:
global SCREEN
pygame.init()
SCREEN = pygame.display.set_mode(SIZE)
pygame.display.set_caption('Tetronur')
running = True
SCREEN.fill(BLACK)
while running:
draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
def draw():
rect = pygame.Rect(100, 100, 400, 600)
pygame.draw.rect(SCREEN, WHITE, rect, 1)
if __name__ == '__main__':
main()
But the thing is, when I run the code, I get a screen looking like this:
PS: I use Python 3.9.6 and Pygame 2.0.1
Pygame draws to a hidden screen, you must call pygame.display.flip() or pygame.display.update() after drawing so that this hidden screen is displayed to the actual screen.
Im trying to draw a rectangle using python's pygame module and i don't really know what im doing wrong
import pygame
import sys
white = 255, 255, 255
red = 255, 0, 0
blue = 0, 0, 255
green = 0, 255, 0
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Rectangle test')
isRunning = pygame.get_init
while isRunning:
pygame.display.update()
pygame.event.get()
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
print(pygame.mouse.get_pos())
pygame.draw.rect(win, blue, (269, 165, 15, 15), 1)
win.fill((255, 255, 255))
pygame.quit()
Thats the code that im currently using, it opens up the window, the window fill works as it should and the event.get also works, it prints what it should print but it doesnt draw any rectangle
Your fill of the window is wiping out your rectangle. Just make the two calls in the opposite order:
win.fill((255, 255, 255))
pygame.draw.rect(win, blue, (269, 165, 15, 15), 1)
and you'll see your rectangle. I do when I run your code with this change applied.
This question already has an answer here:
Why is my pygame application loop not working properly?
(1 answer)
Closed 2 years ago.
I cant blit anything to my pygame screen using studio code on my mac. Is this a known issue or is there a way to fix it I am overlooking? I'm not getting any errors it's just not doing anything. I am kinda new to pygame so anything could work. here's my code:
pygame.display.set_caption('The space Simulator')
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
screen = pygame.display.set_mode([1000, 1000])
background = pygame.Surface((1000,1000))
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = background.get_rect().centerx
running=True
while running:
screen.blit(image, (textpos))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
You're just not flushing / updating the drawing primitives to the screen. Really you just need a pygame.display.update() or pygame.display.flip() after all your blits are done.
I guess you removed parts of your code to keep it simple for the question, but I put them back in for a working answer.
I also re-arranged the code, and removed the creation of the background Surface just to get the centre co-ordinate. This operation can be performed on the existing screen Surface.
import pygame
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
pygame.init()
pygame.display.set_caption('The space Simulator')
screen = pygame.display.set_mode([1000, 1000])
#image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
image = pygame.image.load('background.png' ).convert()
image = pygame.transform.smoothscale( image, (1000,1000) )
#background = pygame.Surface((1000,1000))
myfont = pygame.font.SysFont( None, 24 )
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = screen.get_rect().centerx
running=True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(image, (0,0))
screen.blit(text1, (textpos))
pygame.display.flip()
pygame.quit()
I need to create a clickable 8 by 8 grid in pygame.
Right now I have something like this:
#!/usr/bin/python2
#-------------------------------------------------------------------------------
# Imports & Inits
import pygame, sys
from pygame.locals import *
pygame.init()
#-------------------------------------------------------------------------------
# Settings
WIDTH = 105
HEIGHT = 105
FPS = 60
#-------------------------------------------------------------------------------
# Screen Setup
WINDOW = pygame.display.set_mode([WIDTH,HEIGHT])
CAPTION = pygame.display.set_caption('Test')
SCREEN = pygame.display.get_surface()
TRANSPARENT = pygame.Surface([WIDTH,HEIGHT])
TRANSPARENT.set_alpha(255)
TRANSPARENT.fill((255,255,255))
#-------------------------------------------------------------------------------
# Misc stuff
rect1 = pygame.draw.rect(SCREEN, (255, 255, 255), (0,0, 50, 50))
rect2 = pygame.draw.rect(SCREEN, (255, 255, 255), (0,55, 50, 50))
rect3 = pygame.draw.rect(SCREEN, (255, 255, 255), (55,0, 50, 50))
rect4 = pygame.draw.rect(SCREEN, (255, 255, 255), (55,55, 50, 50))
...
#-------------------------------------------------------------------------------
# Refresh Display
pygame.display.flip()
#-------------------------------------------------------------------------------
# Main Loop
while True:
pos = pygame.mouse.get_pos()
mouse = pygame.draw.circle(TRANSPARENT, (0, 0, 0), pos , 0)
# Event Detection---------------
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
if rect1.contains(mouse):
rect1 = pygame.draw.rect(SCREEN, (155, 155, 155), (0,0, 50, 50))
pygame.display.flip()
Now, in my original code, I have far more rectangles and I need a way to do something like this:
for i in rectangles:
if i hasbeenclickedon:
change color
Obviously, my solution is far too static.
So, how could I accomplish this?
Though your solution is indeed a little cumbersome, I'd first say
rectangles = (rect1, rect2, ...)
then you can iterate over them as intended.
Try sth like
pos = pygame.mouse.get_pos()
for rect in rectangles:
if rect.collidepoint(pos):
changecolor(rect)
You'd have to implement the changecolor method, of course.
Generally, I'd recommend creating a class for you clickable fields that defines a method changecolor.
Simple 'human' colors:
Color("red")
Color(255,255,255)
Color("#fefefe")
Use:
import pygame
# This makes event handling, rect, and colors simpler.
# Now you can refer to `Sprite` or `Rect()` vs `pygame.sprite.Sprite` or `pygame.Rect()`
from pygame.locals import *
from pygame import Color, Rect, Surface
pygame.draw.rect(screen, Color("blue"), Rect(10,10,200,200), width=0)
pygame.draw.rect(screen, Color("darkred"), Rect(210,210,400,400), width=0)