current position:Home>Python combat case, pyGame module, python implementation routine confession artifact vs no routine confession artifact

Python combat case, pyGame module, python implementation routine confession artifact vs no routine confession artifact

2022-02-01 23:03:46 Dai mubai

Time is not negative , Create constantly , This article is participating in 2021 Year end summary essay contest

Preface

utilize Python Realize the routine to express the artifact VS No routine expression artifact . I don't say much nonsense .

Let's start happily ~

development tool

Python edition : 3.6.4

Related modules :

pygame modular ;

random modular

sys modular ;

As well as some Python Built in modules .

Environment building

install Python And add to environment variable ,pip Install the relevant modules required .

utilize Python Of pygame library , Generate a confession artifact

No routine version

Code implementation

import pygame
import random
import sys

#  According to the background image size , Set game screen 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(' Cute girl , Your express is here .')


#  Add text message 
def title(text, screen, scale, color=(0, 0, 0)):
    font = pygame.font.SysFont('SimHei', 27)
    textRender = font.render(text, True, color)
    #  Coordinates of initialization text 
    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 allow 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 disagree button properties 
    unlike_pos_x = 130
    unlike_pos_y = 375
    unlike_pos_width = 450
    unlike_pos_height = 55
    unlike_color = (115, 76, 243)
    #  Set Agree button properties 
    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 in the window 
        screen.fill((255, 255, 255))
        #  Add background 
        background = pygame.image.load('214_2.jpg').convert()
        screen.blit(background, (0, 0))

        #  Get mouse coordinates 
        pos = pygame.mouse.get_pos()
        #  Determine the mouse position , When you disagree , The buttons are constantly changing 
        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('1. If one day I confess to you , What will happen to you ?', screen, scale=[8, 3])
        button('A. You're finally enlightened , If you dare to confess, I dare to promise !', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen)
        button('B. I take you as my best friend , You want to sleep with me ! Refuse decisively !', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen)
        #  Set off option properties 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        #  When the mouse clicks the Agree button , Jump to the end page 
        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 code 

Routine version

import pygame
import random
import sys

#  According to the background image size , Set game screen size 
WIDTH, HEIGHT = 1024, 576
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption(' Cute girl , Your express is here .')


#  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


#  The page displayed after clicking yes 
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 disallow 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 disagree button properties 
    unlike_pos_x = 130
    unlike_pos_y = 375
    unlike_pos_width = 450
    unlike_pos_height = 55
    unlike_color = (115, 76, 243)
    #  Set Agree button properties 
    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 in the window 
        screen.fill((255, 255, 255))
        #  Add background 
        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('1. If one day I confess to you , What will happen to you ?', screen, scale=[8, 3])
        button('A. You're finally enlightened , If you dare to confess, I dare to promise !', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen, (255, 255, 255))
        #  Set small routine text 
        if num < 6:
            button('B. I take you as my best friend , You want to sleep with me ! Refuse decisively !', 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 , You want to sleep with me ! Resolutely promise !', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen, (255, 255, 255))
        #  Set routine text 
        if num == 1:
            button(' Operation hint : Please click the answer directly , Don't shake your 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 ! It seems that he is still very ill !', 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 no one to take care of , Love dearly ……', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
        if num == 5:
            button(' That was close ! It almost arrived !', unlike_pos_x, unlike_pos_y - 50, unlike_pos_width, unlike_pos_height, (255, 255, 255), screen, (192, 0, 0))
        if num == 6:
            button(' Ah , Forget it , No hiding , You can 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 code 

packaged applications

Use pyinstaller The library will code 、 Package pictures and music materials into exe file

1.jpg

2.jpg

copyright notice
author[Dai mubai],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202012303448518.html

Random recommended