With pygame, I created a 20x20 pixel window and added a 2x2 pixel rectangle.
When I run the program, the window size is super small and I can barely see the rectangle. How can I increase the window size whilst keeping the number of pixels constant, i.e. increase the pixel size? I am aware of this similar question, but there a somewhat more complicated case is discussed.
import pygame
screen_width, screen_height = 20, 20
x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width, screen_height))
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill(black)
pygame.draw.rect(win, white, (x, y, rect_width, rect_height))
pygame.display.update()
pygame.quit()
Don't draw directly to the screen, but to another Surface.
Then scale that new Surface to the size of the screen and blit it onto the real screen surface.
Here's an example:
import pygame
screen_width, screen_height = 20, 20
scaling_factor = 6
x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width*scaling_factor, screen_height*scaling_factor))
screen = pygame.Surface((screen_width, screen_height))
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill(black)
pygame.draw.rect(screen, white, (x, y, rect_width, rect_height))
win.blit(pygame.transform.scale(screen, win.get_rect().size), (0, 0))
pygame.display.update()
pygame.quit()
Related
Here is the board I drew using pygame: https://i.stack.imgur.com/Hne6A.png
I'm facing a bug with the thickness of the last two lines as I marked them on the image. I believe there is something wrong with the if statement in my code but I can't quite figure it out, it just won't take effect.
here is the code that has drawn the board above:
import pygame, sys
pygame.init()
width, height = 750, 750
rows, cols = 9, 9
BLACK = (0,0,0)
screen = pygame.display.set_mode((width, height))
screen.fill((255, 255, 255, 255))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
surf = pygame.Surface((600, 600))
surf.fill((255,255,255))
padding = surf.get_width()/9
for i in range(rows+1):
if i % 3 == 0:
thick = 4
else:
thick = 1
pygame.draw.line(surf, BLACK, (0, i*padding), (width, i*padding), width=thick)
pygame.draw.line(surf, BLACK, (i*padding, 0), (i*padding, height), width=thick)
surf_center = (
(width-surf.get_width())/2,
(height-surf.get_height())/2
)
screen.blit(surf, surf_center)
pygame.display.update()
pygame.display.flip()
To draw a thick line, half the thickness is applied to both sides of the line. The simplest solution is to make the target surface a little larger and add a small offset to the coordinates of the lines.
For performance reasons, I also recommend creating the surface before the application loop and continuously blit it in the application loop:
import pygame, sys
pygame.init()
width, height = 750, 750
rows, cols = 9, 9
BLACK = (0,0,0)
screen = pygame.display.set_mode((width, height))
surf_size = 600
surf = pygame.Surface((surf_size+4, surf_size+4))
surf.fill((255,255,255))
padding = surf_size/9
for i in range(rows+1):
thick = 4 if i % 3 == 0 else 1
offset = i * padding + 1
pygame.draw.line(surf, BLACK, (0, offset), (width, offset), width=thick)
pygame.draw.line(surf, BLACK, (offset, 0), (offset, height), width=thick)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255, 255, 255, 255))
screen.blit(surf, surf.get_rect(center = screen.get_rect().center))
pygame.display.update()
pygame.display.flip()
pygame.quit()
sys.exit()
The code below should do the trick. The thing is that the width in pygame.draw.line is drawn in the middle of the line. So, the border lines are actually cropped in half because the half on one side and another half on the other side are not taken into account. But the catch is that if the thickness is even there is an offset of 1 on the borders as the width is larger on one of the sides.
Even if the thickness (bold_think) is raised or lowered the algorithm below will work flawlessly without any gaps outside the grid.
Thickness of 4
Thickness of 8
Thinkness of 15
import pygame
import sys
pygame.init()
width, height = 750, 750
rows, cols = 9, 9
BLACK = (0, 0, 0)
screen = pygame.display.set_mode((width, height))
screen.fill((255, 255, 255, 255))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
surf = pygame.Surface((600, 600))
surf.fill((255, 255, 255))
bold_thick = 4
# Calculates the offset of 1 for the even widths
thick_offset = (bold_thick - 1) % 2
offset = (bold_thick / 2) - thick_offset
# The padding should take into account the borders
padding = (surf.get_width() - bold_thick) / 9
for i in range(rows+1):
if i % 3 == 0:
thick = bold_thick
else:
thick = 1
pygame.draw.line(surf, BLACK, (0, i*padding + offset),
(width, i*padding + offset), width=thick)
pygame.draw.line(surf, BLACK, (i*padding + offset, 0),
(i*padding + offset, height), width=thick)
surf_center = (
(width-surf.get_width())/2,
(height-surf.get_height())/2
)
screen.blit(surf, surf_center)
pygame.display.update()
pygame.display.flip()
This is my code atm. It outputs a square right on top of a circle. I want to make the square sort of "turn into" the circle.
import pygame
GREEN = (0,255,0)
pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
quitVar = True
while quitVar == True:
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (100,100,200,200))
pygame.draw.circle(screen, GREEN, (200,200),100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitVar = False
pygame.display.update()
pygame.quit()
You can draw a rectangle with round corners in Pygame (see Setting a pygame surface to have rounded corners). Animate the corner radius from 0 to the radius of the circle:
import pygame
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
clock = pygame.time.Clock()
radius = 0
step = 1
quitVar = True
while quitVar == True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitVar = False
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (100, 100, 200, 200), border_radius = radius)
pygame.display.update()
radius += step
if radius <= 0 or radius >= 100:
step *= -1
pygame.quit()
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
I tried to make a clone of a game but the rectangle (which is the basic shape that I will need)does not appear. What did I do wrong or its just pygame going crazy?
code:
# importing modules
import pygame
import sys
import random
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.display.quit()
pygame.quit()
exit()
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# cube
if running == True:
pygame.draw.rect(screen, blue, (395, 0, 10, 10)),
pygame.draw.rect(screen, blue, (395, 10, 10, 10)),
pygame.draw.rect(screen, blue, (395, 20, 10, 10)),
clock.tick(60)
and also how am I going to make it empty and 3d. I know I am asking for a lot but I believe someone can explain
You have to draw the scene in the application loop:
# importing modules
import pygame
import sys
import random
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.draw.rect(screen, blue, (395, 0, 10, 10))
pygame.draw.rect(screen, blue, (395, 10, 10, 10))
pygame.draw.rect(screen, blue, (395, 20, 10, 10))
pygame.display.update()
clock.tick(60)
pygame.display.quit()
pygame.quit()
exit()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
I created a window with a width and height of 800 pixels using pygame then drew rectangles with size 32 to make the window a 25x25 grid. What I want to do is change the color of the rectangle I click to change.
My Code:
def createGrid():
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
BLOCK_SIZE = 32
WHITE = (255,255,255)
pygame.init()
frame = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("PathFinder")
frame.fill(WHITE)
for y in range(SCREEN_HEIGHT):
for x in range(SCREEN_WIDTH):
rect = pygame.Rect(x*BLOCK_SIZE, y*BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1)
pygame.draw.rect(frame, (0,250,0), rect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
First you should create list with all rects and they colors.
Next you should draw all rect inside while loop.
And finally you have to use event.MOUSEBUTTONDOWN to get mouse click and compare mouse position with every rect on list and change color on list.
import pygame
import sys
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
BLOCK_SIZE = 32
WHITE = (255,255,255)
pygame.init()
frame = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("PathFinder")
# create list with all rects
all_rects = []
for y in range(0, SCREEN_HEIGHT, BLOCK_SIZE):
row = []
for x in range(0, SCREEN_WIDTH, BLOCK_SIZE):
rect = pygame.Rect(x, y, BLOCK_SIZE-1, BLOCK_SIZE-1)
row.append([rect, (0, 255, 0)])
all_rects.append(row)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# check which rect was clicked and change its color on list
for row in all_rects:
for item in row:
rect, color = item
if rect.collidepoint(event.pos):
if color == (0, 255, 0):
item[1] = (255, 0, 0)
else:
item[1] = (0, 255, 0)
# draw all in every loop
frame.fill(WHITE)
for row in all_rects:
for item in row:
rect, color = item
pygame.draw.rect(frame, color, rect)
pygame.display.flip()
Eventually you could draw all rects before loop and use event to draw new rect on clicked rect. But you still need list with all rects to check which rect was click and what is its color.
It depends on what your code is intended to do? This example shows how to paint to the primary display surface but doesn't keep track of which colour is where.
import pygame
white = (255, 255, 255)
red = (255, 0, 0)
size = 32
pygame.init()
s = pygame.display.set_mode((800, 800))
s.fill(white)
# press escape to exit example
while True:
e = pygame.event.get()
if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
x = int(pygame.mouse.get_pos()[0] / size) * size
y = int(pygame.mouse.get_pos()[1] / size) * size
if pygame.mouse.get_pressed()[0]:
pygame.draw.rect(s, red, (x, y, size, size), 0)
pygame.display.update()
pygame.time.Clock().tick(60)
pygame.quit()
I am new to python and made an algorithm I would like to visualize on a screen with pygame. This is my code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Test screen")
x = 50
y = 50
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()
But this always displays a black or kinda dark gray screen. Does anyone know what I'm doing wrong? I have tried multiple tutorials and they all give me the same screen.
I am using MacOS 10.14
You need to call the fill function on the win object. The background is always going to be black by default. The below example shows how to turn it to white.
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Test screen")
x = 50
y = 50
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(100)
## enter color here
win.fill((255,255,255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()