How to use python to achieve a variety of changes to eliminate happiness that people can't quit?
This article is about how people use python to achieve a variety of changes that people can not quit. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
Introduction
Daily game update series-today we take you to unlock Xiaoxiaole Mini Game!
Late one night, I swam happily in the sea of the Internet as usual and was caught off guard by a phone call.
There is a shocking word on the screen: mom.
As I slowly pressed the call button, I quickly reflected on whether I had forgotten to block it in my moments, and she found that I was staying up late again.
Sure enough, her fiery voice came from the other end of the phone, but I guessed the beginning, but I couldn't guess the end.
My mother said, "haven't you gone to bed yet? unlock the new level of Xiaolue for me."
While I was still judging whether her operation was intended to test or pretend to attack in order to retreat, she was already worried: "come on, I'm waiting for you!"
For people who don't want to sleep, every sleepless night is similar, but as sober as a chicken, my mother and I have different sources of happiness!
Text
In fact, the theme of Xiaoxiole can be a little more casual, can be moon cakes Mid-Autumn Festival version, animation version and so on. Everything is fine.
Two groups of Xiaoxiole themes are prepared here: 1. Facial expression package Xiaoxiole; 2. Twelve signs of the Chinese zodiac.
Set up the jigsaw puzzle wizard class:
Class gemSprite (pygame.sprite.Sprite): def _ init__ (self, img_path, size, position, downlen, * * kwargs): pygame.sprite.Sprite.__init__ (self) self.image = pygame.image.load (img_path) self.image = pygame.transform.smoothscale (self.image, size) self.rect = self.image.get_rect () self.rect.left Self.rect.top = position self.downlen = downlen self.target_x = position [0] self.target_y = position [1] + downlen self.type = img_path.split ('/') [- 1] .split ('.) [0] self.fixed = False self.speed_x = 10 self.speed_y = 10 self.direction = 'down'
Move the puzzle up and down, left and right:
Def move (self): if self.direction = = 'down': self.rect.top = min (self.target_y, self.rect.top+self.speed_y) if self.target_y = = self.rect.top: self.fixed = True elif self.direction = =' up': self.rect.top = max (self.target_y Self.rect.top-self.speed_y) if self.target_y = = self.rect.top: self.fixed = True elif self.direction = = 'left': self.rect.left = max (self.target_x Self.rect.left-self.speed_x) if self.target_x = = self.rect.left: self.fixed = True elif self.direction = = 'right': self.rect.left = min (self.target_x, self.rect.left+self.speed_x) if self.target_x = = self.rect.left: self.fixed = True
Define the game class and start the game main cycle:
Class gemGame (): def _ _ init__ (self, screen, sounds, font, gem_imgs, cfg * * kwargs): self.info = 'Gemgem' self.screen = screen self.sounds = sounds self.font = font self.gem_imgs = gem_imgs self.cfg = cfg self.reset () def start (self): clock = pygame.time.Clock () # traverse the entire game interface to update the location overall_moving = True # specify the individual update location of some objects individual_moving = False # define some necessary variables gem_selected_xy = None gem_selected_xy2 = None swap_again = False add_score = 0 add_score_showtimes = 10 time_pre = int (time.time ()) while True: for event in pygame. Event.get (): if event.type = = pygame.QUIT or (event.type = = pygame.KEYUP and event.key = = pygame.K_ESCAPE): pygame.quit () sys.exit () elif event.type = = pygame.MOUSEBUTTONUP: if (not overall_moving) and (not individual_moving) and (not add_score ): position = pygame.mouse.get_pos () if gem_selected_xy is None: gem_selected_xy = self.checkSelected (position) else: gem_selected_xy2 = self.checkSelected (position) If gem_selected_xy2: if self.swapGem (gem_selected_xy Gem_selected_xy2): individual_moving = True swap_again = False else: gem_selected_xy = None if overall_moving: overall_moving = not self.dropGems (0 0) # multiple 3-connected blocks can be spelled out at a time if not overall_moving: res_match = self.isMatch () add_score = self.removeMatched (res_match) if add_score > 0: overall_moving = True If individual_moving: gem1 = self.getGemByPos (* gem_selected_xy) gem2 = self.getGemByPos (* gem_selected_xy2) gem1.move () gem2.move () if gem1.fixed and gem2.fixed: res_match = self.isMatch () If res_match [0] = 0 and not swap_again: swap_again = True self.swapGem (gem_selected_xy Gem_selected_xy2) self.sounds ['mismatch'] .play () else: add_score = self.removeMatched (res_match) overall_moving = True individual_moving = False gem_selected_xy = None Gem_selected_xy2 = None self.screen.fill ( two hundred and six, Self.drawGrids () self.gems_group.draw (self.screen) if gem_selected_xy: self.drawBlock (self.getGemByPos (* gem_selected_xy) .rect) if add_score: if add_score_showtimes = = 10: random.choice (self.sounds ['match') ]. Play () self.drawAddScore (add_score) add_score_showtimes-= 1 if add_score_showtimes < 1: add_score_showtimes = 10 add_score = 0 self.remaining_time-= (int (time.time ())-time_pre) time_ Pre = int (time.time ()) self.showRemainingTime () self.drawScore () if self.remaining_time 0: self.generateNewGems (res_match) self.score + = self.reward return self.reward return 0
The defined image special effects:
Def dropGems (self, x, y): if not self.getGemByPos (x, y). Fixed: self.getGemByPos (x, y). Move () if x < self.cfg.NUMGRID-1: X + = 1 return self.dropGems (x, y) elif y < self.cfg.NUMGRID-1: X = 0 y + = 1 return self.dropGems (x Y) else: return self.isFull ()
The position exchange of the puzzle:
Def swapGem (self, gem1_pos Gem2_pos): margin = gem1_pos [0]-gem2_pos [0] + gem1_pos [1]-gem2_pos [1] if abs (margin)! = 1: return False gem1 = self.getGemByPos (* gem1_pos) gem2 = self.getGemByPos (* gem2_pos) if gem1_pos [0]-gem2_pos [0] = 1: gem1 .direction = 'left' gem2.direction =' right' elif gem1_pos [0]-gem2_pos [0] =-1: gem2.direction = 'left' gem1.direction =' right' elif gem1_pos [1]-gem2_pos [1] = 1: gem1.direction = 'up' gem2.direction =' down' Elif gem1_pos [1]-gem2_pos [1] =-1: gem2.direction = 'up' gem1.direction =' down' gem1.target_x = gem2.rect.left gem1.target_y = gem2.rect.top gem1.fixed = False gem2.target_x = gem1.rect.left gem2.target_y = gem1.rect.top gem2.fixed = False Self.all_ gems [Gem2 _ pos [0]] [gem2_pos [1]] = gem1 self.all_ gems [gem1 _ pos [0]] [gem1_pos [1]] = gem2 return True
Attached game main program interface:
Import osimport sysimport cfgimport pygamefrom modules import * def main (): pygame.init () screen = pygame.display.set_mode (cfg.SCREENSIZE) pygame.display.set_caption ('Xiaoxiangle Mini Game') # load background music pygame.mixer.init () pygame.mixer.music.load (os.path.join (cfg.ROOTDIR) "resources/audios/bg.mp3") pygame.mixer.music.set_volume (0.6) pygame.mixer.music.play (- 1) # load sound sounds = {} sounds ['mismatch'] = pygame.mixer.Sound (os.path.join (cfg.ROOTDIR) 'resources/audios/badswap.wav')) sounds [' match'] = [] for i in range (6): sounds ['match'] .append (pygame.mixer.Sound (os.path.join (cfg.ROOTDIR,' resources/audios/match%s.wav'% I) # load font font = pygame.font.Font (os.path.join (cfg.ROOTDIR, 'resources/font/font.TTF')) 25) # Image loading gem_imgs = [] for i in range (1,8): gem_imgs.append (os.path.join (cfg.ROOTDIR, 'resources/images/gem%s.png'% I)) # main loop game = gemGame (screen, sounds, font, gem_imgs) Cfg) while True: score = game.start () flag = False # after a round of games, players choose to replay or exit while True: for event in pygame.event.get (): if event.type = = pygame.QUIT or (event.type = = pygame.KEYUP and event.key = = pygame.K_ESCAPE): pygame Pygame.K_r () sys.exit () elif event.type = = pygame.KEYUP and event.key = = pygame.K_r: flag = True if flag: break screen.fill 206235) text0 = 'Final score:% s'% score text1 = 'Press to restart the game.' Text2 = 'Press to quit the game.' Y = 150for idx, text in enumerate ([text0, text1, text2]): text_render = font.render (text, 1, (85,65,0)) rect = text_render.get_rect () if idx = 0: rect.left, rect.top = Y) elif idx = = 1: rect.left, rect.top = (122.5, y) else: rect.left, rect.top = (126.5, y) y + = 100screen.blit (text_render) Rect) pygame.display.update () game.reset ()''run'''if _ _ name__ = =' _ main__': main ()
Effect picture:
To be honest, this meme version is a good one to find! I can't find a lot of similar ones.
This is the real Kawai, suitable for children at home to play.
This is how people use python to achieve a variety of changes that people can't quit. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.