Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Balloon Game by Pygame

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how Pygame realizes the balloon game. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Text

The following balloon Mini Game prototype is the roadside balloon game, based on Pygame!

Just get ready to shoot arrows, balloons of different colors, a background picture, and then the special effects of the explosion. Oh, by the way, the music still needs to be prepared. If the game has a music background, it will be more interesting.

I. preparation

1) material materials

The first is to prepare the required material, pictures, and background music:

2) operating environment

Environment installs the running environment used in this article: Python3.7, Pycharm Community Edition 2020, Pygame game module part comes with

Module direct import does not need to be installed.

Module installation:

Pip install-I https://pypi.douban.com/simple/ + module name II, code demonstration

This Mini Game is composed of 6 .py files, and the code is compared. Just a little bit is put here.

The main program runs:

From game import * def main (): intro = True game = Game () game.loadMusic () game.readHighScore () pygame.mixer.music.play (loops=-1) while intro: for event in pygame.event.get (): if event.type = = pygame.QUIT: intro = False game.screen.fill (SKY_BLUE) game.screen.blit (game.background Game.background_rect) game.draw.Button (200,200, 2*game.HEIGHT/3, "PLAY", BRIGHT_GREEN, GREEN, game.gameloop, 150,100) game.draw.Button (game.WIDTH/2-75, 2*game.HEIGHT/3, "PLAY TIMED", BRIGHT_RED, RED, game.time_restricted, 150100) game.draw.Button (game.WIDTH-350, 2*game.HEIGHT/3, "QUIT", BRIGHT_GREEN, GREEN, quit, 150) Some constants defined by game.draw.draw_text ("_ ArcuS__", game.WIDTH/2, game.HEIGHT/3, 200,200, BLUE) game.draw.draw_text ("HIGH SCORE:%d"% (game.highscore), game.WIDTH-400, 50,30, BLACK) pygame.display.flip () game.clock.tick (FPS) main (): desktop background, music, etc. FPS = 60GRAVITY = 0.15PI = 3.142 WHITE = (255,255,255) BLACK = (0,0,0) RED = (200,0,0) BRIGHT_RED = (255,0,0) GREEN = (0200,0) BRIGHT_GREEN = (0,255,0) SKY_BLUE = (0,255,255) BLUE = (0,0) GREEN_YELLOW= (181 Peregrine 255 VOLUME 98) BROWN= (204 Peregrine 102 Legend 0) DARK_BROWN= HIGHSCORE_FILE= "highscore.txt" ARROW_IMAGE = "assets/arrow_1.png" BACKGROUND_IMAGE = "assets/background.png" EXPLOSION_SOUND = "assets/boom.wav" CLICK_SOUND = "assets/select.wav" MUSIC_FILE = "assets/tgfcoder-FrozenJam-SeamlessLoop.ogg" VOLUME = 0.2ARROW_SIZE = (16150) BALOON_SIZE = HIT_RADIUS = 15MISSES = 15GAME_TIME = 60 define game elves, etc.: import pygameimport mathimport randomfrom os import pathfrom constants import * # game elves class Arrow (pygame.sprite.Sprite): def _ _ init__ (self,game): pygame.sprite.Sprite.__init__ (self) self.WIDTH = game.WIDTH self.HEIGHT = game.HEIGHT self.image_orig = pygame.transform.scale (game.arrow_img) ARROW_SIZE) self.image_orig.set_colorkey (BLACK) self.image = self.image_orig self.rect = self.image.get_rect () self.rect.centerx = self.WIDTH/2 self.rect.bottom = self.HEIGHT-100 self.rot = 0 self.speedx = 0 self.speedy = 0 self.range = 0 self.max_height = 0 Self.release_angle = 0 self.set_vel = False self.Released = False self.releasex = self.rect.centerx self.releasey = self.rect.bottom self.cy = self.rect.centery self.game = game def update (self): if self.Released: self.speedy-= GRAVITY self.rect.bottom-= self.speedy Self.rect.centerx + = self.speedx self.rot = (- math.atan2 (self.speedx Self.speedy) * 180Universe 3.14)% 360 new_image = pygame.transform.rotate (self.image_orig, self.rot) old_center = self.rect.center self.image = new_image self.rect = self.image.get_rect () self.rect.center = old_center # print "moving" if self.rect.bottom

