Recently began to learn python, it feels very fun, both the flexibility of scripting language, and rich library and object-oriented features, development is very convenient.
The rules of the game, like table tennis, if the frog seeds fall off the ground, even if you lose, you can use the blue seesaw to play it, so that he does not fall on the ground.
You can press any key to resume the game or opt out after game over.
The code is as follows:
1 Import sys, pygame2 fromRandom Import *3 fromPygame.locals Import *4 fromPygame.font Import *5 classMyplayer (pygame.sprite.Sprite):6 def __init__ (self, image_file, location, speed):7 pygame.sprite.sprite.__init__ (self)8Self.image =pygame.image.load (image_file)9Self.rect =Self.image.get_rect ()TenSelf.rect.left, Self.rect.top = Location OneSelf.speed = Speed ASelf.status =True - def Move (self): -Self.rect =Self.rect.move (self.speed) the ifSelf.rect.left <0or Self.rect.right >Width: -self.speed[0] =-self.speed[0] - ifSelf.rect.top <0: -self.speed[1] =-self.speed[1] + ifSelf.rect.bottom >Height: - #Game over +Self.status =False A classReflector (pygame.sprite.Sprite): at def __init__ (self, image_file, location, speed): - pygame.sprite.sprite.__init__ (self) -Self.image =pygame.image.load (image_file) -Self.rect =Self.image.get_rect () -Self.rect.left, Self.rect.top = Location -Self.speed = Speed in def Move (self): -Self.rect =Self.rect.move (self.speed) to ifSelf.rect.left <0or Self.rect.right >Width: +self.speed[0] =-self.speed[0] - ifSelf.rect.top <0or Self.rect.bottom >Height: theself.speed[1] =-self.speed[1] * def animate (players): $Screen.fill ([255,255,255])Panax Notoginseng forPlayerinchPlayers: - Player.move () the forPlayerinchPlayers: + players.remove (player) A ifpygame.sprite.spritecollide (player,players,false): theplayer.speed[0] =-player.speed[0] +player.speed[1] =-player.speed[1] - Players.add (player) $ Player.move () $ screen.blit (player.image,player.rect) - Pygame.display.flip () -Pygame.time.delay (Ten) the - Pygame.init ()WuyiSize = Width,height =640,480 theScreen =pygame.display.set_mode (size) -Screen.fill ([255,255,255]) WuPygame.display.set_caption ("Miaowa Game") - def play (): AboutImg_player ="C:\Users\dswu\Desktop\player.png" $Players =Pygame.sprite.Group () - forRowinchRange0,1): - forColumninchRange0,1): -Playerlocation = [column* -+Ten, row* -+Ten] APlayerspeed = [Choice ([-2,2]), Choice ([-2,2])] +Player =Myplayer (Img_player, Playerlocation, Playerspeed) the Players.add (player) -Img_ref_path ="C:\Users\dswu\Desktop\Reflector.png" $Ref_pos = [0,464] theRef_speed = [0,0] theReflector =Reflector (Img_ref_path, Ref_pos, Ref_speed) the Players.add (Reflector) therunning =True - whileRunning: inkey_pressed =pygame.key.get_pressed () the for Event inchPygame.Event.Get(): the if Event. Type = =Pygame. QUIT: About game.quit () the if Event. Type = =KEYDOWN: the if Event. Key = =K_left: theref_speed[0] = -2 +ElifEvent. Key = =K_right: -ref_speed[0] = +2 the Animate (Players)Bayi ifPlayer.status = =False: therunning =False theFinal_text ="Game over!" -Ft_font = Pygame.font.Font (None, -) -Ft_surf = Ft_font.render (Final_text,1, (0,0,0)) theScreen.blit (Ft_surf, [Screen.get_width ()/2-Ft_surf.get_width ()/2, -]) theTip_text ="Type any key to continue" theTip_font = Pygame.font.Font (None, -) theTip_surf = Tip_font.render (Tip_text,1, (0,0,0)) -Screen.blit (Tip_surf, [Screen.get_width ()/2-Tip_surf.get_width ()/2, $]) the Pygame.display.flip () theKeepon =True the whileKeepon:94key_pressed =pygame.key.get_pressed () the for Event inchPygame.Event.Get(): the if Event. Type = =Pygame. QUIT: the pygame.quit ()98 if Event. Type = =KEYDOWN: About Play () -Play ()
Mainly through the Pygame.sprite.Sprite class to achieve collision monitoring, through the capture and judgment of the event to achieve this elastic ball game, which involves the text in the interface display, game in the loop control, and restart the game.
Paste the above code into your idle, find the following two lines to replace the picture path in your machine, press F5 to run.
" C:\Users\dswu\Desktop\player.png " "C:\Users\dswu\Desktop\Reflector.png"
Because it is relatively simple, look at the code to run a bit should be almost clear, if there is not understand the place please give me a message, convenient to learn and progress together.
Usually online, welcome to Exchange.
Write a ping-pong game in Python