Python 如何使用随机化器使我的平台彼此远离

Python 如何使用随机化器使我的平台彼此远离,python,pygame,Python,Pygame,所以我一直在尝试让我的玩家使用日志/平台越来越高,但我有个问题,我想让日志/平台离我的玩家有一段很长的距离,这样我就可以使用随机化器跳到日志/平台,然后再往上爬,但对我来说,日志/平台有时会彼此靠近,或者距离玩家很远 我的日志/平台变得随机 run = True while run: #[....] for Platform in platforms: if Platform.y > 510: Platform.x = random.randint

所以我一直在尝试让我的玩家使用日志/平台越来越高,但我有个问题,我想让日志/平台离我的玩家有一段很长的距离,这样我就可以使用随机化器跳到日志/平台,然后再往上爬,但对我来说,日志/平台有时会彼此靠近,或者距离玩家很远

我的日志/平台变得随机

run = True
while run:
#[....]

for Platform in platforms:
        if Platform.y > 510:
            Platform.x = random.randint(0, 400)
            Platform.y = random.randint(-160, -100)
我的完整代码

import pygame
import random
pygame.init()

window = pygame.display.set_mode((700,500))
pygame.display.set_caption(("Noobs First Game"))

# Player class
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 6
        self.fall = 0
        self.idle = [pygame.image.load("Player_idle1.png"),
                     pygame.image.load("Player_idle2.png"),
                     pygame.image.load("Player_idle3.png"),
                     pygame.image.load("Player_idle4.png"),
                     pygame.image.load("Player_idle5.png"),
                     pygame.image.load("Player_idle6.png"),
                     pygame.image.load("Player_idle7.png"),
                     pygame.image.load("Player_idle8.png"),
                     pygame.image.load("Player_idle9.png"),
                     pygame.image.load("Player_idle10.png")]
        
        self.idlel = [pygame.image.load("Player_lidle1.png"),
                     pygame.image.load("Player_lidle2.png"),
                     pygame.image.load("Player_lidle3.png"),
                     pygame.image.load("Player_lidle4.png"),
                     pygame.image.load("Player_lidle5.png"),
                     pygame.image.load("Player_lidle6.png"),
                     pygame.image.load("Player_lidle7.png"),
                     pygame.image.load("Player_lidle8.png"),
                     pygame.image.load("Player_lidle9.png"),
                     pygame.image.load("Player_lidle10.png")]

        self.run = [pygame.image.load("Player_run1.png"),
                    pygame.image.load("Player_run2.png"),
                    pygame.image.load("Player_run3.png"),
                    pygame.image.load("Player_run4.png"),
                    pygame.image.load("Player_run5.png"),
                    pygame.image.load("Player_run6.png"),
                    pygame.image.load("Player_run7.png"),
                    pygame.image.load("Player_run8.png")]
        
        self.jump = [pygame.image.load("Player_Jump.png")]

        self.lrun = [pygame.image.load("Player_lrun1.png"),
                    pygame.image.load("Player_lrun2.png"),
                    pygame.image.load("Player_lrun3.png"),
                    pygame.image.load("Player_lrun4.png"),
                    pygame.image.load("Player_lrun5.png"),
                    pygame.image.load("Player_lrun6.png"),
                    pygame.image.load("Player_lrun7.png"),
                    pygame.image.load("Player_lrun8.png")]

        self.ljump = [pygame.image.load("Player_lJump.png")]

        self.direction = "run"
        self.direction = "jump"
        self.direction = "lrun"
        self.direction = "ljump"
        self.direction = "idle"
        self.direction = "idlel"
        self.run = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.run]
        self.jump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.jump]
        self.lrun = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.lrun]
        self.ljump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.ljump]
        self.idle = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idle]
        self.idlel = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idlel]

        self.isJump = False
        self.JumpCount = 10
        self.rect = pygame.Rect(x,y,width,height)
        self.next_frame_time = 0
        self.fps = 10
        self.clock = pygame.time.Clock()
        self.anim_index = 0
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        if self.direction == "run":
            image_list = self.run
        if self.direction == "jump":
            image_list = self.jump
        if self.direction == "lrun":
            image_list = self.lrun
        if self.direction == "ljump":
            image_list = self.ljump
        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "idlel":
            image_list = self.idlel
            

        # Is it time to show next frame?
        time_now = pygame.time.get_ticks()
        if (time_now > self.next_frame_time):
            # seconds till next frame
            inter_time_delay = 1000 // self.fps
            self.next_frame_time = time_now + inter_time_delay
            # switch to next frame
            self.anim_index += 1
            if self.anim_index >= len(image_list):
                self.anim_index = 0

        if self.anim_index >= len(image_list):
            self.anim_index = 0
        player_image = image_list[self.anim_index]

        pygame.draw.rect( window, self.color, self.get_rect(), 2 )
        player_image = image_list[self.anim_index]

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 3
        player_rect.centery -= 17
        window.blit(player_image, player_rect)

