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.
Related
Hello I'm creating a sudoku game in pygame and while making the board I wanted to make all lines black but some thicker than others like so:
Do any of you know of a way to do this?
The last argument of pygame.draw.line is optional and specifies the thickness of the line:
draw a straight line
line(surface, color, start_pos, end_pos, width=1) -> Rect
...
width (int) -- (optional) used for line thickness
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((255, 255, 255))
for i in range(10):
w = 3 if i == 3 or i == 6 else 1
pygame.draw.line(window, (0, 0, 0), (50, 50+i*30), (320, 50+i*30), w)
pygame.draw.line(window, (0, 0, 0), (50+i*30, 50), (50+i*30, 320), w)
pygame.display.flip()
pygame.quit()
exit()
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()
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()
Please help me i've started very recently writting programs in pygame and i can pass this obstacle i can't find any help online. So to be more specific what i need is something that when i press maybe the tab key just once beacause if i put KEYDOWN i would have to me always pressing the key i would get out of the starting background and then would appear a message. Thank you for your time.
import pygame
import random
perguntaa = random.randint(1,2)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
ORANGE = (255, 154, 23)
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Quem quer ser MilionĂ¡rio")
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
screen.fill(ORANGE)
pygame.draw.rect(screen, BLACK, [225, 200, 250, 100])
font = pygame.font.SysFont('cambria', 100, False, False)
text = font.render("Play", True, WHITE)
screen.blit(text, [225, 200])
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
screen.fill(ORANGE)
pygame.draw.rect(screen, BLACK, [0, 50, 700, 100])
pygame.display.flip()
clock.tick(30)
pygame.quit()
i believe this is what you are looking for
bgs = [Orange, Black, Green]
current_bg = 0
if event.key == pygame.K_Q:
current_bg += 1
if current_bg > len(bgs)-1:
current_bg = 0
screen.fill(bgs[current_bg] #alternately screen.blit(bgs[current_bg]) if bgs indexes are instead sprites
screen.update()#flip and update are interchangeable however update is more commonly used
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)