In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to use python to make a game to protect single dogs. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the details of this article.
Text
First of all, prepare the picture material and so on:
Bgm Music essential:
Import pygame class Sound (): def _ _ init__ (self): pygame.mixer.music.load ('.. / sound/baab.mp3') pygame.mixer.music.play ()
Let's start with the effect picture. Hey, watch this:
Game interface--
Program running--
(1) set the background of scrolling:
# Map scrolling import pygameimport random class GameBackground (object): # initialize map def _ _ init__ (self, scene): # load the same image resources Alternate map scrolling self.image1 = pygame.image.load (".. / image/background.png") self.image2 = pygame.image.load (".. / image/background.png") # Save scene object self.main_scene = scene # Auxiliary mobile map self.x1 = 0 self.x2 = 1280 self.snowflag=False # Calculate the map drawing coordinates def action (self): self.x1 = self.x1-1 self.x2 = self.x2-1 if self.x1 = 540 and mousex=180 and mousey= 0: dog.vUP + = 0.8 dog.Y + = dog.vUP if dog.Y > = 290: dog.jumping = False dog.Y = 290 dog.vUP = 0.0 if not collidetest.gameover: if onoff: scor.beginpage () print (onoff) else: print (onoff) # collision detection collidetest.dogs_cpdogs (dogs Cpdogs) collidetest.dog_cpdog (dog Cpdog2s) # background background.action () background.draw () # score # scor.beginpage () collidetest.score+=1 scor.displayScore (collidetest.score) # time control ticks = pygame.time.get_ticks () # player drawing Dogs.update (ticks) dogs.draw (screen) cpdog2s.update (ticks) cpdog2s.draw (screen) if ticks > lasttimecreat + random.randint (1000 ~ 20000): # generate obstacles cp = Monster (screen) cpdogs.add (cp) cpdog2 = MySprite2 () Cpdog2.load (".. / image/cpdogs.png" 113,62,3) cpdog2.X = 1280 cpdog2.Y = 1280 cpdog2s.add (cpdog2) lasttimecreat=ticks for m in cpdogs: m.draw_monster () if m.rect.x 500: snow.y = 0 pygame.draw.circle (screen) [255255255], snow.getsnowpos (), 1) else: if collidetest.snowflag: background.action () background.draw () for snow in snowlist: snow.vx = random.randint (- 3 3) snow.vy = 1 snow.x + = snow.vx snow.y + = snow.vy if snow.y > 500: snow.y = 0 pygame.draw.circle (screen, [255,255,255], snow.getsnowpos () 1) if not crydog: crydog=MySpritec () crydog.load (".. / image/crydog.png", 82 62 crydog.Y 3) crydog.X = 100crydog.Y = 285crydogs=Group () crydogs.add (crydog) ticks = pygame.time.get_ticks () # player draw crydogs.update (ticks) crydogs.draw (screen) scor.gameoverrrrrrrrr () if Crydog.X > 1280: sys.exit () fpsset.tick (60) pygame.display.update ()
Effect picture: the rules of the game avoid lovers' dogs and keep running for extra points!
All right! The single dog fight is about to begin, come on.
The above content is how to use python to make games to protect single dogs. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.