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
copyright notice
author[Dai mubai],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202012303448518.html
The sidebar is recommended
- How IOS developers learn Python Programming 22 - Supplement 1
- Python can meet any API you need
- Python 3 process control statement
- The 20th of 120 Python crawlers, 1637. All the way business opportunity network joined in data collection
- Datetime of pandas time series preamble
- How to send payslips in Python
- [Python] closure and scope
- Application of Python Matplotlib color
- leetcode 1627. Graph Connectivity With Threshold (python)
- Python thread 08 uses queues to transform the transfer scenario
guess what you like
-
Python: simple single player strange game (text)
-
Daily python, chapter 27, Django template
-
TCP / UDP communication based on Python socket
-
Use of pandas timestamp index
-
leetcode 148. Sort List(python)
-
Confucius old book network data collection, take one anti three learning crawler, python crawler 120 cases, the 21st case
-
[HTB] cap (datagram analysis, setuid capability: Python)
-
How IOS developers learn Python Programming 23 - Supplement 2
-
How to automatically identify n + 1 queries in Django applications (2)?
-
Data analysis starts from scratch. Pandas reads HTML pages + data processing and analysis
Random recommended
- 1313. Unzip the coding list (Java / C / C + + / Python / go / trust)
- Python Office - Python edit word
- Collect it quickly so that you can use the 30 Python tips for taking off
- Strange Python strip
- Python crawler actual combat, pyecharts module, python realizes China Metro data visualization
- DOM breakpoint of Python crawler reverse
- Django admin custom field stores links in the database after uploading files to the cloud
- Who has powder? Just climb who! If he has too much powder, climb him! Python multi-threaded collection of 260000 + fan data
- Python Matplotlib drawing streamline diagram
- The game comprehensively "invades" life: Python releases the "cool run +" plan!
- Python crawler notes: use proxy to prevent local IP from being blocked
- Python batch PPT to picture, PDF to picture, word to picture script
- Advanced face detection: use Dlib, opencv and python to detect face markers
- "Python 3 web crawler development practice (Second Edition)" is finally here!!!!
- Python and Bloom filters
- Python - singleton pattern of software design pattern
- Lazy listening network, audio novel category data collection, multi-threaded fast mining cases, 23 of 120 Python crawlers
- Troubleshooting ideas and summary of Django connecting redis cluster
- Python interface automation test framework (tools) -- interface test tool requests
- Implementation of Morse cipher translator using Python program
- [Python] numpy notes
- 24 useful Python tips
- Pandas table beauty skills
- Python tiktok character video, CV2 module, Python implementation
- I used Python to climb my wechat friends. They are like this
- 20000 words take you into the python crawler requests library, the most complete in history!!
- Answer 2: why can you delete the table but not update the data with the same Python code
- [pandas learning notes 02] - advanced usage of data processing
- How to implement association rule algorithm? Python code and powerbi visualization are explained to you in detail (Part 2 - actual combat)
- Python adds list element append() method, extend() method and insert() method [details]
- python wsgi
- Introduction to Python gunicorn
- Python dictionary query key value pair methods and examples
- Opencv Python reads video, processes and saves it frame by frame
- Python learning process and bug
- Imitate the up master and realize a live broadcast room controlled by barrage with Python!
- Essence! Configuration skills of 12 pandas
- [Python automated operation and maintenance road] path inventory
- Daily automatic health punch in (Python + Tencent cloud server)
- [Python] variables, comments, basic data types