# Platform class
class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.log = pygame.image.load("log.png")
        self.rect = pygame.Rect(x,y,width,height)
        self.log = pygame.transform.scale(self.log,(self.log.get_width()//4,self.log.get_height()//6))
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.log.get_rect(center = self.rect.center)
        platform_rect.centerx += 60
        platform_rect.centery -= 2
        window.blit(self.log,platform_rect)


# Platform2 class
class Platform2:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.log = pygame.image.load("Grass_1.png")
        self.rect = pygame.Rect(x,y,width,height)
        self.log = pygame.transform.scale(self.log,(self.log.get_width()-90,self.log.get_height()-91))
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.log.get_rect(center = self.rect.center)
        platform_rect.centerx += 2
        platform_rect.centery += 0.2
        window.blit(self.log,platform_rect)

class Dirt:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.dirt = pygame.image.load("Dirt_1.png")
        self.dirt = pygame.transform.scale(self.dirt,(self.dirt.get_width()-90,self.dirt.get_height()-91))
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        dirt_rect = self.dirt.get_rect(center = self.rect.center)
        dirt_rect.centerx += 2
        dirt_rect.centery += 0.2
        window.blit(self.dirt,dirt_rect)

# displaying Color
white = (255,255,255)

# Drawing stuff

# Drawing player
playerman = Player(255,440,40,40,white)
# Drawing Platform
platform1 = Platform(0,388,130,30,white)
# Drawing Platform2
Platform1 = Platform2(7000,470,1,1,white)
# Drawing Dirt
dirt1 = Dirt(7000,255,35,35.1,white)

# List

# Putting Platform in a list
platforms = [platform1]
# Putting Platform2 in a list
Platforms = [Platform1]
# Putting Dirt in a list
dirts = [dirt1]

platformGroup = pygame.sprite.Group
Level = [
"  l",
"l",
"",
"  l",
"  ",
"l",
"   ",
"  l",
"   ",
"",
"",
"",
"11111111111111111111",
"22222222222222222222",]
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "1":
            new_platform = Platform2(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
            Platforms.append(new_platform)
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "2":
            new_dirt = Dirt(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
            dirts.append(new_dirt)
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "l":
            new_platform = Platform(ix*110, iy*60, 130,30,(255, 255, 255))
            platforms.append(new_platform)
    


# redrawing window
def redrawwindow():
    window.fill((0,0,0))
    
    # bliting a counter the game
    window.blit(text,textRect)
    # showing player on the screen
    playerman.draw()

    # Drawing Platform
    for Platform in platforms:
        Platform.draw()
    # Drawing Platform2
    for Platform2 in Platforms:
        Platform2.draw()
    # Drawing Dirt
    for Dirt in dirts:
        Dirt.draw()
        

# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((150,40))


fps = 30
clock = pygame.time.Clock()

x = 10
y = 10

x_change = 0
y_change = 0

old_x = x
old_y = y

timer = 0
Stimer =  0
# Space down = False
spcdown = False
run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()


    for Platform in platforms:
        if Platform.y > 510:
            Platform.x = random.randint(0, 400)
            Platform.y = random.randint(-160, -100)


    # Mkaing screen go up
    if playerman.y < 250:
        playerman.y += playerman.speed
        for Platform in platforms:
            Platform.y += playerman.speed
        for Platform2 in Platforms:
            Platform2.y += playerman.speed
        for Dirt in dirts:
            Dirt.y += playerman.speed

            
    # Marking screen go down
    if playerman.y > 410:
        playerman.y -= playerman.fall
        for Platform in platforms:
            Platform.y -= playerman.fall
        for Platform2 in Platforms:
            Platform2.y -= playerman.fall
        for Dirt in dirts:
            Dirt.y -= playerman.fall
            



        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                x_change = -7
            if event.key == pygame.K_a:
                x_change = 7

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_a:
                x_change = 0

            x += x_change
            if x > 500 - playerman.width or x < 0:
                x = old_x

        
    if timer > 0:
        timer += 1
    if timer > 50:
        timer = 0


            
        

    # If keys get pressed
    keys = pygame.key.get_pressed()
    px,py = playerman.x,playerman.y




    # Adding one to score every time player jumps
    if not keys[pygame.K_SPACE]:
        spcdown = False  # space released
    
    if keys[pygame.K_SPACE]:
        if not spcdown:
            score += 1  # if space pressed first time
        spcdown = True  # space key is pressed
        text = font.render(" = "+str(score),True,(255,255,255))
        textRect.center = ((150,40))
        


    # Player movment
    if keys[pygame.K_a] and px > playerman.speed:
        px -= playerman.speed
        playerman.direction = "lrun"
    elif keys[pygame.K_d] and px < 700 - playerman.width - playerman.speed:
        px += playerman.speed
        playerman.direction = "run"
    else:
        if playerman.direction == "run":
            playerman.direction = "idle"
        else:
            if playerman.direction == "lrun":
                playerman.direction = "idlel"
    

    if keys[pygame.K_w] and py > playerman.speed:
        py -= playerman.speed

    if keys[pygame.K_s] and py < 500 - playerman.height - playerman.speed:
        py += playerman.speed

    # animation for player jump
    if playerman.direction == "run":
        if keys[pygame.K_SPACE]: 
            playerman.direction = "jump"
    else:
        if playerman.direction == "lrun":
            if keys[pygame.K_SPACE]:
                playerman.direction = "ljump"


    platform_rect_list =[p.rect for p in platforms]
    player_rect = playerman.get_rect()
    playerman.rect.topleft = (px,py)


    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px


    

        
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

                    

            # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right

        
                # Lets Player jump on top of second Platform
        for Platform2 in Platforms:
            if playerman.get_rect().colliderect(Platform2.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform2.rect.top - playerman.height
                if playerman.rect.right > Platform2.rect.left and playerman.rect.left < Platform2.rect.left - playerman.width:
                    playerman.x = Platform2.rect.left - playerman.width
                if playerman.rect.left < Platform2.rect.right and playerman.rect.right > Platform2.rect.right + playerman.width:
                    playerman.x = Platform2.rect.right

                           
                # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

            # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10

        
            
    redrawwindow()
    pygame.display.update()
quit_game

导入pygame
随机输入
pygame.init()
window=pygame.display.set_模式((700500))
pygame.display.set_标题((“Noobs第一场游戏”))
#球员级别
职业球员:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.color=颜色
自身速度=6
self.fall=0
self.idle=[pygame.image.load(“Player_idle1.png”),
pygame.image.load(“Player_idle2.png”),
pygame.image.load(“Player_idle3.png”),
pygame.image.load(“Player_idle4.png”),
pygame.image.load(“Player_idle5.png”),
pygame.image.load(“Player_idle6.png”),
pygame.image.load(“Player_idle7.png”),
pygame.image.load(“Player_idle8.png”),
pygame.image.load(“Player_idle9.png”),
pygame.image.load(“Player_idle10.png”)]
self.idel=[pygame.image.load(“Player_lidle1.png”),
pygame.image.load(“Player_lidle2.png”),
pygame.image.load(“Player_lidle3.png”),
pygame.image.load(“Player_lidle4.png”),
pygame.image.load(“Player_lidle5.png”),
pygame.image.load(“Player_lidle6.png”),
pygame.image.load(“Player_lidle7.png”),
pygame.image.load(“Player_lidle8.png”),
pygame.image.load(“Player_lidle9.png”),
pygame.image.load(“Player_lidle10.png”)]
self.run=[pygame.image.load(“Player_run1.png”),
pygame.image.load(“Player_run2.png”),
pygame.image.load(“Player_run3.png”),
pygame.image.load(“Player_run4.png”),
pygame.image.load(“Player_run5.png”),
pygame.image.load(“Player_run6.png”),
pygame.image.load(“Player_run7.png”),
pygame.image.load(“Player_run8.png”)]
self.jump=[pygame.image.load(“Player_jump.png”)]
self.lrun=[pygame.image.load(“Player_lrun1.png”),
pygame.image.load(“Player_lrun2.png”),
pygame.image.load(“Player_lrun3.png”),
pygame.image.load(“Player_lrun4.png”),
pygame.image.load(“Player_lrun5.png”),
pygame.image.load(“Player_lrun6.png”),
pygame.image.load(“Player_lrun7.png”),
pygame.image.load(“Player_lrun8.png”)]
self.ljump=[pygame.image.load(“Player_ljump.png”)]
self.direction=“运行”
self.direction=“跳跃”
self.direction=“lrun”
self.direction=“ljump”
self.direction=“空闲”
self.direction=“idrel”
self.run=[pygame.transform.scale(image,(image.get_-width()//6,image.get_-height()//6)],用于self.run中的图像]
self.jump=[pygame.transform.scale(对于self.jump中的图像,(image.get\u width()//6,image.get\u height()//6))]
self.lrun=[pygame.transform.scale(image,(image.get_-width()//6,image.get_-height()//6))用于self.lrun中的图像]
self.ljump=[pygame.transform.scale(image,(image.get\u width()//6,image.get\u height()//6)],用于self.ljump中的图像]
self.idle=[pygame.transform.scale(对于self.idle中的图像,(image.get\u width()//6,image.get\u height()//6))]
self.idlel=[pygame.transform.scale(image,(image.get_-width()//6,image.get_-height()//6))用于self.idlel中的图像]
self.isJump=False
self.JumpCount=10
self.rect=pygame.rect(x,y,宽度,高度)
self.next\u frame\u time=0
self.fps=10
self.clock=pygame.time.clock()
self.anim_索引=0
def获取正确(自我):
self.rect.topleft=(self.x,self.y)
返回self.rect
def牵引(自):
如果self.direction==“运行”:
image\u list=self.run
如果self.direction==“jump”:
image\u list=self.jump
如果self.direction==“lrun”:
image\u list=self.lrun
如果self.direction==“ljump”:
image\u list=self.ljump
如果self.direction==“空闲”:
image\u list=self.idle
如果self.direction==“idlel”:
image\u list=self.idlel
#是时候播放下一帧了吗?
time\u now=pygame.time.get\u ticks()
如果(现在时间>下一帧时间):
#离下一帧还有几秒钟
间隔时间延迟=1000//self.fps
self.next\u frame\u time=time\u now+inter\u time\u delay
#切换到下一帧
self.anim_索引+=1
如果self.anim\u index>=len(图像列表):
self.anim_索引=0
如果self.anim\u index>=len(图像列表):
self.anim_索引=0
玩家图片=图片列表[自我动画索引]
pygame.draw.rect(窗口,self.color,self.get_rect(),2)
玩家图片=图片列表[自我动画索引]
player\u rect=player\u image.get\u rect(中心=self.get\u rect().center)
player_rect.centerx+=3
玩家右中锋-=17
blit(player\u image,player\u rect)
#平台类
课程平台:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.color=颜色
self.log=pygame.image.load(“l
    for i, Platform in enumerate(platforms):
        if i:
            if not Platform.fixed:
                x = platforms[i-1].x + random.randint(-150, 150) # Set the x to be the previous log's x minus/added with a random number that will guarantee the player will be able to access 
                if x < 0:
                    x = 0
                elif x > 500:
                    x = 500
                Platform.x = x
                Platform.y = platforms[i-1].y - random.randint(100, 140) # Set the y to be the previous log's y minus/added with a random number that will guarantee the player will be able to access 
                Platform.fixed = True
import pygame
import random
pygame.init()

window = pygame.display.set_mode((700,500))
pygame.display.set_caption(("Noobs First Game"))

# Player class
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 6
        self.fall = 0
        self.idle = [pygame.image.load("Player_idle1.png"),
                     pygame.image.load("Player_idle2.png"),
                     pygame.image.load("Player_idle3.png"),
                     pygame.image.load("Player_idle4.png"),
                     pygame.image.load("Player_idle5.png"),
                     pygame.image.load("Player_idle6.png"),
                     pygame.image.load("Player_idle7.png"),
                     pygame.image.load("Player_idle8.png"),
                     pygame.image.load("Player_idle9.png"),
                     pygame.image.load("Player_idle10.png")]
        
        self.idlel = [pygame.image.load("Player_lidle1.png"),
                     pygame.image.load("Player_lidle2.png"),
                     pygame.image.load("Player_lidle3.png"),
                     pygame.image.load("Player_lidle4.png"),
                     pygame.image.load("Player_lidle5.png"),
                     pygame.image.load("Player_lidle6.png"),
                     pygame.image.load("Player_lidle7.png"),
                     pygame.image.load("Player_lidle8.png"),
                     pygame.image.load("Player_lidle9.png"),
                     pygame.image.load("Player_lidle10.png")]

        self.run = [pygame.image.load("Player_run1.png"),
                    pygame.image.load("Player_run2.png"),
                    pygame.image.load("Player_run3.png"),
                    pygame.image.load("Player_run4.png"),
                    pygame.image.load("Player_run5.png"),
                    pygame.image.load("Player_run6.png"),
                    pygame.image.load("Player_run7.png"),
                    pygame.image.load("Player_run8.png")]
        
        self.jump = [pygame.image.load("Player_Jump.png")]

        self.lrun = [pygame.image.load("Player_lrun1.png"),
                    pygame.image.load("Player_lrun2.png"),
                    pygame.image.load("Player_lrun3.png"),
                    pygame.image.load("Player_lrun4.png"),
                    pygame.image.load("Player_lrun5.png"),
                    pygame.image.load("Player_lrun6.png"),
                    pygame.image.load("Player_lrun7.png"),
                    pygame.image.load("Player_lrun8.png")]

        self.ljump = [pygame.image.load("Player_lJump.png")]

        self.direction = "run"
        self.direction = "jump"
        self.direction = "lrun"
        self.direction = "ljump"
        self.direction = "idle"
        self.direction = "idlel"
        self.run = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.run]
        self.jump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.jump]
        self.lrun = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.lrun]
        self.ljump = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.ljump]
        self.idle = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idle]
        self.idlel = [pygame.transform.scale(image,(image.get_width()//6,image.get_height()//6))for image in self.idlel]

        self.isJump = False
        self.JumpCount = 10
        self.rect = pygame.Rect(x,y,width,height)
        self.next_frame_time = 0
        self.fps = 10
        self.clock = pygame.time.Clock()
        self.anim_index = 0
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        if self.direction == "run":
            image_list = self.run
        if self.direction == "jump":
            image_list = self.jump
        if self.direction == "lrun":
            image_list = self.lrun
        if self.direction == "ljump":
            image_list = self.ljump
        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "idlel":
            image_list = self.idlel
            

        # Is it time to show next frame?
        time_now = pygame.time.get_ticks()
        if (time_now > self.next_frame_time):
            # seconds till next frame
            inter_time_delay = 1000 // self.fps
            self.next_frame_time = time_now + inter_time_delay
            # switch to next frame
            self.anim_index += 1
            if self.anim_index >= len(image_list):
                self.anim_index = 0

        if self.anim_index >= len(image_list):
            self.anim_index = 0
        player_image = image_list[self.anim_index]

        pygame.draw.rect( window, self.color, self.get_rect(), 2 )
        player_image = image_list[self.anim_index]

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 3
        player_rect.centery -= 17
        window.blit(player_image, player_rect)

# Platform class
class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.fixed = False
        self.width = width
        self.height = height
        self.color = color
        self.log = pygame.image.load("log.png")
        self.rect = pygame.Rect(x,y,width,height)
        self.log = pygame.transform.scale(self.log,(self.log.get_width()//4,self.log.get_height()//6))
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.log.get_rect(center = self.rect.center)
        platform_rect.centerx += 60
        platform_rect.centery -= 2
        window.blit(self.log,platform_rect)


# Platform2 class
class Platform2:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.log = pygame.image.load("Grass_1.png")
        self.rect = pygame.Rect(x,y,width,height)
        self.log = pygame.transform.scale(self.log,(self.log.get_width()-20,self.log.get_height()-20))
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.log.get_rect(center = self.rect.center)
        platform_rect.centerx += 2
        platform_rect.centery += 0.2
        window.blit(self.log,platform_rect)

class Dirt:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.dirt = pygame.image.load("Dirt_1.png")
        self.dirt = pygame.transform.scale(self.dirt,(self.dirt.get_width()-20,self.dirt.get_height()-20))
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        dirt_rect = self.dirt.get_rect(center = self.rect.center)
        dirt_rect.centerx += 2
        dirt_rect.centery += 0.2
        window.blit(self.dirt,dirt_rect)

# displaying Color
white = (255,255,255)

# Drawing stuff

# Drawing player
playerman = Player(255,440,40,40,white)
# Drawing Platform
platform1 = Platform(0,388,130,30,white)
# Drawing Platform2
Platform1 = Platform2(7000,470,1,1,white)
# Drawing Dirt
dirt1 = Dirt(7000,255,35,35.1,white)

# List

# Putting Platform in a list
platforms = [platform1]
# Putting Platform2 in a list
Platforms = [Platform1]
# Putting Dirt in a list
dirts = [dirt1]

platformGroup = pygame.sprite.Group
Level = [
"  l",
"l",
"",
"  l",
"  ",
"l",
"   ",
"  l",
"   ",
"",
"",
"",
"11111111111111111111",
"22222222222222222222",]
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "1":
            new_platform = Platform2(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
            Platforms.append(new_platform)
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "2":
            new_dirt = Dirt(ix*35, iy*36.4, 35,35.1,(255, 255, 255))
            dirts.append(new_dirt)
for iy, row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "l":
            new_platform = Platform(ix*110, iy*60, 130,30,(255, 255, 255))
            platforms.append(new_platform)
    


# redrawing window
def redrawwindow():
    window.fill((0,0,0))
    
    # bliting a counter the game
    window.blit(text,textRect)
    # showing player on the screen
    playerman.draw()

    # Drawing Platform
    for Platform in platforms:
        Platform.draw()
    # Drawing Platform2
    for Platform2 in Platforms:
        Platform2.draw()
    # Drawing Dirt
    for Dirt in dirts:
        Dirt.draw()
        

# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((150,40))


fps = 30
clock = pygame.time.Clock()

x = 10
y = 10

x_change = 0
y_change = 0

old_x = x
old_y = y

timer = 0
Stimer =  0
# Space down = False
spcdown = False
run = True

while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
    for i, Platform in enumerate(platforms):
        if i:
            if not Platform.fixed:
                x = platforms[i-1].x + random.randint(-150, 150)
                if x < 0:
                    x = 0
                elif x > 500:
                    x = 500
                Platform.x = x
                Platform.y = platforms[i-1].y - random.randint(100, 140)
                Platform.fixed = True

    # Mkaing screen go up
    if playerman.y < 250:
        playerman.y += playerman.speed
        for Platform in platforms:
            Platform.y += playerman.speed
        for Platform2 in Platforms:
            Platform2.y += playerman.speed
        for Dirt in dirts:
            Dirt.y += playerman.speed

            
    # Marking screen go down
    if playerman.y > 410:
        playerman.y -= playerman.fall
        for Platform in platforms:
            Platform.y -= playerman.fall
        for Platform2 in Platforms:
            Platform2.y -= playerman.fall
        for Dirt in dirts:
            Dirt.y -= playerman.fall
            



        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                x_change = -7
            if event.key == pygame.K_a:
                x_change = 7

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_a:
                x_change = 0

            x += x_change
            if x > 500 - playerman.width or x < 0:
                x = old_x

        
    if timer > 0:
        timer += 1
    if timer > 50:
        timer = 0


            
        

    # If keys get pressed
    keys = pygame.key.get_pressed()
    px,py = playerman.x,playerman.y




    # Adding one to score every time player jumps
    if not keys[pygame.K_SPACE]:
        spcdown = False  # space released
    
    if keys[pygame.K_SPACE]:
        if not spcdown:
            score += 1  # if space pressed first time
        spcdown = True  # space key is pressed
        text = font.render(" = "+str(score),True,(255,255,255))
        textRect.center = ((150,40))
        


    # Player movment
    if keys[pygame.K_a] and px > playerman.speed:
        px -= playerman.speed
        playerman.direction = "lrun"
    elif keys[pygame.K_d] and px < 700 - playerman.width - playerman.speed:
        px += playerman.speed
        playerman.direction = "run"
    else:
        if playerman.direction == "run":
            playerman.direction = "idle"
        else:
            if playerman.direction == "lrun":
                playerman.direction = "idlel"
    

    if keys[pygame.K_w] and py > playerman.speed:
        py -= playerman.speed

    if keys[pygame.K_s] and py < 500 - playerman.height - playerman.speed:
        py += playerman.speed

    # animation for player jump
    if playerman.direction == "run":
        if keys[pygame.K_SPACE]: 
            playerman.direction = "jump"
    else:
        if playerman.direction == "lrun":
            if keys[pygame.K_SPACE]:
                playerman.direction = "ljump"


    platform_rect_list =[p.rect for p in platforms]
    player_rect = playerman.get_rect()
    playerman.rect.topleft = (px,py)


    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px


    

        
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

                    

            # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right

        
                # Lets Player jump on top of second Platform
        for Platform2 in Platforms:
            if playerman.get_rect().colliderect(Platform2.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform2.rect.top - playerman.height
                if playerman.rect.right > Platform2.rect.left and playerman.rect.left < Platform2.rect.left - playerman.width:
                    playerman.x = Platform2.rect.left - playerman.width
                if playerman.rect.left < Platform2.rect.right and playerman.rect.right > Platform2.rect.right + playerman.width:
                    playerman.x = Platform2.rect.right

                           
                # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

            # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10

        
            
    redrawwindow()
    pygame.display.update()
quit_game