Как мне сделать и закрыть экран, когда мой класс игрока сталкивается с одним из моих классов платформы? - PullRequest
3 голосов
/ 02 августа 2020

Итак, я пытался сделать конечную заставку, но я знаю как, я просмотрел несколько руководств, но ни одно из них не имеет для меня смысла. Я также пробовал использовать свой начальный экран, но немного переключил его и поместил в другое место, но это не сработало. Я пытаюсь сделать так, чтобы когда мой игрок сталкивается с одной из моих платформ, появляется конечный экран, но я не знаю, как сделать конечный экран.

I do not know what to really put here but this is my start screen code 
##############################################
#START MENUE

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    #print(click)
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(window, ac,(x,y,w,h))

        if click[0] == 1 and action != None:
            action()         
    else:
        pygame.draw.rect(window, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    window.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    
def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
                
        window.fill((255,255,255))
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Jump", largeText)
        TextRect.center = ((500/2),(500/2))
        window.blit(TextSurf, TextRect)

        button("GO!",100,350,100,50,green,darkgreen,main_loop)
        button("Quit",300,350,100,50,orange,darkred,quitgame)

        pygame.display.update()
        clock.tick(15)
        
############################################


my Full code




import pygame
import time
import random
pygame.init()

#this is screem height
window = pygame.display.set_mode((500,500))

#know we put screem name
pygame.display.set_caption("Noobs first Game")





bg = pygame.image.load("New.png")

gg = pygame.image.load("lol.png")

Rule2 = pygame.image.load("one1.png")

Rule1 = pygame.image.load("two2.png")

Rule3 = pygame.image.load("three3.png")

Rule4 = pygame.image.load("four4.png")

Rule5 = pygame.image.load("five5.png")

Rule6 = pygame.image.load("six6.png")

#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 = 5
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

class platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
        self.ss1 = pygame.image.load("Rock.png")
        self.ss1 = pygame.transform.scale(self.ss1,(self.ss1.get_width()//2,self.ss1.get_height()//2))
        self.hitbox = (self.x + 20 , self.y,58,60)
    def draw(self):
        self.rect.topleft=(self.x,self.y)

        player_rect = self.ss1.get_rect(center = self.rect.center)
        player_rect.centerx += 70
        player_rect.centery += 88
        window.blit(self.ss1,player_rect)
        self.hitbox = (self.x + 20 , self.y,58,60)

class item:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
        self.ss1 = pygame.image.load("H6.png")
        self.ss1 = pygame.transform.scale(self.ss1,(self.ss1.get_width()//2,self.ss1.get_height()//2))
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.ss1,self.rect)
        
        
class wall:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


#draw player
white = (255,255,255)
player1 = player(255,400,30,30,white)

darkred = (200,0,0)

darkgreen = (0,200,0)

black = (0,0,0)
item1 = item(200,-500,50,50,white)
item2 = item(250,250,50,50,white)
item3 = item(330,-1030,50,50,white)
item4 = item(400,-2290,50,50,white)
item5 = item(300,-1600,50,50,white)
item6 = item(200,130,50,50,white)
item7 = item(230,-1830,50,50,white)
item8 = item(300,0,50,50,white)
item9 = item(330,-1440,50,50,white)
item10 = item(20,-160,50,50,white)

items = [item1,item2,item3,item4,item5,item6,item7,item8,item9,item10]

green = (0,255,0)
orange = (255,0,0)
platform1 = platform(100,300,10,60,orange)
platform2 = platform(5,200,10,60,orange)
platform3 = platform(5,400,10,60,orange)
platform4 = platform(100,100,10,60,orange)
platform5 = platform(5,-50,10,60,orange)
platform6 = platform(100,-200,10,60,orange)
platform7 = platform(5,400,60,600,orange)
platform8 = platform(300,-2430,10,60,orange)
platform9 = platform (350,-340,10,60,orange)
platform10 = platform (100,-470,10,60,orange)

platform13 = platform(330,-600,10,60,orange)
platform14 = platform(100,-790,10,60,orange)
platform15 = platform(330,-990,10,60,orange)
platform16 = platform(70,-1130,10,60,orange)
platform17 = platform(200,-1340,10,60,orange)
platform18 = platform(400,-1500,10,60,orange)
platform19 = platform(300,-1700,10,60,orange)
platform20 = platform(100,-1950,10,60,orange)
platform21 = platform (350,-2140,10,60,orange)
platform22 = platform (100,-2270,10,60,orange)
platform23 = platform(0,-2500,60,500,green)


#walls
platform11 = wall (485,-9600,10000,10,orange)
platform12 = wall (0,-9600,10000,10,orange)
wall1 = wall (0,400,60,500,orange)
wall2 = wall (0,-2500,60,500,green)
wall3 = wall (485,-9600,10000,10,orange)
wall4 = wall (0,-9600,10000,10,orange)


walls = [wall1,wall2]



platforms = (platform1,platform2,platform3,platform4,platform5,platform6,platform7,platform8,platform9,platform10,platform13,platform14,platform15,platform16,platform17,platform18,platform19,platform20,platform21,platform22,platform23)


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








##############################################
#START MENUE

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    #print(click)
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(window, ac,(x,y,w,h))

        if click[0] == 1 and action != None:
            action()         
    else:
        pygame.draw.rect(window, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    window.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    
def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
                
        window.fill((255,255,255))
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Jump", largeText)
        TextRect.center = ((500/2),(500/2))
        window.blit(TextSurf, TextRect)

        button("GO!",100,350,100,50,green,darkgreen,main_loop)
        button("Quit",300,350,100,50,orange,darkred,quitgame)

        pygame.display.update()
        clock.tick(15)
        
############################################



def main_loop():
    
    #window
    def redrawwindow():
        window.fill((0,0,0))

        window.blit(bg,(0,0))
    

        #draw plyer
        player1.draw()
        platform11.draw()
        platform12.draw()
        wall3.draw()
        wall4.draw()
        for platform in platforms:
            platform.draw()
        for wall in walls:
            wall.draw()
    



        # the score draw it on the screen
        window.blit(text,textRect)

        for item in items:
            item.draw()

            
    font  = pygame.font.Font("freesansbold.ttf",30)
    score = 0
    text = font.render("Coins = "+str(score),True,(255,255,255))
    textRect = text.get_rect()
    textRect.center = ((100,40))

                    
    run = True
    while run:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
              

            #walls
        if player1.rect.colliderect(platform11) and player1.rect.colliderect(platform12):
            player1.x = 40

            # this makes you scroll up
        if player1.y < 250:
            player1.y += 1
            #platforms
            for platform in platforms:
                platform.y += player1.speed

            for item in items:
                item.y += player1.speed

            for wall in walls:
                wall.y += player1.speed
                #walls
            platform11.y += player1.speed
            platform12.y += player1.speed

            # this makes you scroll down
        if player1.y > 450:
            player1.y -= player1.fall
            for platform in platforms:
                platform.y -= player1.fall
            platform11.y -= player1.fall
            platform12.y -= player1.fall


            for item in items:
                item.y -= player1.fall

            for wall in walls:
                wall.y -= player1.fall
        




         # coin collisions
        for item in items:
            for one in range(len(items)-1,-1,-1):
                if player1.rect.colliderect(items[one].rect):
                    del items[one]
                    score += 1
                    text = font.render("Coins = "+str(score),True,(255,255,255))
                    textRect.center = ((100,40))
        



        keys = pygame.key.get_pressed()

        if keys[pygame.K_a]and player1.x > player1.speed:
            player1.x -= player1.speed

        if keys[pygame.K_d]and player1.x <500 - player1.height - player1.speed:
            player1.x += player1.speed

        if keys[pygame.K_w]and player1.y > player1.speed:
            player1.y -= player1.speed

        if keys[pygame.K_s]and player1.y <500 - player1.width - player1.speed:
            player1.y += player1.speed

        if not player1.isJump:
            player1.y += player1.fall
            player1.fall += 1
            player1.isJump = False

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

   
                

                if player1.rect.bottom >= 500:
                    collide = True
                    player1.isJump = False
                    player1.JumpCount = 10
                    player1.y = 500 - player1.height

            if collide:
                if keys[pygame.K_SPACE]:
                    player1.isJump = True
                player1.fall = 0


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

        if player1.rect.colliderect(platform6):
            window.blit(Rule3,(-150,-80))
            
        if player1.rect.colliderect(platform2.rect):
            window.blit(Rule1,(-30,0))
            
        if player1.rect.colliderect(platform23.rect):
            window.blit(gg,(100,100))
            
        if player1.rect.colliderect(platform7.rect):
            window.blit(Rule2,(-70,0))

        if player1.rect.colliderect(platform1.rect):
            window.blit(Rule4,(-40,100))

        if player1.rect.colliderect(platform14.rect):
            window.blit(Rule5,(-50,0))

        if player1.rect.colliderect(platform17.rect):
            window.blit(Rule6,(-50,0))

    
        pygame.display.update()
    pygame.quit()
game_intro()
main_loop()

1 Ответ

0 голосов
/ 02 августа 2020

Вот схема, которая может вам помочь. В том же духе, что и комментарий выше. Вам нужна логическая переменная, чтобы отслеживать, когда показывать конечный экран (произошло что-то плохое), а затем использовать ее, чтобы решить, какой экран рисовать в вашем основном l oop ...

def redrawwindow():
    # basically what you have now...

def draw_end_screen():
    # make this new function such that it shows your end-screen


run=True
end_condition=False   # a new boolean variable to keep track if collision (or something else happened)

while run:
    # basically what you have now....

    # check for collision (in pseudo-code) check for game-ending events
    if player1.collides_with_death...:
        end_condition=True
    if player1.falls_into_pit()...:
        end_condition=True

    # use the boolean variable to decide what to do next, and in next loop
    if end_condition:
        draw_end_screen()
    else:
        redrawwindow()

    pygame.display.update()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...