I'm new to Pygame and I'm trying to create a flappy bird clone for my first Pygame project. I have a decent starting point but I'm running into an issue when I try to add a texture to a rectangle. I have two rectangles representing the top and bottom pipes. When I leave them as just rectangles the size of the rectangles is properly fitted how I want them. However, when I try to add the pipe texture to the rectangle, they texture doesn't properly map to the rectangle. My code is:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((250,500))
clock = pygame.time.Clock()
run = True
background = pygame.image.load('graphics/background-day.png')
ground = pygame.image.load('graphics/base.png')
birdRec = pygame.Rect(0, 0, 30, 24)
birdRec.center = (100,300)
bottomPipeHeight = random.randrange(50, 350)
pipeRecBottom = pygame.Rect(0,0, 50, bottomPipeHeight)
pipeRecBottom.midbottom = (150, 450)
pipeRecTop = pygame.Rect(0,0,50,500)
pipeRecTop.midbottom = pipeRecBottom.midtop
pipeRecTop.y -= 100
gravity = 0
pipeBottomTexture = pygame.image.load("graphics/pipe-green.png").convert_alpha()
while run:
#poll for events
#pygame.QUIT event means the user clicked x
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
gravity += .1
birdRec.y += gravity
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
birdRec.y -= 7
gravity = 0
if keys[pygame.K_s]:
birdRec.y += 2
if keys[pygame.K_d]:
birdRec.x += 2
if keys[pygame.K_a]:
birdRec.x -= 2
pipeRecBottom.x -= 1
pipeRecTop.x -= 1
if(birdRec.colliderect(pipeRecTop) or birdRec.colliderect(pipeRecBottom)):
print("Collision")
# RENDER GAME
screen.blit(background,(0,0))
screen.blit(ground,(0,450))
#PROBLOMATIC AREA BEGIN
pygame.draw.rect(screen, "Black", pipeRecBottom)
#screen.blit(pipeBottomTexture, pipeRecBottom)
#PROBLOMATIC AREA END
pygame.draw.rect(screen, "Green", pipeRecTop)
pygame.draw.rect(screen, "Red", birdRec)
if pipeRecBottom.right < -30:
pipeRecBottom.height = random.randrange(50,350)
pipeRecBottom.bottomleft = (280,450)
if pipeRecTop.right < -30:
pipeRecTop.midbottom = pipeRecBottom.midtop
pipeRecTop.y -= 100
pygame.display.flip()
clock.tick(60)
pygame.quit()
I tried to initially comment out the first line in the problematic area and simply write the second line. My thought process was that I have the rectangle location calculated so I just want to slap the texture on top of it.
But instead, the texture is offset from the rectangle, usually resulting in the bottom of the pipe going beneath the floor. I think the top of the bottom pipe is being mapped to the correct point, I would just like to be able to stop the texture from going into the floor.
Here's a screenshot of the correct behavior:

Here's a screenshot of the incorrect behavior, notice the pipe clipping into the floor:

The difference in height between the two is because the height is being randomly generated. If I fix the height value, the clipping issue is still there.
blithas third argument to define displayedareafrom original image.Doc: blit
It can crop any part of image so it needs own
rector tuple(x,y, width, height)You can't use
pipeRecBottomas third value because it needsx=0, y=0to keep top of image.BTW:
In your code other solution is to blit
groundafterpipes.And this doesn't need to change
heightinpipe.Full working example:
I use
Surfaceto create image - so everyone can simply copy and run it.