Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Pygame: How to map png texture 1-1 to rectangle

$
0
0

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 pygameimport randompygame.init()screen = pygame.display.set_mode((250,500))clock = pygame.time.Clock()run = Truebackground = 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.midtoppipeRecTop.y -= 100gravity = 0pipeBottomTexture = 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.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>