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 play ping-pong Mini Game with Python?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how Python can play ping-pong Mini Game. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The source code is attached:

Import sysimport cfgimport pygamefrom modules import *''define buttons' 'def Button (screen, position, text, button_size= (200,50)): left, top = position bwidth, bheight = button_size pygame.draw.line (screen, (150,150,150), (left, top), (left+bwidth, top), 5) pygame.draw.line (screen, (150,150,150), (left, top-2), (left, top+bheight) 5) pygame.draw.line (screen, (50,50,50), (left, top+bheight), (left+bwidth, top+bheight), 5) pygame.draw.line (screen, (50,50,50), (left+bwidth, top+bheight), (left+bwidth, top), 5) pygame.draw.rect (screen, (100,100,100), (left, top, bwidth, bheight) font = pygame.font.Font (cfg.FONTPATH, 30) text_render = font.render (text, 1) (255,235,205) return screen.blit (text_render, (left+50, top+10))''Function: start interface Input:-- screen: game interface Return:-- game_mode: 1 (single player mode) / 2 (double player mode)' 'def startInterface (screen): clock = pygame.time.Clock () while True: screen.fill ((41,36,33)) button_1 = Button (screen) (150,175),'1 Player') button_2 = Button (screen, (150,275) '2 Player') for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () sys.exit () if event.type = = pygame.MOUSEBUTTONDOWN: if button_1.collidepoint (pygame.mouse.get_pos ()): return 1 Elif button_2.collidepoint (pygame.mouse.get_pos ()): return 2 clock.tick (10) pygame.display.update ()''end interface' 'def endInterface (screen Score_left, score_right): clock = pygame.time.Clock () font1 = pygame.font.Font (cfg.FONTPATH, 30) font2 = pygame.font.Font (cfg.FONTPATH, 20) msg = 'Player on left wonders' If score_left > score_right else 'Player on right wonders' Texts = [font1.render (msg, True, cfg.WHITE), font2.render ('Press ESCAPE to quit.', True, cfg.WHITE), font2.render (' Press ENTER to continue or play again.', True, cfg.WHITE)] positions = [[120,200], [155,270], [80,300]] while True: screen.fill ((41,36) 33)) for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () sys.exit () if event.type = = pygame.KEYDOWN: if event.key = = pygame.K_RETURN: return elif event.key = = pygame.K_ ESCAPE: sys.exit () pygame.quit () for text Pos in zip (texts, positions): screen.blit (text, pos) clock.tick (10) pygame.display.update ()''run the game Demo'''def runDemo (screen): # load game material hit_sound = pygame.mixer.Sound (cfg.HITSOUNDPATH) goal_sound = pygame.mixer.Sound (cfg.GOALSOUNDPATH) pygame.mixer.music.load (cfg.BGMPATH) pygame.mixer.music.play (- 1 Font = pygame.font.Font (cfg.FONTPATH, 50) # start interface game_mode = startInterface (screen) # game main loop #-left racket (ws control, only in double mode) score_left = 0 racket_left = Racket (cfg.RACKETPICPATH, 'LEFT', cfg) #-right racket (↑↓ control) score_right = 0 racket_right = Racket (cfg.RACKETPICPATH 'RIGHT', cfg) #-Ball ball = Ball (cfg.BALLPICPATH, cfg) clock = pygame.time.Clock () while True: for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () sys.exit (- 1) screen.fill ((41,36) 33) # players operate pressed_keys = pygame.key.get_pressed () if pressed_ Keys [pygame.K _ UP]: racket_right.move ('UP') elif pressed_ Keys [pygame.K _ DOWN]: racket_right.move (' DOWN') if game_mode = = 2: if pressed_key s [pygame.K _ W]: racket_left.move ('UP') elif pressed_ Keys [pygame.Ks]: racket_left.move (' DOWN') else: racket_left.automove (ball) # Ball scores = ball.move (ball Racket_left, racket_right, hit_sound, goal_sound) score_left + = scores [0] score_right + = scores [1] # display #-Separator pygame.draw.rect (screen, cfg.WHITE, (247,0,6) ) #-Ball ball.draw (screen) #-Pat racket_left.draw (screen) racket_right.draw (screen) #-score screen.blit (font.render (str (score_left), False, cfg.WHITE), (150,10) screen.blit (str (score_right), False, cfg.WHITE), 10)) if score_left = 11 or score_right = 11: return score_left, score_right clock.tick (100) pygame.display.update ()''main function' 'def main (): # initialize pygame.init () pygame.mixer.init () screen = pygame.display.set_mode ((cfg.WIDTH) Cfg.HEIGHT)) pygame.display.set_caption ('pingpong-Nine songs') # start the game while True: score_left, score_right = runDemo (screen) endInterface (screen, score_left, score_right)''run'''if _ _ name__ = =' _ main__': main () Thank you for reading! This is the end of the article on "how to play ping-pong Mini Game with Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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