How can i creat a multiple rect by pygame? [duplicate] - python

This question already has answers here:
Pygame Drawing a Rectangle
(6 answers)
Closed 2 years ago.
i want to creat a game like Pong but just one player from downside, and multiple box from up side, when the ball hit the box, the box will disappear. I can't remember name of that game but i don't know hot to create multiple rectangles. I can draw it by easy way

Use pygame.draw.rect to draw a rectangle:
pygame.draw.rect(screen, color, (x, y, width, height))
Use nested loops to draw create the locations of multiple rectangles
rectwidth = 40
rectheight = 40
rectdist = 10
block_positions = []
for i in range(10):
for j in range(2)
x = 100 + i * (rectdist + rectwidth)
y = 100 + j * (rectdist + rectheight)
block_positions.append((x, y))
Draw the rectangles in a loop:
for x, y in block_positions:
pygame.draw.rect(screen, (255, 255, 255), (x, y, rectwidth, rectheight))

Related

I can't draw a filled rectangle [duplicate]

This question already has answers here:
Pygame Drawing a Rectangle
(6 answers)
Draw a transparent rectangles and polygons in pygame
(4 answers)
Closed 2 years ago.
I am trying to draw a screen with a filled in box in the center. I am using pygame.Surface.fill() for the box but it gives this error: TypeError: descriptor 'fill' for 'pygame.Surface' objects doesn't apply to a 'tuple' object Here is my code:
my_color = (0, 0, 0)
my_rect = (100, 100, 200, 150)
box = pygame.Surface.fill(my_color, my_rect)
surf.blit(box)
You just need to do it in two steps. First create the surface, then fill it. Creating a surface makes a separate image, so it takes a size not a rectangle.
my_color = (0, 0, 0)
my_rect = (100, 100, 200, 150)
my_size = ( 200, 150 )
box = pygame.Surface( my_size )
box.fill( my_colour )
surf.blit( box, my_rect )
If you just want to draw a rectangle, you can use the drawing functions, which use the base surface directly:
pygame.draw.rect( surf, my_color, my_rect )

Pygame: Drawing a border of a rectangle [duplicate]

This question already has an answer here:
Pygame is running slow
(1 answer)
Closed 2 years ago.
I am creating a 3d pong game using pygame. I wanted to add a thick black border layer to the rectangle to make it more stylish. Here's what I tried:
pygame.draw.rect(screen, (0,0,255), (x,y,150,150), 0)
pygame.draw.rect(screen, (0,0,0), (x-1,y-1,155,155), 1)
pygame.draw.rect(screen, (0,0,0), (x-2,y-2,155,155), 1)
pygame.draw.rect(screen, (0,0,0), (x-3,y-3,155,155), 1)
pygame.draw.rect(screen, (0,0,0), (x-4,y-4,155,155), 1)
It worked but as the game I am trying to create is a 3d game this method was time consuming. Please tell me if there is any inbuilt method in pygame to draw borders around a rectangle. Sorry for my English.
You could also put it in a function, and make it more concise with for loops. First, you'll note that the four rectangles you drew were in a nice, easy pattern, so you could compact the drawing of the four rectangles like this:
pygame.draw.rect(surface, (0,0,255), (x,y,150,150), 0)
for i in range(4):
pygame.draw.rect(surface, (0,0,0), (x-i,y-i,155,155), 1)
Then, because pygame draw functions do not need to be run in the global scope, you can put all this into a function:
def drawStyleRect(surface):
pygame.draw.rect(surface, (0,0,255), (x,y,150,150), 0)
for i in range(4):
pygame.draw.rect(surface, (0,0,0), (x-i,y-i,155,155), 1)
Then, in your mainloop, all you have to do is run:
while not done:
...
drawStyleRect(screen) # Or whatever you named the returned surface of 'pygame.display.set_mode()'
...
You could even put the drawing function in a separate module, if you really wanted to.
Create a function that creates a pygame.Surface object with per pixel alpha (SRCALPHA) and draw the rectangle and the border on the surface:
def create_rect(width, height, border, color, border_color):
surf = pygame.Surface((width+border*2, height+border*2), pygame.SRCALPHA)
pygame.draw.rect(surf, color, (border, border, width, height), 0)
for i in range(1, border):
pygame.draw.rect(surf, border_color, (border-i, border-i, width+5, height+5), 1)
return surf
Create all the surfaces before the main application loop and blit them in the loop:
rect_surf1 = create_rect(150, 150, 5, (0, 0, 255), (0, 0, 0))
# [...]
run = True
while run:
# [...]
screen.blit(rect_surf1, (x, y))
# [...]

