예제 #1
0
파일: game.py 프로젝트: kabi175/snake-game
class Game:
    def __init__(self, w=1000, h=500):
        pygame.init()
        self.s_width = w
        self.s_height = h
        self.crashed = False
        self.speed = 10
        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode([self.s_width, self.s_height])
        self.snake = Snake(self.screen)
        self.apple = Apple(self.screen)
        self.score = 0

    def run(self):
        pygame.display.set_caption('Snake')
        self.screen.fill((255, 255, 255))
        pygame.display.flip()
        while not self.crashed:
            self.handle_event(self.snake)
            self.screen.fill((255, 255, 255))
            self.snake.move()
            if self.apple.show(self.snake.snake[0]):
                self.score += 1
                self.snake.grow()

            pygame.display.flip()

            if self.snake.is_dashed():
                self.game_over()
            self.clock.tick(self.speed)

        pygame.quit()

    def handle_event(self, snake):
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    self.crashed = True
                elif event.key == K_RIGHT:
                    snake.turn_right()
                elif event.key == K_LEFT:
                    snake.turn_left()
            elif event.type == QUIT:
                self.crashed = True

    def welcome(self):
        pass

    def game_over(self):
        self.screen.fill((0, 0, 0))
        pygame.display.flip()
        time.sleep(1)
        print("score", self.score)
        print("game over")
        self.crashed = True
예제 #2
0
        y = 0
        while y <= height:
            pg.draw.line(win, (255, 255, 255), (0, y), (width, y))
            y = y + scale

        if (test.hitApple(apple.pos[0], apple.pos[1])):
            apple.setPos()
            applePosition = apple.getPos()

        if (test.isDead()):
            test.setPos(0, 0)
            dead = True
            break

        test.show()
        apple.show()

        pg.display.update()
        clock.tick(150)
pg.quit()
'''keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
    if not(test.direction == [1,0]):
        test.direction = [-1,0]
if keys[pg.K_RIGHT]:
    if not(test.direction == [-1,0]):
        test.direction = [1,0]

if keys[pg.K_UP]:
    if not(test.direction == [0,1]):
        test.direction = [0,-1]
예제 #3
0
def gameLoop():
    gameExit = False
    gameOver = False

    points = 0
    speed = FPS

    lead_x = display_width / 2
    lead_y = display_height / 2
    lead_x_change = block_size
    lead_y_change = 0

    snake = Snake(lead_x, lead_y)
    stones = Stones()
    apple = Apple(stones, snake)
    diamond = Diamond()
    trimer = Diamond()

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

            if event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_LEFT) and snake.direction != "right":
                    snake.direction = "left"
                elif (event.key == pygame.K_RIGHT) and snake.direction != "left":
                    snake.direction = "right"
                elif (event.key == pygame.K_UP) and snake.direction != "down":
                    snake.direction = "up"
                elif (event.key == pygame.K_DOWN) and snake.direction != "up":
                    snake.direction = "down"

                if event.key == pygame.K_p:
                    pause()

        if snake.direction == "left":
            lead_x_change = -block_size
            lead_y_change = 0
        elif snake.direction == "right":
            lead_x_change = block_size
            lead_y_change = 0
        elif snake.direction == "up":
            lead_y_change = -block_size
            lead_x_change = 0
        elif snake.direction == "down":
            lead_y_change = block_size
            lead_x_change = 0

        lead_x += lead_x_change
        lead_y += lead_y_change

        if lead_x == apple.x and lead_y == apple.y:
            apple = Apple(stones, snake)
            snake.length += 1
            points += 10
            POINT.play()

            if (points) % 40 == 0:
                stones.add(snake)

            if (points) % 70 == 0:
                speed += 1
                print(speed)

            if (points) % 150 == 0:
                diamond.renew(stones, snake, speed)

            if (points) % 280 == 0:
                trimer.renew(Stones, snake, speed)

        if lead_x == diamond.x and lead_y == diamond.y:
            points += 50
            diamond.kill()
            snake.superSnake(speed)
            EVOLUTION.play()

            if points % 280 == 0:
                trimer.renew(stones, snake, speed)

        if lead_x == trimer.x and lead_y == trimer.y:
            points += 50
            trimer.kill()
            snake.trim()

            if points % 150 == 0:
                diamond.renew(stones, snake, speed)

        if snake.superTimer > 0:
            if 15 <= snake.superTimer:
                pygame.mixer.music.set_volume(0.05)
            if 15 > snake.superTimer >= 10:
                pygame.mixer.music.set_volume(0.10)
            elif 10 > snake.superTimer >= 5:
                pygame.mixer.music.set_volume(0.15)
            elif 5 > snake.superTimer:
                pygame.mixer.music.set_volume(0.2)
            for stone in Stones.list:
                if stone[0] == snake.head[1] and stone[1] == snake.head[2]:
                    points += 20
                    Stones.destroy(stone)
                    STONEDESTROY.play()

            if lead_x >= display_width - block_size:
                lead_x = block_size
            elif lead_x < block_size:
                lead_x = display_width - 2 * block_size
            elif lead_y >= display_height - block_size:
                lead_y = block_size
            elif lead_y < block_size:
                lead_y = display_height - 2 * block_size

        snake.update(lead_x, lead_y)
        gameOver = snake.isDead(stones)

        gameDisplay.blit(background, (0, 0))
        apple.show(gameDisplay)
        stones.show(gameDisplay)
        diamond.show(gameDisplay, 'black')
        trimer.show(gameDisplay, 'white')
        snake.show(gameDisplay, FPS)
        gameDisplay.blit(wall, (0, 0))
        score(gameDisplay, points)

        pygame.display.update()
        clock.tick(speed)

        while gameOver == True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameExit = True
                    gameOver = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()

            gameDisplay.blit(GAMEOVER, (0, 0))
            draw_text(gameDisplay, "SCORE: " + str(points), czarny, 50, display_width / 2, display_height / 2 - 25)

            button("MENU", 100, 450, 200, 70, czerwony, LIGHT_RED, action='menu')
            button("PLAY AGAIN", 350, 450, 200, 70, czerwony, LIGHT_RED, action='again')
            button("QUIT", 600, 450, 200, 70, czerwony, LIGHT_RED, action='quit')

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

    pygame.quit()
    pygame.font.quit()
    quit()