0

I want to add infinite platforms to this game I created. Currently there are 4 different types of platforms, and I want it to infinitely go up and cycle through the different types of platforms. Meaning as you go up, some will be red, some green, etc.

Here is my code so far:

import pygame
from pygame.locals import *
pygame.init()

run = True
width = 500
height = 500
x = 250
y = 475
vel = 10
x_plat = 100
y_plat = 400
textX = 50
textY = 30
platform_vel = 5
clock = pygame.time.Clock()
isjump = False
jumpcount = 7.5
collision_tolerance = 10
gravity = 1
player_vy = 0
score_value = 0

surface = pygame.display.set_mode((width, height))
rect = Rect(x_plat, y_plat, 150, 20)
rect2 = Rect(250, 300, 150, 20)
rect3 = Rect(250, 200, 150, 20)
rect4 = Rect(250, 100, 150, 20)
player = Rect(x, y, 25, 25)

def show_score(x, y):
    font = pygame.font.Font('freesansbold.ttf', 32)
    score = font.render(str("Score: " + str(score_value)), True, (255, 255, 255))
    surface.blit(score, (x, y))

while run:

    clock.tick(30)

    if rect.left >= 355 or rect.left < 1:
        platform_vel *= -1
    if rect2.left >= 355 or rect2.left < 1:
        platform_vel *= -1
    if rect3.left >= 355 or rect3.left < 1:
        platform_vel *= -1
    if rect4.left >= 355 or rect4.left < 1:
        platform_vel *= -1

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and player.x > 0:
        player.x -= vel
    if keys[pygame.K_RIGHT] and player.x < (500 - 25):
        player.x += vel

    if not(isjump):
        if keys[pygame.K_SPACE]:
            isjump = True
            player_vy = -16
    player_vy += gravity
    player.y += player_vy

    collide = pygame.Rect.colliderect(rect, player)
    collide2 = pygame.Rect.colliderect(rect2, player)
    collide3 = pygame.Rect.colliderect(rect3, player)
    collide4 = pygame.Rect.colliderect(rect4, player)

    if collide and player_vy >= 0:
        player.bottom = rect.top
        score_value += 1
        player_vy = 0
        player.left += platform_vel
        isjump = False

    if collide2 and player_vy >= 0:
        player.bottom = rect2.top
        score_value += 1
        player_vy = 0
        player.left -= platform_vel
        isjump = False

    if collide3 and player_vy >= 0:
        player.bottom = rect3.top
        score_value += 1
        player_vy = 0
        player.left += platform_vel
        isjump = False

    if collide4 and player_vy >= 0:
        player.bottom = rect4.top
        score_value += 1
        player_vy = 0
        player.left += platform_vel
        isjump = False

    if player.y > 475:
        player.y = 475
        player_vy = 0
        isjump = False

    rect.left += platform_vel
    rect2.left -= platform_vel
    rect4.left -= platform_vel

    pygame.draw.rect(surface, (255, 255, 255), player)

    pygame.draw.rect(surface, (0, 0, 0), rect)

    pygame.draw.rect(surface, (0, 0, 255), rect2)

    pygame.draw.rect(surface, (0, 255, 0), rect3)

    pygame.draw.rect(surface, (255, 0, 0), rect4)

    show_score(textX, textY)

    pygame.display.update()

    surface.fill((255, 222, 173))

    for event in pygame.event.get():
        if event.type == QUIT:
            run = False

The code so far creates a player that can move left, right, and jump. The players main goal is to jump onto these different platforms that each have different functionalities.

Bob
  • 11
  • 2
  • 2
    instead of rect1, rect2, rect3, etc, put all of the platforms into a list. Then you can either a) use a counter and `nextplatform = rectList[counter % len(rectList)]` to always get them in the same order. b) or use `import random` `nextplatorm = random.choice(rectList)` to pick them randomly. – nigh_anxiety Jul 26 '22 at 20:55
  • there are other more advanced methods as well such as generators but they don't seem necessary for what you're describing as your intent – nigh_anxiety Jul 26 '22 at 20:56
  • You might not want infinite platforms - that'd use infinite resources and stuff. If you want to make it _seem_ infinite, you should make a finite amount of them and update the ones "not needed anymore" to be the "new" ones. (somehow the image of a train laying tracks in front of it and picking them up from the behind came into my mind. You might use the same trick) – Uncle Dino Jul 26 '22 at 21:18
  • Sasszem I understand what you are saying and it makes sense, but I just dont know how to code it so if you could help me in that sense it would be great – Bob Jul 27 '22 at 00:14
  • 1
    I would recoomend to create a [sprite class](https://www.pythoninformer.com/python-libraries/pygame/sprite-class/) for every platform. Then you can easily create a new platform when needed. – The_spider Jul 27 '22 at 08:53

0 Answers0