[Pygame practice] The Adventures of meow: the programmer's Guide to cat smoking: it's really great~

Introduction Hello ~ Hello, I'm Muzi. First o...
1, In preparation
2, Environment installation
3, Formal knock code
4, Effect display
summary
👑 Article summary——
Introduction

Hello ~ Hello, I'm Muzi. First of all, Muzi will tell you a little story:

In the meow world, there is such a net red - mixed in the two-dimensional and expression pack world, cheap, cute, lively, naughty and clever, with a big white round face and neck

It is said that it can be switched freely among children, teenagers and young people.

His joy brings warmth and laughter to many people, and his optimism is also the treatment of sad, empty, lonely and cold syndrome

He's the right recipe for it - ow, meow!

Ps introduction:

AODA meow information: he is a cheerful, optimistic, passionate and warm meow star. He is as quiet as paralysis and as active as epilepsy. He likes it

The thing is to eat and sleep. His directness and loveliness are often mistaken for neuropathy.

AODA meow and his little friends live happily together. They can always bring joy to everyone. Everyone said, "Oh, big meow, really

Great! ". ​

...........................................................................................................................................................

That's about it. The little game to be written today is also related to Ao Da meow~

Use this "net red" ip to create a unique AODA meow expression package game to relieve your fatigue. Oh ~ fishing at work is also OK. jpg

...........................................................................................................................................................

Text

1, In preparation

1.1 material preparation

Background music: (modifiable)

Picture material: (modifiable)

1.2 rules of the game

AODA meow's distress: AODA meow is the player. When the plane is lost, a cat 🐱 Fall alone to an uninhabited desert island, fortunately on the desert island

With sufficient resources, AODA meow survived. One day, AODA meow chased a strange creature and ran to a very high place

At the entrance of the cave, curiosity drove AODA meow nervously into the cave. Unexpectedly, as soon as he entered the cave, he met a legendary dragon. AODA meow was scared

I had to run out of the cave quickly - I have been chased by the dragon since then

Over, cough... That's the story of children. All we have to do is save AODA meow ~ away from the dragon.

How to play: hold down the space to avoid the dragon's flame. If you hit it, you will slow down and be eaten by the dragon.

2, Environment installation

Environment: Python 3, pychar, Pygame and some built-in modules.

Here are the module installation commands:

pip install +Module name or Douban image source pip install -i https://Pypi. Doublan. COM / simple / + module name

3, Formal knock code

three point one   Define the start game button

# -*- coding: utf-8 -*- import pygame from sys import exit from pygame.locals import * pygame.init() screen = pygame.display.set_mode((300,200),0,32) upImageFilename = 'game_start_up.png' downImageFilename = 'game_start_down.png' class Button(object): def __init__(self, upimage, downimage,position): self.imageUp = pygame.image.load(upimage).convert_alpha() self.imageDown = pygame.image.load(downimage).convert_alpha() self.position = position def isOver(self): point_x,point_y = pygame.mouse.get_pos() x, y = self. position w, h = self.imageUp.get_size() in_x = x - w/2 < point_x < x + w/2 in_y = y - h/2 < point_y < y + h/2 return in_x and in_y def render(self): w, h = self.imageUp.get_size() x, y = self.position if self.isOver(): screen.blit(self.imageDown, (x-w/2,y-h/2)) else: screen.blit(self.imageUp, (x-w/2, y-h/2)) def is_start(self): b1,b2,b3 = pygame.mouse.get_pressed() if b1 == 1: return True button = Button(upImageFilename,downImageFilename, (150,100)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() screen.fill((200, 200, 200)) button.render() if button.is_start(): print ("start") pygame.display.update()

3.11 effect screenshot attached

three point two   main program