< 0 or self.rect.left >

Self.WIDTH + 10 or self.rect.right

< -10: self.kill() else: mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if mouse[1] >

Self.rect.centery and click [0] = 1: self.set_vel = True dist = math.sqrt (math.pow (self.rect.centerx-mouse [0], 2) + math.pow (self.rect.bottom-mouse [1]) = self.speedy *\ (mouse [0]-self.releasex) / (self.cy-mouse [1]) self.rot = (- math.atan2 (self.speedx) Self.speedy) * 180max 3.14mm 0.5)% 360 new_image = pygame.transform.rotate (self.image_orig) Self.rot) old_center = self.rect.center self.image = new_image self.rect = self.image.get_rect () self.rect.center = old_center # print "setting velocity" else: if self.set_vel: self .Released = True self.game.last_arrow_time = pygame.time.get_ticks () self.max_height = (self.rect.bottom-mouse [1]) self.range = (mouse [0]-self.rect.centerx) * 2 # print "releasing" # math.sqrt (math.pow (mouse [0]-self.rect.centerx 2) + math.pow (mouse [1]-self.rect.centery,2))

< 200: else: if (mouse[0]-self.rect.centerx) != 0: theta = math.atan( (mouse[1]-self.rect.bottom)/(self.rect.centerx-mouse[0])) else: theta = PI move = theta-self.rot self.rot = math.degrees(theta) new_image = pygame.transform.rotate( self.image_orig, self.rot) old_center = self.rect.center self.image = new_image self.rect = self.image.get_rect() self.rect.center = old_center # print "rotating" # print self.rot # print theta class Baloon(pygame.sprite.Sprite): def __init__(self,game): pygame.sprite.Sprite.__init__(self) self.WIDTH = game.WIDTH self.HEIGHT = game.HEIGHT bcolor = random.choice(game.baloon_color) temp = "assets/balloon_{}.png".format(bcolor) self.image_orig = pygame.image.load( path.join(path.dirname(__file__), temp)) if bcolor == "blue": self.image_orig.set_colorkey(BLUE) elif bcolor == "black": self.image_orig.set_colorkey(BLACK) elif bcolor == "green": self.image_orig.set_colorkey(BRIGHT_GREEN) elif bcolor == "red": self.image_orig.set_colorkey(BRIGHT_RED) self.image_orig = pygame.transform.scale(self.image_orig, BALOON_SIZE) self.image = self.image_orig.copy() self.rect = self.image.get_rect() self.radius = HIT_RADIUS temp = random.randrange(self.WIDTH - self.rect.width) while (-150 < temp-self.WIDTH/2 < 150): temp = random.randrange(self.WIDTH - self.rect.width) self.rect.x = temp self.rect.y = random.randrange(self.HEIGHT+100, self.HEIGHT+150) self.speedy = random.randrange(-4, -1) self.speedx = random.randrange(-3, 3) self.game = game self.last_update = pygame.time.get_ticks() # print "baloon" def update(self): self.rect.y += self.speedy if self.rect.top < -20 or self.rect.left < -25 or self.rect.right >

Self.WIDTH + 20: self.kill () self.game.misses + = 1. 3. Effect display

Rules of the game: the arrow will appear at the bottom of the game interface in the middle position, pull it: that is, the left mouse button pull direction can be adjusted by yourself

Just pull it down and shoot the balloon with an arrow. The more shots you hit, the higher the accumulated scores!

1) the screenshot shows the effect--

The game start interface is as follows:

The game start interface is as follows:

The effect of hitting the balloon is as follows:

The total score at the end of the game is 13:

2) Video display effect--

The above is the editor for you to share the Pygame is how to achieve the balloon game, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report