Python has both the flexibility of scripting language, and rich library and object-oriented features, it is very convenient to develop.
Game rules and table tennis, if the frog seeds dropped on the ground even if you lose, you can use the blue seesaw to play it, so that he does not fall to the ground, game over can press any key to continue the game or opt out;
Paste the following code into your idle, find the following two lines to replace the picture path in your machine, press F5 to run.
Img_player = "C:\Users\dswu\Desktop\player.png"
Img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"
The code is as follows:
1 Importsys, Pygame2 fromRandomImport*3 fromPygame.localsImport*4 fromPygame.fontImport*5 classMyplayer (pygame.sprite.Sprite):6 def __init__(self, image_file, location, speed):7Pygame.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 - defMove (self): -Self.rect =Self.rect.move (self.speed) the ifSelf.rect.left < 0orSelf.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 defMove (self): -Self.rect =Self.rect.move (self.speed) to ifSelf.rect.left < 0orSelf.rect.right >Width: +Self.speed[0] =-Self.speed[0] - ifSelf.rect.top < 0orSelf.rect.bottom >Height: theSELF.SPEED[1] =-self.speed[1] * defAnimate (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 (10) 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") - defPlay (): AboutImg_player ="C:\Users\dswu\Desktop\player.png" $Players =Pygame.sprite.Group () - forRowinchRange (0,1): - forColumninchRange (0,1): -Playerlocation = [column*250+10,row*250+10] 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 forEventinchpygame.event.get (): the ifEvent.type = =Pygame. QUIT: About game.quit () the ifEvent.type = =KEYDOWN: the ifEvent.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, 100) -Ft_surf = Ft_font.render (Final_text, 1, (0,0,0)) theScreen.blit (Ft_surf, [Screen.get_width ()/2-ft_surf.get_width ()/2, 100]) theTip_text ="Type any key to continue" theTip_font = Pygame.font.Font (None, 50) theTip_surf = Tip_font.render (Tip_text, 1, (0,0,0)) -Screen.blit (Tip_surf, [Screen.get_width ()/2-tip_surf.get_width ()/2, 200]) the Pygame.display.flip () theKeepon =True the whileKeepon:94key_pressed =pygame.key.get_pressed () the forEventinchpygame.event.get (): the ifEvent.type = =Pygame. QUIT: the pygame.quit ()98 ifEvent.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.
Code run a bit should be almost understand, if there is not understand the place please give me a message, convenient to learn and progress together.
Table tennis effect in Python