# -*- coding: utf-8 -*- import sys, time, random, math, pygame,locale from pygame.locals import * from MyLibrary import * #Reset rocket function def reset_arrow(): y = random.randint(270,350) arrow.position = 800,y bullent_sound.play_sound() #Define a scrolling map class class MyMap(pygame.sprite.Sprite): def __init__(self,x,y): self.x = x self.y = y self.bg = pygame.image.load("background.png").convert_alpha() def map_rolling(self): if self.x < -600: self.x = 600 else: self.x -=5 def map_update(self): screen.blit(self.bg, (self.x,self.y)) def set_pos(x,y): self.x =x self.y =y #Define a button class class Button(object): def __init__(self, upimage, downimage,position): self.imageUp = pygame.image.load(upimage).convert_alpha() self.imageDown = pygame.image.load(downimage).convert_alpha() self.position = position self.game_start = False def isOver(self): point_x,point_y = pygame.mouse.get_pos() x, y = self. position w, h = self.imageUp.get_size() in_x = x - w/2 < point_x < x + w/2 in_y = y - h/2 < point_y < y + h/2 return in_x and in_y def render(self): w, h = self.imageUp.get_size() x, y = self.position if self.isOver(): screen.blit(self.imageDown, (x-w/2,y-h/2)) else: screen.blit(self.imageUp, (x-w/2, y-h/2)) def is_start(self): if self.isOver(): b1,b2,b3 = pygame.mouse.get_pressed() if b1 == 1: self.game_start = True bg_sound.play_pause() btn_sound.play_sound() bg_sound.play_sound() def replay_music(): bg_sound.play_pause() bg_sound.play_sound() #Define a data IO method def data_read(): fd_1 = open("data.txt","r") best_score = fd_1.read() fd_1.close() return best_score #Define a class that controls sound and a method for initial audio def audio_init(): global hit_au,btn_au,bg_au,bullent_au pygame.mixer.init() hit_au = pygame.mixer.Sound("exlposion.wav") btn_au = pygame.mixer.Sound("button.wav") bg_au = pygame.mixer.Sound("background.ogg") bullent_au = pygame.mixer.Sound("bullet.wav") class Music(): def __init__(self,sound): self.channel = None self.sound = sound def play_sound(self): self.channel = pygame.mixer.find_channel(True) self.channel.set_volume(0.5) self.channel.play(self.sound) def play_pause(self): self.channel.set_volume(0.0) self.channel.play(self.sound) #Main program part pygame.init() audio_init() screen = pygame.display.set_mode((800,600),0,32) pygame.display.set_caption("Ow, meow, run!") font = pygame.font.Font(None, 22) font1 = pygame.font.Font(None, 40) framerate = pygame.time.Clock() upImageFilename = 'game_start_up.png' downImageFilename = 'game_start_down.png' #Create button object button = Button(upImageFilename,downImageFilename, (400,500)) interface = pygame.image.load("interface.png") #Create map objects bg1 = MyMap(0,0) bg2 = MyMap(600,0) #Create a sprite group group = pygame.sprite.Group() group_exp = pygame.sprite.Group() group_fruit = pygame.sprite.Group() #Create monster spirit dragon = MySprite() dragon.load("dragon.png", 260, 150, 3) dragon.position = 100, 230 group.add(dragon) #Create explosion animation explosion = MySprite() explosion.load("explosion.png",128,128,6) #Create player Sprite player = MySprite() player.load("sprite.png", 100, 100, 4) player.position = 400, 270 group.add(player) #Create bullet Wizard arrow = MySprite() arrow.load("flame.png", 40, 16, 1) arrow.position = 800,320 group.add(arrow) #Define some variables arrow_vel = 10.0 game_over = False you_win = False player_jumping = False jump_vel = 0.0 player_start_y = player.Y player_hit = False monster_hit = False p_first = True m_first = True best_score = 0 global bg_sound,hit_sound,btn_sound,bullent_sound bg_sound=Music(bg_au) hit_sound=Music(hit_au) btn_sound=Music(btn_au) bullent_sound =Music(bullent_au) game_round = game_pause = True index =0 current_time = 0 start_time = 0 music_time = 0 score =0 replay_flag = True #loop bg_sound.play_sound() best_score = data_read() while True: framerate.tick(60) ticks = pygame.time.get_ticks() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[K_ESCAPE]: pygame.quit() sys.exit() elif keys[K_SPACE]: if not player_jumping: player_jumping = True jump_vel = -12.0 screen.blit(interface,(0,0)) button.render() button.is_start() if button.game_start == True: if game_pause : index +=1 tmp_x =0 if score >int (best_score): best_score = score fd_2 = open("data.txt","w+") fd_2.write(str(best_score)) fd_2.close() #Judge whether the game passes the customs if index == 6: you_win = True if you_win: start_time = time.clock() current_time =time.clock()-start_time while current_time<5: screen.fill((200, 200, 200)) print_text(font1, 270, 150,"YOU WIN THE GAME!",(240,20,20)) current_time =time.clock()-start_time print_text(font1, 320, 250, "Best Score:",(120,224,22)) print_text(font1, 370, 290, str(best_score),(255,0,0)) print_text(font1, 270, 330, "This Game Score:",(120,224,22)) print_text(font1, 385, 380, str(score),(255,0,0)) pygame.display.update() pygame.quit() sys.exit() for i in range(0,100): element = MySprite() element.load("fruit.bmp", 75, 20, 1) tmp_x +=random.randint(50,120) element.X = tmp_x+300 element.Y = random.randint(80,200) group_fruit.add(element) start_time = time.clock() current_time =time.clock()-start_time while current_time<3: screen.fill((200, 200, 200)) print_text(font1, 320, 250,game_round[index],(240,20,20)) pygame.display.update() game_pause = False current_time =time.clock()-start_time else: #Update bullet if not game_over: arrow.X -= arrow_vel if arrow.X < -40: reset_arrow() #Collision detection, does the bullet hit the player if pygame.sprite.collide_rect(arrow, player): reset_arrow() explosion.position =player.X,player.Y player_hit = True hit_sound.play_sound() if p_first: group_exp.add(explosion) p_first = False player.X -= 10 #Collision detection, does the bullet hit the monster if pygame.sprite.collide_rect(arrow, dragon): reset_arrow() explosion.position =dragon.X+50,dragon.Y+50 monster_hit = True hit_sound.play_sound() if m_first: group_exp.add(explosion) m_first = False dragon.X -= 10 #Collision detection, whether the player is caught up by the monster if pygame.sprite.collide_rect(player, dragon): game_over = True #Traverse the fruit and make the fruit move for e in group_fruit: e.X -=5 collide_list = pygame.sprite.spritecollide(player,group_fruit,False) score +=len(collide_list) #Pass the level if dragon.X < -100: game_pause = True reset_arrow() player.X = 400 dragon.X = 100 #Check whether the player is jumping if player_jumping: if jump_vel <0: jump_vel += 0.6 elif jump_vel >= 0: jump_vel += 0.8 player.Y += jump_vel if player.Y > player_start_y: player_jumping = False player.Y = player_start_y jump_vel = 0.0 #Draw background bg1.map_update() bg2.map_update() bg1.map_rolling() bg2.map_rolling() #Update sprite group if not game_over: group.update(ticks, 60) group_exp.update(ticks,60) group_fruit.update(ticks,60) #Loop background music music_time = time.clock() if music_time > 150 and replay_flag: replay_music() replay_flag =False #Draw sprite group group.draw(screen) group_fruit.draw(screen) if player_hit or monster_hit: group_exp.draw(screen) print_text(font, 330, 560, "press SPACE to jump up!") print_text(font, 200, 20, "You have get Score:",(219,224,22)) print_text(font1, 380, 10, str(score),(255,0,0)) if game_over: start_time = time.clock() current_time =time.clock()-start_time while current_time<5: screen.fill((200, 200, 200)) print_text(font1, 300, 150,"GAME OVER!",(240,20,20)) current_time =time.clock()-start_time print_text(font1, 320, 250, "Best Score:",(120,224,22)) if score >int (best_score): best_score = score print_text(font1, 370, 290, str(best_score),(255,0,0)) print_text(font1, 270, 330, "This Game Score:",(120,224,22)) print_text(font1, 370, 380, str(score),(255,0,0)) pygame.display.update() fd_2 = open("data.txt","w+") fd_2.write(str(best_score)) fd_2.close() pygame.quit() sys.exit() pygame.display.update()

