Creating a bold line in pygame - python

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()

Related

It wont draw my rectangle (pygame on prdroid)

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()

Python pygame need help on drawing rectangles

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.

Pygame in StudioCode on mac wont blit [duplicate]

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()

How to make transparent pygame.draw.circle [duplicate]

This question already has answers here:
Draw a transparent rectangles and polygons in pygame
(4 answers)
Closed 2 years ago.
How to make pygame.draw.circle transparent (add alpha level), as for "surface" "set_alpha"?
I found a solution only in changing the color of pygame.draw.circle to less bright
You've to use a pygame.Surface. Create a Surface with a per pixel alpha image format. e.g:
radius = 100
circle = pygame.Surface((radius*2, radius*2), pygame.SRCALPHA)
And draw a transparent circle on it. The color of the circle has to have an alpha channel < 255 (e.g. 128):
pygame.draw.circle(circle, (255, 0, 0, 128), (radius, radius), radius)
blit() the Surface to the window. e.g.:
window.blit(circle, (100, 100))
Example:
import pygame
pygame.init()
wndsize = (400, 400)
window = pygame.display.set_mode(wndsize)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
pygame.draw.rect(window, (0, 0, 255), (0, 0, 200, 400))
radius = 100
circle = pygame.Surface((radius*2, radius*2), pygame.SRCALPHA)
pygame.draw.circle(circle, (255, 0, 0, 128), (radius, radius), radius)
window.blit(circle, (100, 100))
pygame.display.flip()
You can create surface with alpha channel
surface1 = screen.convert_alpha()
fill it with transparent color -
surface1.fill([0,0,0,0])
draw circle using color [R,G,B,Alpha]
pygame.draw.circle(surface1, (255, 0, 0, 128), (300, 300), 200)
and blit it on screen
screen.blit(surface1, (0,0))
But alpha always mix object color with background color so it makes it less bright.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))#, depth=32)
surface1 = screen.convert_alpha()
surface1.fill([0,0,0,0])
pygame.draw.circle(surface1, (255, 0, 0, 128), (325, 250), 100)
surface2 = screen.convert_alpha()
surface2.fill([0,0,0,0])
pygame.draw.circle(surface2, (0, 255, 0, 128), (475, 250), 100)
surface3 = screen.convert_alpha()
surface3.fill([0,0,0,0])
pygame.draw.circle(surface3, (0, 0, 255, 128), (400, 350), 100)
screen.fill([255,255,255]) # white background
screen.blit(surface1, (0,0))
screen.blit(surface2, (0,0))
screen.blit(surface3, (0,0))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
pygame.quit()
Example on GitHub: furas/python-examples/pygame/transparency

Creating a rect grid in Pygame

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)

Categories

Resources