Write a ping-pong game with Python

Source: Internet
Author: User
Tags reflector

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:

1Importsys, Pygame2 fromRandomImport* 3 fromPygame.localsImport* 4 fromPygame.fontImport* 5classMyplayer (pygame.sprite.Sprite):6def __init__(self, image_file, location, speed):7 Pygame.sprite.Sprite.__init__(self)8 Self.image =pygame.image.load (image_file)9 Self.rect =Self.image.get_rect ()Ten self.rect.left, self.rect.top = LocationOne self.speed = SpeedSelf.status =True13defMove (self):Self.rect =Self.rect.move (self.speed)15ifSelf.rect.left < 0orSelf.rect.right >Width:Self.speed[0] =-Self.speed[0]17ifSelf.rect.top <0:SELF.SPEED[1] =-self.speed[1]  19ifSelf.rect.bottom >Height:20#Game overSelf.status =False22classReflector (pygame.sprite.Sprite):23def __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 = LocationSelf.speed = Speed29defMove (self):Self.rect =Self.rect.move (self.speed)31ifSelf.rect.left < 0orSelf.rect.right >Width:Self.speed[0] =-Self.speed[0]33ifSelf.rect.top < 0orSelf.rect.bottom >Height:SELF.SPEED[1] =-self.speed[1]  35defAnimate (Players):Screen.fill ([255,255,255])  37 forPlayerinchPlayers:38Player.move ()39 forPlayerinchPlayers:40players.remove (player)41ifpygame.sprite.spritecollide (player,players,false):Player.speed[0] =-Player.speed[0]PLAYER.SPEED[1] =-player.speed[1]  44Players.add (player)45Player.move ()46screen.blit (player.image,player.rect)47Pygame.display.flip ()Pygame.time.delay (10)  49 50Pygame.init ()Wuyi size = Width,height = 640,480pygame.display.set_mode (size)Screen.fill ([255,255,255])  Pygame.display.set_caption ("Miaowa Game")  55defPlay ():Img_player ="C:\Users\dswu\Desktop\player.png"Players =Pygame.sprite.Group ()58 forRowinchRange (0,1):  59 forColumninchRange (0,1):  Playerlocation = [column*250+10,row*250+10]  Playerspeed = [Choice ([ -2,2]), Choice ([ -2,2])]  Player =Myplayer (Img_player, Playerlocation, Playerspeed)63Players.add (player)Img_ref_path ="C:\Users\dswu\Desktop\Reflector.png"Ref_pos = [0,464]  Ref_speed =[0,0]Reflector =Reflector (Img_ref_path, Ref_pos, Ref_speed)68Players.add (Reflector)running =True70 whileRunning:key_pressed =pygame.key.get_pressed ()72 forEventinchpygame.event.get ():73ifEvent.type = =Pygame. QUIT:74game.quit ()75ifEvent.type = =KEYDOWN:76ifEvent.key = =K_left:Ref_speed[0] =-2 78elifEvent.key = =K_right:Ref_speed[0] = +2 80Animate (Players)81ifPlayer.status = =False:running =FalseFinal_text ="Game over!"Ft_font = Pygame.font.Font (None, 100)  Ft_surf = Ft_font.render (Final_text, 1, (0,0,0))Screen.blit (Ft_surf, [Screen.get_width ()/2-ft_surf.get_width ()/2, 100])  Tip_text ="Type any key to continue"Tip_font = Pygame.font.Font (None, 50)  Tip_surf = Tip_font.render (Tip_text, 1, (0,0,0))Screen.blit (Tip_surf, [Screen.get_width ()/2-tip_surf.get_width ()/2, 200])  91Pygame.display.flip ()Keepon =True93 whileKeepon:94 key_pressed =pygame.key.get_pressed ()95 forEventinchpygame.event.get ():96ifEvent.type = =Pygame. QUIT:97pygame.quit ()98ifEvent.type = =KEYDOWN:99Play ()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.

Img_player = "C:\Users\dswu\Desktop\player.png"

Img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"

Write a ping-pong game with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.