4, Effect display

4.1 dynamic video display——

The adventures of meow~

4.2 static screenshot display——

summary

It's up to you whether Mengmeng's AODA meow can escape the pursuit of the dragon~

Poof ~ hahaha, I feel that my recent painting style is a little wrong. They are all strange and lovely children's painting styles!

🎯 Complete free source code collection:

For the complete project source code + material source code base, see: # private letter editor 06# or click the blue text to add to get free benefits!

Your support is my biggest motivation!! Remember the third company ~mua   Welcome to read previous articles~

🎉 Recommended popular articles of previous games:

Item 1.8   Man machine Gobang game  

Pygame practice: use Python to realize intelligent Gobang. After implementation, I found that I couldn't win it.

Item 4.2   My world game

Pygame actual combat: the square connects the world and travels around the great rivers and mountains - my world has been launched. Are you sure you don't want to come to Kangkang?

Item 4.4   Plot version game

[Python plot version game] beautiful and exquisite painting style, sweet plot, it's hard not to let people go? Did you get it

Item 1.1     mine clearance

  Pygame actual combat: it is said that this is the most difficult minesweeping game in history. There is no one. Feel it

Item 1.2     Contra

Pygame actual combat: many years later, the pixel style of "soul duel" returned, not just classics and feelings @ all members

22 November 2021, 00:02 | Views: 4933

Add new comment

For adding a comment, please log in
or create account

0 comments