how can i make two images collide with each other in pygame? [duplicate]

This question already has an answer here:
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I want a code that whenever the player and the fruit collide... to just end the game.
player:
playerImg = pygame.image.load('trash.png')
x = 370
y = 480
def player(x, y):
screen.blit(playerImg, (x, y))
fruit1:
fruitImg = pygame.image.load('001-apple.png')
fruit_x = random.randrange(0, width)
fruit_y = -600
fruit_speed = 5
fruit_width = 100
fruit_height = 100
def fruit(fruit_x, fruit_y, fruit_width, fruit_height):
screen.blit(fruitImg, (fruit_x, fruit_y, fruit_width, fruit_height))
Use pygame.Rect objects and .colliderect():
fruit_rect = fruitImg.get_rect(topleft = (fruit_x, fruit_y))
player_rect = playerImg.get_rect(topleft = (x, y))
if player_rect.colliderect(fruit_rect):
print("collide")
colliderect returns True if 2 rectangles are intersecting.
get_rect() returns a pygame.Rect which contains the size of a pygame.Surface. This rectangle will always start at (0, 0). The location of the rectangle can be set a keyword argument (e.g. topleft = (x, y))

pygame surface rotating in a nonsensical way [duplicate]

This question already has answers here:
How do I rotate an image around its center using Pygame?
(6 answers)
Closed 3 years ago.
I'm trying to build a simple game and so far I'm just learning the basics.
I'm trying to draw a rectangle tilted 45°, but I couldn't figure how to keep it centered even after reading some previous question here on SO.
So I tried making a rectangle that keeps rotating; this is the corresponding code.
alpha=0
while True:
w, h=screen.get_size()
s=pygame.Surface((w/2, h))
pygame.draw.rect(s, col, (300,150,50,10))
s=pygame.transform.rotozoom(s, alpha, 1)
alpha+=2
s.set_colorkey((0,0,0))
background.blit(s, (0, 0))
# flip screen, etc
The surface should keep rotating forever around some center (I wanted to use this to understand clearly which it was), but it moves in an irregular way.
This is the video of what happens [...].
EDIT:marked as duplicate, I'm removing the video link
Rotating a rectangle (not image) in pygame provides you an answer:
Example code:
import pygame as py
# define constants
WIDTH = 500
HEIGHT = 500
FPS = 30
# define colors
BLACK = (0 , 0 , 0)
GREEN = (0 , 255 , 0)
# initialize pygame and create screen
py.init()
screen = py.display.set_mode((WIDTH , HEIGHT))
# for setting FPS
clock = py.time.Clock()
rot = 0
rot_speed = 3
# define a surface (RECTANGLE)
image_orig = py.Surface((100 , 100))
# for making transparent background while rotating an image
image_orig.set_colorkey(BLACK)
# fill the rectangle / surface with green color
image_orig.fill(GREEN)
# creating a copy of orignal image for smooth rotation
image = image_orig.copy()
image.set_colorkey(BLACK)
# define rect for placing the rectangle at the desired position
rect = image.get_rect()
rect.center = (WIDTH // 2 , HEIGHT // 2)
# keep rotating the rectangle until running is set to False
running = True
while running:
# set FPS
clock.tick(FPS)
# clear the screen every time before drawing new objects
screen.fill(BLACK)
# check for the exit
for event in py.event.get():
if event.type == py.QUIT:
running = False
# making a copy of the old center of the rectangle
old_center = rect.center
# defining angle of the rotation
rot = (rot + rot_speed) % 360
# rotating the orignal image
new_image = py.transform.rotate(image_orig , rot)
rect = new_image.get_rect()
# set the rotated rectangle to the old center
rect.center = old_center
# drawing the rotated rectangle to the screen
screen.blit(new_image , rect)
# flipping the display after drawing everything
py.display.flip()
py.quit()

How do I color in specific blocks for a chess board? [duplicate]

This question already has an answer here:
Nested for loop chess board coloring not working Python
(1 answer)
Closed 1 year ago.
I made a chess board, but I need help with coloring the different squares. I only have white squares but I need black squares. Here is my code.
import pygame
def board():
width=480 # measurements for the window
height=480
block_size=59
window = pygame.display.set_mode((width,height))
background_color = (0,0,0) # This is how I make the lines
window.fill(background_color)
for y in range(height):
for x in range(width):
rect = pygame.Rect(x*(block_size+1), y*(block_size+1), block_size, block_size)
pygame.draw.rect(window, (255,255,255), rect) # Leaves space for lines to be visible.
pygame.display.flip()
board()
# It's just the board.
I already know I violated PEP 8.
You can do it like: I changed your coord-manipulation to benefit directly from the given range values not using a mult inside it. Color is flipped on each rect drawn and also on line-change to get alternating row colorings:
import pygame
def board():
def flipColor(color):
white = (240,240,240)
black = (30,30,30)
if not color or color == white:
color = black
else:
color = white
return color
width=480 # measurements for the window
height=480
block_size= 60
window = pygame.display.set_mode((width,height))
background_color = (0,0,0) # This is how I make the lines
window.fill(background_color)
c = None
pygame.draw.rect(window,(255,0,0),pygame.Rect(0,0,width,height)) # red background
for y in range(0,height,block_size):
c = flipColor(c)
for x in range(0,width,block_size):
c = flipColor(c)
rect = pygame.Rect(x , y , x+block_size , y+block_size )
pygame.draw.rect(window, c , rect, 0) # Leaves space for lines to be visible.
for i in range(0,height+1,block_size):
pygame.draw.line(window,(233,33,187),(i,0),(i,width),2)
pygame.draw.line(window,(233,33,187),(0,i),(height,i),2)
pygame.draw.line(window,(233,33,187),(height-2,0),(height-2,width),2) # fix for out of window line
pygame.draw.line(window,(233,33,187),(0,width-2),(height,width-2),2) # fix for out of wondow line
pygame.display.flip()
board()
You can also use itertools.cycle, pass an iterable with the colors and then just call next to cycle through them. I'd create the background surface when the program starts and then just blit it in the while loop. If you need a background with extra lines, just draw them on the same background surface as well or create a copy.
import itertools
import pygame as pg
pg.init()
screen = pg.display.set_mode((480, 480))
clock = pg.time.Clock()
width, height = screen.get_size()
block_size = 60
# Create a surface onto which we'll blit the rectangles.
background = pg.Surface((width, height))
colors = itertools.cycle((pg.Color('white'), pg.Color('black')))
for y in range(0, height, block_size):
for x in range(0, width, block_size):
rect = (x, y, block_size, block_size)
pg.draw.rect(background, next(colors), rect)
next(colors) # Skip the next color.
# Then you can just blit the background in the while loop.
screen.blit(background, (0, 0))
pg.display.flip()
You can also calculate the color.
If the index of the column and row are both equal or not equal the color is white, else black.
For example field A8:
Index of A is 0 --> equal
8 --> equal
==> Field is white
Example field A1:
Index of A is 0 --> equal
1 --> unequal
==> Field is black

Categories