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.