0

I'm beginner in this site. I have been doing the game using pygame from python since half of November. I have problems with 2 things: how to make a gravity and platforms, which sprite doesn't overcome right? I show full code to you can show me what to do right. Please, help me because it is the one of the hardest tasks I have ever had. Full code:

import pygame
from os import *
import random
import time

WIDTH = 1300
HEIGHT = 660
FPS = 60
usr_y = HEIGHT - 415
usr_x = WIDTH - 1150
pictures_dir = path.join(path.dirname(__file__), 'Pictures')


BLUE = (0, 255, 255)
GREEN = (34, 89, 76)
NOTGREEN = (0, 128, 128)
WHITE1 = (255, 255, 255)
BLACK = (0, 0, 0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mini-games")
clock = pygame.time.Clock()
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
day_jpg = pygame.image.load('День.jpg')
day_rect = day_jpg.get_rect()
player_img = pygame.image.load('Sonic.actionp1.png')
pygame.mixer.music.load('Фоновая музыка.mp3')
pygame.mixer.music.play(-1)
clouds_jpg = pygame.image.load('Clouds.jpg').convert()
cloudsbig_jpg = pygame.image.load('Cloudsbig.jpg').convert()    
pictures_mouse = [pygame.image.load('mouse_level1.1.png'), pygame.image.load('mouse_level1.2.png'), pygame.image.load('mouse_level1.3.png'), pygame.image.load('mouse_level1.2.png')]
pictures_fire = [pygame.image.load('Fire.png'), pygame.image.load('Fire2.png'), pygame.image.load('Fire3.png')]

ADDCLOUD = pygame.USEREVENT + 1
pygame.time.set_timer(ADDCLOUD, 3499)

ADDCLOUDBIG = pygame.USEREVENT + 2
pygame.time.set_timer(ADDCLOUDBIG, 4099)

ADDENEMY = pygame.USEREVENT + 3
pygame.time.set_timer(ADDENEMY, 3999)

ADDPLATFORM_1 = pygame.USEREVENT + 4
pygame.time.set_timer(ADDPLATFORM_1, 3797)

ADDPLATFORM_2 = pygame.USEREVENT + 5
pygame.time.set_timer(ADDPLATFORM_2, 4756)

ADDPLATFORM_3 = pygame.USEREVENT + 6
pygame.time.set_timer(ADDPLATFORM_3, 5578)

NUMBERS_1 = [250, 300, 420, 500]

NUMBERS_2 = [270, 330, 370, 450]

NUMBERS_3 = [100, 140, 180, 220]

jump = False
counter = -30

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        pygame.sprite.Sprite.__init__(self)
        self.image = player_img
        self.image.set_colorkey(NOTGREEN)
        self.rect = self.image.get_rect()
        self.rect.centerx = usr_x
        self.rect.centery = usr_y
        self.y = self.rect.y

    def update(self):
        self.rect.y = round(self.y)
        self.speedx = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speedx = -8
        if keystate[pygame.K_RIGHT]:
            self.speedx = 8
        self.rect.x += self.speedx
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT
        if self.rect.top < 0:
            self.rect.top = 0


    
def make():
    global  usr_y, counter, jump
    if counter >=  -30:
        player.y -= counter / 2.7
        counter -= 1
    else:
        counter = 30
        jump = False


class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.images = []
        self.images.append(pictures_mouse[0])
        self.images.append(pictures_mouse[1])
        self.images.append(pictures_mouse[2])
        self.images.append(pictures_mouse[3])
        self.index = 0
        self.image = self.images[self.index]
        self.rect = self.image.get_rect()
        self.rect.centerx = random.randint(WIDTH, WIDTH)
        self.rect.centery = random.randint(150, HEIGHT - 200)

    def update(self):
        self.rect.move_ip(-2, 0)
        if self.rect.right <= 0:
            self.kill()
        
        self.index += 1
        if self.index >= 100:
            self.index = 0

        
        
        self.image = self.images[self.index // 25]
        self.image.set_colorkey(WHITE1)
        
        
class Cloud(pygame.sprite.Sprite):
    def __init__(self):
        super(Cloud, self).__init__()
        self.image = clouds_jpg
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = random.randint(WIDTH, WIDTH)
        self.rect.centery = random.randint(50, HEIGHT - 450)

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Cloud1(pygame.sprite.Sprite):
    def __init__(self):
        super(Cloud1, self).__init__()
        self.image = cloudsbig_jpg
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = random.randint(WIDTH, WIDTH)
        self.rect.centery = random.randint(50, HEIGHT - 450)

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

platform_images = []
platform_list =['platform1.png', 'platform2.png', 'platform3.png', 'platform1start.png', 'platform2start.png', 'platform3start.png']
for Pictures in platform_list:
    platform_images.append(pygame.image.load(path.join(pictures_dir, Pictures)).convert())


class Platform1(pygame.sprite.Sprite):
    def __init__(self):
        super(Platform1, self).__init__()
        self.image = platform_images[0]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = random.randint(WIDTH, WIDTH)
        self.rect.centery = random.choice(NUMBERS_1)

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Platform2(pygame.sprite.Sprite):
    def __init__(self):
        super(Platform2, self).__init__()
        self.image = platform_images[1]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = random.randint(WIDTH, WIDTH)
        self.rect.centery = random.choice(NUMBERS_2)

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Platform3(pygame.sprite.Sprite):
    def __init__(self):
        super(Platform3, self).__init__()
        self.image = platform_images[2]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = random.randint(WIDTH, WIDTH)
        self.rect.centery = random.choice(NUMBERS_3)

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Start_Platform1(pygame.sprite.Sprite):
    def __init__(self):
        super(Start_Platform1, self).__init__()
        self.image = platform_images[3]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = 400
        self.rect.centery = 500

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Start_Platform2(pygame.sprite.Sprite):
    def __init__(self):
        super(Start_Platform2, self).__init__()
        self.image = platform_images[4]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = 750
        self.rect.centery = 400

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Start_Platform3(pygame.sprite.Sprite):
    def __init__(self):
        super(Start_Platform3, self).__init__()
        self.image = platform_images[5]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = 930
        self.rect.centery = 300

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Start_Platform1_1(pygame.sprite.Sprite):
    def __init__(self):
        super(Start_Platform1_1, self).__init__()
        self.image = platform_images[3]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = 1450
        self.rect.centery = 500

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Start_Platform2_1(pygame.sprite.Sprite):
    def __init__(self):
        super(Start_Platform2_1, self).__init__()
        self.image = platform_images[4]
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = 1100
        self.rect.centery = 400

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

class Fire(pygame.sprite.Sprite):
    def __init__(self):
        super(Fire, self).__init__()
        self.images = []
        self.images.append(pictures_fire[0])
        self.images.append(pictures_fire[1])
        self.images.append(pictures_fire[2])
        self.index1 = 0
        self.image = self.images[self.index1]
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH // 2
        self.rect.centery = HEIGHT - 65

        
    def update(self):
        self.index1 += 1
        if self.index1 >= 75:
            self.index1 = 0
        
        self.image = self.images[self.index1 // 25]
        self.image.set_colorkey(WHITE1)
        
clouds = pygame.sprite.Group()
clouds1 = pygame.sprite.Group()
enemies = pygame.sprite.Group()
platforms1 = pygame.sprite.Group()
platforms2 = pygame.sprite.Group()
platforms3 = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
platform1 = Platform1()
platform2 = Platform2()
platform3 = Platform3()
start_platform1 = Start_Platform1()
start_platform2 = Start_Platform2()
start_platform3 = Start_Platform3()
start_platform1_1 = Start_Platform1_1()
start_platform2_1 = Start_Platform2_1()
player = Player()
cloud = Cloud()
cloud1 = Cloud1()
enemy = Enemy()
fire = Fire()
all_sprites.add(player, start_platform1, start_platform2, start_platform3, start_platform1_1, start_platform2_1, fire)

running = True
while running: 
    clock.tick(FPS)
    for event in pygame.event.get(): 
        if event.type == KEYDOWN:    
            if event.key == K_ESCAPE:
                running = False
            elif event.key == K_SPACE:
                jump = True    
        elif event.type == ADDCLOUD:
            new_cloud = Cloud()
            clouds.add(new_cloud)
            all_sprites.add(new_cloud)
        elif event.type == ADDCLOUDBIG:
            new_cloud1 = Cloud1()
            clouds1.add(new_cloud1)
            all_sprites.add(new_cloud1)
        elif event.type == ADDENEMY:
            new_enemy = Enemy()
            enemies.add(new_enemy)
            all_sprites.add(new_enemy)
        elif event.type == ADDPLATFORM_1:
            new_platform1 = Platform1()
            platforms1.add(new_platform1)
            all_sprites.add(new_platform1)
        elif event.type == ADDPLATFORM_2:
            new_platform2 = Platform2()
            platforms2.add(new_platform2)
            all_sprites.add(new_platform2)
        elif event.type == ADDPLATFORM_3:
            new_platform3 = Platform3()
            platforms3.add(new_platform3)
            all_sprites.add(new_platform3)
        elif event.type == pygame.QUIT:
            running = False

    if jump:
        make()

                                       
    all_sprites.update()
    screen.fill(BLUE)
    screen.blit(day_jpg, day_rect)
    all_sprites.draw(screen)
    pygame.display.flip()
    cloud.update()
    cloud1.update()
    enemy.update()
    


pygame.quit()
Vildan
  • 131
  • 6
  • 1
    I imagine this is getting lost in translation, but what does "which sprite doesn't overcome right" mean? I understand "Sprite" but not in the sense that the sprite "overcomes" and not sure what "Right" is meant here. Can you share more about what are you attempting? – JNevill Dec 06 '21 at 18:26
  • JNevill, sorry for my bad English. I am learning him at school. I want to my sprite can't cross the platforms. He must stand on them. – Vildan Dec 06 '21 at 18:32

0 Answers0