Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay contest

preface

Using Python to implement routine vindicates VS unroutine vindicates. Without further ado.

Let’s have a good time

The development tools

Python version: 3.6.4

Related modules:

Pygame module;

The random module

Sys module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Use Python’s PyGame library to generate a vindicate artifact

No set version

Code implementation

import pygame
import random
import sys

Set the screen size of the game according to the background image size
WIDTH, HEIGHT = 1024.576
# not full screen
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0.32)
# full screen
# screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN, 32)
pygame.display.set_caption('Little sister, your delivery has arrived. ')


# Add text message
def title(text, screen, scale, color=(0.0.0)) :
    font = pygame.font.SysFont('SimHei'.27)
    textRender = font.render(text, True, color)
    Initialize the text coordinates
    screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1]))


# button
def button(text, x, y, w, h, color, screen) :
        pygame.draw.rect(screen, color, (x, y, w, h))
        font = pygame.font.SysFont('SimHei'.20)
        textRender = font.render(text, True, (255.255.255))
        textRect = textRender.get_rect()
        textRect.center = ((x+w/2), (y+h/2))
        screen.blit(textRender, textRect)


# Generate random position coordinates
def get_random_pos() :
        x, y = random.randint(10.600), random.randint(20.500)
        return x, y


# the page displayed after clicking the Say Yes button
def show_like_interface(screen) :
    screen.fill((255.255.255))
    background1 = pygame.image.load('214_1.jpg').convert()
    screen.blit(background1, (0.0))
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()


def main() :
    pygame.init()
    clock = pygame.time.Clock()
    # Add background music
    pygame.mixer.music.load('214_1.mp3')
    pygame.mixer.music.play(-1.20)
    pygame.mixer.music.set_volume(0.5)
    # Set the disagree button property
    unlike_pos_x = 130
    unlike_pos_y = 375
    unlike_pos_width = 450
    unlike_pos_height = 55
    unlike_color = (115.76.243)
    # Set the agree button property
    like_pos_x = 130
    like_pos_y = 280
    like_pos_width = 450
    like_pos_height = 55
    like_color = (115.76.243)

    running = True
    while running:
        # Fill window
        screen.fill((255.255.255))
        # Add background image
        background = pygame.image.load('214_2.jpg').convert()
        screen.blit(background, (0.0))

        Get mouse coordinates
        pos = pygame.mouse.get_pos()
        # Judge mouse position, disagree, button changes constantly
        if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
            while True:
                unlike_pos_x, unlike_pos_y = get_random_pos()
                if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
                    continue
                break

        # Set the title and button text information
        title(What would you do if ONE day I confessed my love to you ', screen, scale=[8.3])
        button('A. You've finally come to the senses. I'll say yes if you dare! ', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen)
        button('B. I take you as my best friend and you try to sleep with me! Say no! ', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen)
        # Set the closing option property
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        # Jump to the end of the page after clicking the agree button
        if pos[0] < like_pos_x + like_pos_width + 5 and pos[0] > like_pos_x - 5 and pos[1] < like_pos_y + like_pos_height + 5 and pos[1] > like_pos_y - 5:
            if event.type == pygame.MOUSEBUTTONDOWN:
                show_like_interface(screen)

        pygame.display.flip()
        pygame.display.update()
        clock.tick(60)


main()
Copy the code

Routine version

import pygame
import random
import sys

Set the screen size of the game according to the background image size
WIDTH, HEIGHT = 1024.576
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0.32)
pygame.display.set_caption('Little sister, your delivery has arrived. ')


# Add text message
def title(text, screen, scale, color=(0.0.0)) :
    font = pygame.font.SysFont('SimHei'.27)
    textRender = font.render(text, True, color)
    Initialize the coordinates of the text
    screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1]))


# button
def button(text, x, y, w, h, color, screen, color_text) :
        pygame.draw.rect(screen, color, (x, y, w, h))
        font = pygame.font.SysFont('SimHei'.20)
        textRender = font.render(text, True, color_text)
        textRect = textRender.get_rect()
        textRect.center = ((x+w/2), (y+h/2))
        screen.blit(textRender, textRect)


# Generate random position coordinates
def get_random_pos() :
        x, y = random.randint(20.620), random.randint(20.460)
        return x, y


# Click on the page that will be displayed after agreeing
def show_like_interface(screen) :
    screen.fill((255.255.255))
    background1 = pygame.image.load('214_1.jpg').convert()
    screen.blit(background1, (0.0))
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()


# the page displayed after clicking the Do not agree button
def show_unlike_interface(screen) :
    screen.fill((255.255.255))
    background_1 = pygame.image.load('214_3.jpg').convert()
    screen.blit(background_1, (0.0))
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()


def main() :
    num = 0
    pygame.init()
    clock = pygame.time.Clock()
    # Add background music
    pygame.mixer.music.load('214_2.mp3')
    pygame.mixer.music.play(-1.40)
    pygame.mixer.music.set_volume(0.5)
    # Set the disagree button property
    unlike_pos_x = 130
    unlike_pos_y = 375
    unlike_pos_width = 450
    unlike_pos_height = 55
    unlike_color = (115.76.243)
    # Set the agree button property
    like_pos_x = 130
    like_pos_y = 280
    like_pos_width = 450
    like_pos_height = 55
    like_color = (115.76.243)

    running = True
    while running:
        # Fill window
        screen.fill((255.255.255))
        # Add background image
        background = pygame.image.load('214_2.jpg').convert()
        screen.blit(background, (0.0))

        Get mouse coordinates
        pos = pygame.mouse.get_pos()
        if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
            while True:
                if num > 5:
                    break
                num += 1
                unlike_pos_x, unlike_pos_y = get_random_pos()
                if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
                    continue
                break

        # Set the title and button text information
        title(What would you do if ONE day I confessed my love to you ', screen, scale=[8.3])
        button('A. You've finally come to the senses. I'll say yes if you dare! ', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen, (255.255.255))
        # Set up small set text
        if num < 6:
            button('B. I take you as my best friend and you try to sleep with me! Say no! ', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen, (255.255.255))
        if num > 5:
            button('B. I take you as my best friend and you try to sleep with me! Say yes! ', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen, (255.255.255))
        # Set the routine text
        if num == 1:
            button('Operating tips: please click the answer directly, do not shake hands! ', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255.255.255), screen, (192.0.0))
        if num == 2:
            button('Why are you shaking again? Is there something wrong with the goddess? ', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255.255.255), screen, (192.0.0))
        if num == 3:
            button('ah! Looks like this is a serious illness! ', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255.255.255), screen, (192.0.0))
        if num == 4:
            button('Sick and unattended, distressed... ', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255.255.255), screen, (192.0.0))
        if num == 5:
            button('had a narrow squeak! It was almost there! ', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255.255.255), screen, (192.0.0))
        if num == 6:
            button('Oh, forget it. You choose.', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255.255.255), screen, (192.0.0))

        # Click the routine button
        if num > 5:
            if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    show_unlike_interface(screen)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        Click the Agree button
        if pos[0] < like_pos_x + like_pos_width + 5 and pos[0] > like_pos_x - 5 and pos[1] < like_pos_y + like_pos_height + 5 and pos[1] > like_pos_y - 5:
            if event.type == pygame.MOUSEBUTTONDOWN:
                show_like_interface(screen)

        pygame.display.flip()
        pygame.display.update()
        clock.tick(60)


main()
Copy the code

Packaging application

Use the PyInstaller library to package code, images, and music materials into exe files