由于本人是个懒狗, 所以只有代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author : Jelly He
import math
import random
import time
from typing import Tuple, List
import pygame
from pygame.locals import QUIT, RESIZABLE
from pygame.cursors import broken_x, arrow
pic_path = r'D:/cvphotos/yeshou.png'
music_path = r'D:/cvphotos/114514.mp3'
pic_size = 25
class Game(object):
White = (255, 255, 255)
def __init__(self, pic, music):
pygame.init()
pygame.mixer.init()
pygame.font.init()
pygame.display.set_caption("一个恶臭的游戏")
self.win_width = 800
self.win_height = 600
self._win_width = 0
self._win_height = 0
self.center = (self.win_width / 2, self.win_height / 2)
self.Display_surf = pygame.display.set_mode(
(self.win_width, self.win_height), RESIZABLE)
self.sqr_width = pic_size
self._pic_x, self._pic_y = 0, 0
self.rand_x, self.rand_y = 0, 0
self.pic_path = pic
self.music_path = music
self.cute_img = pygame.image.load(pic)
self.cute_img = pygame.transform.scale(self.cute_img, (pic_size, pic_size))
self.my_font = pygame.font.SysFont("arial", 16)
self.score = 0
self.click_on_ = False
self.final = False
@staticmethod
def get_pos() -> Tuple[float, float]:
pos = pygame.mouse.get_pos()
return pos
def gen_rand(self) -> Tuple[float, float]:
center_x: int = random.randint(self.sqr_width, self.win_width - self.sqr_width)
center_y: int = random.randint(self.sqr_width, self.win_height - self.sqr_width)
self._pic_x, self._pic_y = center_x, center_y
return center_x, center_y
@property
def change_pos(self) -> Tuple[int, int]:
return self._pic_x, self._pic_y
@change_pos.setter
def change_pos(self, size: tuple):
current_w, current_h = size
self._pic_x = round(self.rand_x * current_w / self.win_width)
self._pic_y = round(self.rand_y * current_h / self.win_height)
self.center = (round(current_w/2), round(current_h/2))
self._win_width, self._win_height = current_w, current_h
def calc_volume(self, pic_x: int, pic_y: int) -> float:
corner: List[Tuple[int, int]] = [(0, 0), (0, self._win_height),
(self._win_width, 0), (self._win_width, self._win_height)]
pos_x, pos_y = self.get_pos()
distance_list = []
for x in corner:
x_, y_ = x[0], x[1]
dis = (x_ - self._pic_x)**2 + (y_ - self._pic_y) ** 2
distance_list.append(dis)
max_distance: float = max(distance_list)
distance: float = (pos_x - pic_x)**2 + (pos_y - pic_y)**2
volume: float = 1 - (math.sqrt(distance) / math.sqrt(max_distance))
return math.pow(2, volume) - 1
def if_click_on(self, pos_x: float, pos_y: float) -> bool:
distance = (pos_x - self._pic_x) ** 2 + (pos_y - self._pic_y) ** 2
return round(distance) <= self.sqr_width ** 2
def if_mouse_in(self, pos_x: float, pos_y: float):
center_x = self._pic_x + pic_size / 2
center_y = self._pic_y + pic_size / 2
distance: float = math.sqrt((center_x - pos_x) ** 2 + (center_y - pos_y) ** 2)
if distance < pic_size:
pygame.mouse.set_cursor(broken_x)
else:
pygame.mouse.set_cursor(arrow)
def calc_path_to_center(self) -> Tuple[List[Tuple[int, int]], int]:
y_diff = -self._pic_y + self.center[1]
x_diff = -self._pic_x + self.center[0]
step_list = []
length = int(min(abs(x_diff), abs(y_diff)))
for i in range(length + 1):
ratio: float = i / length
x_step, y_step = round(x_diff * ratio), round(y_diff*ratio)
step_list.append((self._pic_y + y_step, self._pic_x + x_step))
return step_list, length
def final_ani(self):
self.final = True
step_list, length = self.calc_path_to_center()
for index, size in enumerate(range(pic_size, length)):
pygame.display.update()
self.Display_surf.fill(Game.White)
self._pic_x, self._pic_y = step_list[index][1], step_list[index][0]
self.Display_surf.blit(self.cute_img, (step_list[index][1], step_list[index][0]))
pygame.display.update()
for i in range(pic_size, 500):
cat_img = pygame.transform.scale(self.cute_img, (i, i))
pygame.display.update()
self.Display_surf.fill(Game.White)
self.Display_surf.blit(cat_img, (self._pic_x - i / 2, self._pic_y - i/2))
time.sleep(5)
self.score += 1
self.rand_x, self.rand_y = self.gen_rand()
self.final = False
@staticmethod
def check_for_quit():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
def run(self):
pygame.mixer.music.load(self.music_path)
self.rand_x, self.rand_y = self.gen_rand()
pygame.mixer.music.play(start=2)
count = 0
while True:
text_surface = self.my_font.render("Your score:{}".format(self.score),
True, (0, 0, 0)
)
count += 1
if count == 400:
count = 0
pygame.mixer.music.play(start=1.2)
pos_x, pos_y = self.get_pos()
self.if_mouse_in(pos_x, pos_y)
mouse = pygame.mouse.get_pressed(3)
if mouse[0]:
if self.if_click_on(pos_x, pos_y) and not self.final:
self.click_on_ = True
self.final_ani()
volume_ratio = self.calc_volume(self._pic_x, self._pic_y)
pygame.mixer.music.set_volume(volume_ratio)
self.Display_surf.fill(Game.White)
area = self.Display_surf.get_rect()
self.Display_surf.blit(text_surface, (0, 0))
self.change_pos = (area.w, area.h)
pygame.display.update()
self.check_for_quit()
if __name__ == '__main__':
Game(pic_path, music_path).run()