In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use python to achieve flappy bird games". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The process of the program is as follows: 1. Load picture material file 2. Draw the start interface and wait for the program to start (press space) 3. Program refresh, continuous while loop (a. Randomly generate columns, move them, and then draw b. Birds fall from the ground c. Detect the keyboard direction key) 4. At the end of the program, draw the end interface and record the scores to the txt file.
#-*-coding: utf-8-*-"Created on Thu Dec 15 00:27:17 2016BY LINJUNJI SYSU 1360622136@qq.com" import pygamefrom sys import exitimport randomfrom pygame.locals import * from pygame.font import * from gamerole import * import osimport datetimeimport numpy as npimport operator# picture path information BackGround_image_path2='./sources/background/day.png' # background image BackGround_image_path3='./sources/background/night.png'GetStart_image_path= " '. / sources/guide/day/down.png' # start interface GameOver_image_path='./sources/other/gameover.png'Restart_image_path='./sources/other/restart.png'Grade_blackball_path='./sources/other/grade_blackball.png'white_gold_Medal_image_path='./sources/medal/white_gold_medal.png' # medal picture gold_image_path='./sources/medal/gold_medal. Png'silver_medal_image_path='./sources/medal/silver_medal.png'bronze_medal_image_path='./sources/medal/bronze_medal.png'Ground_image_path='./sources/background/ground.png' # ground Image pilar_image_up_path='./sources/pilar/up.png' # Pillar Picture pilar_image_down_path='./sources/pilar/down.png'Bird_image1 _ path='./sources/bird/up.png' # Bird Picture Bird_image2_path='./sources/bird/med.png'Bird_image3_path='./sources/bird/down.png'# Game form Settings pygame.init () screen = pygame.display.set_mode ((SCREEN_WIDTH) SCREEN_HEIGHT)) pygame.display.set_caption ('falppy_bird') # picture is loaded into background_day=pygame.image.load (BackGround_image_path2) background_night=pygame.image.load (BackGround_image_path3) ground=pygame.image.load (Ground_image_path) pilar_image_up=pygame.image.load (pilar_image_up_path) pilar_image_down=pygame.image.load (pilar_image_down_path) get_start_image=pygame.image.load (GetStart_image_path) Gameover_image=pygame.image.load (GameOver_image_path) medal_blackball_image=pygame.image.load (Grade_blackball_path) restart_image=pygame.image.load (Restart_image_path) bird_images= [] bird_image_up=pygame.image.load (Bird_image1_path) bird_image_med=pygame.image.load (Bird_image2_path) bird_image_down=pygame.image.load (Bird_image3_path) bird_images.append (bird_image_up) bird_images.append (bird_image_med ) bird_images.append (bird_image_down) medal_images= [] medal1=pygame.image.load (white_gold_Medal_image_path) medal2=pygame.image.load (gold_image_path) medal3=pygame.image.load (silver_medal_image_path) medal4=pygame.image.load (bronze_medal_image_path) medal_images.append (medal1) medal_images.append (medal2) medal_images.append (medal3) medal_images.append (medal4) # New Bird bird_pos= [190190] # Bird initial position mybird=Bird (bird_images Bird_pos) # column set pilar_set = pygame.sprite.Group () # run parameter setting pilar_frequency=0 # column update parameter bird_frequency=0 # Bird flight frequency clock = pygame.time.Clock () running=Falsescore=0flag=1def collide_circle (pilar, mybird): # collision detection function if mybird.rect.right > pilar.pilar1_rect.left and mybird.rect.left
< pilar.pilar1_rect.right: if (mybird.rect.top >Pilar.pilar1_rect.bottom and mybird.rect.bottom 400: return True else: return False def get_history_record (score): # get the score of the record record= [] index = 0 if os.path.isfile ("record.txt"): # exist: get the content of the record f=open ("record.txt") line=f.readline () Line=f.readline () while line! = "": record.append ([int (line.strip (). Split (") ") [0]), line.strip (). Split (",) [1]]) line=f.readline () f.close () record.append ([score,str (datetime.datetime.now ())]) record.sort (key=operator.itemgetter (0), reverse=True) print record while len (record) > 10: record.pop () file_writer = open ("record.txt") 'w') file_writer.writelines ("time" + "," + "grade" + "\ n") for i in range (len (record)): file_writer.writelines (str (record [I] [0])) + " "+ str (record [I] [1]) +"\ n ") if int (record [I] [0]) = = int (score): index=i file_writer.close () if index > 3: index=3 return record [0] [0], index while not running: clock.tick (60) screen.fill (0) screen.blit (get_start_image, (0,0)) pygame.display.update () x Y=pygame.mouse.get_pos () for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () exit () elif event.type = = pygame.MOUSEBUTTONDOWN: if y300 and x > 180 and x = 1000: pilar_frequency=0 flag= (- 1) * flag # move column for pilar in pilar_set: Pilar.move () if collide_circle (pilar Mybird): # collision detection code mybird.is_hit=True for pilar in pilar_set: pilar.stop () if pilar.pilar1_rect.right 150 and x = 400: self.rect.bottom = 400 self.is_downtoground=True def moveUp (self): if self.rect.top=SCREEN_HEIGHT-self.rect.height: Self.rect.top = SCREEN_HEIGHT-self.rect.height else: self.rect.top + = self.down_speed''def moveLeft (self): if self.rect.left = SCREEN_WIDTH-self.rect.width: self.rect.left = SCREEN_WIDTH-self.rect.width else: self.rect.left + = self.speed '' # column class class Pilar (pygame.sprite.Sprite): def _ init__ (self Pilar_image_up,pilar_image_down,init_pos): pygame.sprite.Sprite.__init__ (self) self.pilar1_image=pilar_image_up self.pilar2_image=pilar_image_down self.pilar1_rect=self.pilar1_image.get_rect () self.pilar2_rect=self.pilar2_image.get_rect () self.pilar1_rect.bottomleft=init_pos self.pilar2_rect.topleft= [init _ pos [0] Init_pos [1] + INTERVEL] self.horizontal_speed=2 # the speed of the translation of the column self.vertical_speed=0.3 # the speed of the column moving up and down self.direction=random.randint (0mem1) # the direction of the column moving up and down def move (self): self.pilar1_rect.left-= self.horizontal_speed # the column moves left and right self.pilar2_rect.left -= self.horizontal_speed if self.direction= = 1: # Control column up and down self.pilar1_rect.bottom + = self.vertical_speed self.pilar2_rect.top + = self.vertical_speed if self.pilar2_rect.top > DOWN_LIMIT: self.direction=0 else: self. Pilar1_rect.bottom-= self.vertical_speed self.pilar2_rect.top-= self.vertical_speed if self.pilar1_rect.bottom < UP_LIMIT: self.direction=1 def stop (self): self.horizontal_speed=0 self.vertical_speed=0
Pictures of the material of the game
"how to use python to achieve flappy bird games" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.