我如何在这个游戏中添加在静态平台之上产生的硬币?

kx5bkwkv  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(201)

我最近开始学习python和pygame,我决定制作一个用于练习的游戏。我从一个类似的游戏中复制了大部分代码,但是现在我想修改它,我无法掌握它的窍门。我正在尝试在我的游戏中添加一枚硬币或一个球体,它只在第一种平台上产生,并且有“罕见”的产生几率。一旦收集,它将增加分数。欢迎任何帮助!

class NoobieJumper:                     #https://github.com/f-prime/DoodleJump
    def __init__(self):
        self.screen = pygame.display.set_mode((800, 600))
        self.green = pygame.image.load("images/cloudA.png").convert_alpha()
        self.score = 0
        self.font = pygame.font.SysFont("Arial", 25)
        self.blue = pygame.image.load("images/cloudB.png").convert_alpha()
        self.red = pygame.image.load("images/cloudB.png").convert_alpha()
        self.playerRight = pygame.image.load("images/right.png").convert_alpha()
        self.playerRight_1 = pygame.image.load("images/right_1.png").convert_alpha()
        self.playerLeft = pygame.image.load("images/left.png").convert_alpha()
        self.playerLeft_1 = pygame.image.load("images/left_1.png").convert_alpha()
        self.orbSprite = pygame.image.load("images/orb.png").convert_alpha()
        self.direction = 0
        self.playerx = 400
        self.playery = 400
        self.platforms = [[400, 500, 0, 0]]
        self.scor = [pygame.Rect(100, 250, 50, 50)]
        self.cameray = 0
        self.jump = 0
        self.gravity = 0
        self.xmovement = 0

    def updatePlayer(self):
        if not self.jump:        
            self.playery += self.gravity
            self.gravity += 1
        elif self.jump:
            self.playery -= self.jump
            self.jump -= 1
        key = pygame.key.get_pressed()
        if key[K_RIGHT]:
            if self.xmovement < 10:
                self.xmovement += 1
            self.direction = 0

        elif key[K_LEFT]:
            if self.xmovement > -10:
                self.xmovement -= 1
            self.direction = 1
        else:
            if self.xmovement > 0:
                self.xmovement -= 0.5
            elif self.xmovement < 0:
                self.xmovement += 0.5
        if self.playerx > 840:
            self.playerx = -40
        elif self.playerx < -40:
            self.playerx = 840
        self.playerx += self.xmovement
        if self.playery - self.cameray <= 200:
            self.cameray -= 10
        if not self.direction:
            if self.jump:
                self.screen.blit(self.playerRight_1, (self.playerx, self.playery - self.cameray))
            else:
                self.screen.blit(self.playerRight, (self.playerx, self.playery - self.cameray))
        else:
            if self.jump:
                self.screen.blit(self.playerLeft_1, (self.playerx, self.playery - self.cameray))
            else:
                self.screen.blit(self.playerLeft, (self.playerx, self.playery - self.cameray))

    def updatePlatforms(self):
        for p in self.platforms:
            rect = pygame.Rect(p[0], p[1], self.green.get_width() - 10, self.green.get_height())
            player = pygame.Rect(self.playerx, self.playery, self.playerRight.get_width() - 10, self.playerRight.get_height())
            if rect.colliderect(player) and self.gravity and self.playery < (p[1] - self.cameray):
                if p[2] != 2:
                    self.jump = 15
                    self.gravity = 0
                else:
                    p[-1] = 1
            if p[2] == 1:
                if p[-1] == 1:
                    p[0] += 5
                    if p[0] > 550:
                        p[-1] = 0
                else:
                    p[0] -= 5
                    if p[0] <= 0:
                        p[-1] = 1

    def drawPlatforms(self):
        for p in self.platforms:
            check = self.platforms[1][1] - self.cameray
            if check > 600:
                platform = random.randint(0, 1000)
                if platform < 800:
                    platform = 0
                elif platform < 900:
                    platform = 1
                else:
                    platform = 2

                self.platforms.append([random.randint(0, 700), self.platforms[-1][1] - 50, platform, 0])
                coords = self.platforms[-1]
                #check = random.randint(0, 1000)
                self.platforms.pop(0)
                self.score += 100
            if p[2] == 0:
                self.screen.blit(self.green, (p[0], p[1] - self.cameray))
            elif p[2] == 1:
                self.screen.blit(self.blue, (p[0], p[1] - self.cameray))
            elif p[2] == 2:
                self.screen.blit(self.red, (p[0], p[1] - self.cameray))

    def scorOrbs(self):
        for o in self.scor:
            screen.blit(orbSprite, (o[0], o[1]))
            '''orb = random.randint(0, 6)
            if orb == 0:
                scor = 1
            else:
                scor = 0
            self.scor.append'''

    def generatePlatforms(self):
        on = 600
        while on > -100:
            x = random.randint(0,700)
            platform = random.randint(0, 1000)
            if platform < 770:
                platform = 0
            elif platform < 900:
                platform = 1
            else:
                platform = 2
            self.platforms.append([x, on, platform, 0])
            on -= 50

    def drawBack(self):
        screen.blit(background, [0, 0])

    def restart(self):
        self.cameray = 0
        self.score = 0
        self.platforms = [[400, 500, 0, 0]]
        self.generatePlatforms()
        self.playerx = 400
        self.playery = 400
        pygame.display.flip()

    def run(self):
        running = True
        lost = False
        clock = pygame.time.Clock()
        self.generatePlatforms()
        while running:
            self.screen.fill(BLACK)
            clock.tick(60)
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_r:
                        lost = False
                        self.restart()
                    if event.key == K_ESCAPE:
                        lost = False
                        self.restart()
                        running = False

            if self.playery - self.cameray > 600:
                lost = True

            if lost == False:
                self.drawBack()
                self.drawPlatforms()
                self.updatePlayer()
                self.updatePlatforms()
                self.screen.blit(self.font.render(str(self.score), -1, (0, 0, 0)), (25, 22))
                pygame.display.flip()
            else:
                screen.blit(loser, [0, 0])
                points = font_L.render (str(self.score), True, BLACK)
                screen.blit(points, [540, 445])
                pygame.display.flip()

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题