0

In the game our character jumps on platforms in order to move higher. My game resolution is 600x600. I have a picture that's going to be my background and has resolution of 600x1000. I'm trying to setup a background that progressively moves or changes as we go higher. So that when I get higher, the background shows not the bottom of the original picture (Y-axis 400-1000), but shows, for example, the middle part of the picture (Y-axis 200-800). So far I haven't been able to figure out a way how to blit 600x600 image from 600x1000 image. It's like I want to cut the upper part of the original picture so that it fits. Could you help me with this?

I'm sorry if I didn't explain it properly. How do I blit a non-distorted, smaller-sized image from a bigger-sized image. The background I have in mind should look like from an android game called "Happy Jump".

Thank you for reading. Have a good day.

Nedeljak
  • 1
  • 1

1 Answers1

0

enter image description here

import pygame
pygame.init()
run = True
surf = pygame.display.set_mode((600, 600))
img = pygame.image.load('image.jpg')
down = -400
center_of_screen = 300
maximum_limit_of_down = 0

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((40, 40))
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.center = (300, 570)
        self.isJump = False
        self.jumpCount = 10

    def update(self):
        global down
        key = pygame.key.get_pressed()
        if key[pygame.K_UP] and not self.isJump:
            self.isJump = True
        if self.isJump:
            if self.rect.y > center_of_screen or (down >= maximum_limit_of_down):
                if self.jumpCount >= 0:
                    self.rect.y = self.rect.y - (self.jumpCount ** 2) * 0.5
                    self.jumpCount -= 1
                else:
                    self.jumpCount = 10
                    self.isJump = False
                if down >= maximum_limit_of_down:
                    down = maximum_limit_of_down
            else:
                if self.jumpCount >= 0:
                    down = down + (self.jumpCount ** 2) * 0.5
                    self.jumpCount -= 1
                else:
                    self.jumpCount = 10
                    self.isJump = False


player = Player()
all_sprite = pygame.sprite.Group()
all_sprite.add(player)
clock = pygame.time.Clock()
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    surf.fill((0, 0, 0))
    surf.blit(img, (0, down))
    all_sprite.update()
    all_sprite.draw(surf)
    pygame.display.update()

This will help you, sorry for the Image i used whatever i found

Mohammad Sartaj
  • 111
  • 1
  • 4
  • I just needed to realize that surf.blit(img, (0, -400)) actually works the way we want it to work. That's the only thing i couldn't figure out. That's what I was looking for... – Nedeljak Jan 06 '20 at 19:29