In this article, we share examples of Python to achieve the specific code to play ping pong game for your reference, the specific content is as follows
The source code is presented:
import sys import cfg import pygame from modules import * '''Define Buttons''' def Button(screen, position, text, button_size=(200, 50)): left, top = position bwidth, bheight = button_size (screen, (150, 150, 150), (left, top), (left+bwidth, top), 5) (screen, (150, 150, 150), (left, top-2), (left, top+bheight), 5) (screen, (50, 50, 50), (left, top+bheight), (left+bwidth, top+bheight), 5) (screen, (50, 50, 50), (left+bwidth, top+bheight), (left+bwidth, top), 5) (screen, (100, 100, 100), (left, top, bwidth, bheight)) font = (, 30) text_render = (text, 1, (255, 235, 205)) return (text_render, (left+50, top+10)) ''' Function. Start Interface Input. --screen: Game screen Return: --game_mode: 1(single player mode)/2(two player mode) --game_mode: 1(single player)/2(two player) ''' def startInterface(screen): clock = () while True: ((41, 36, 33)) button_1 = Button(screen, (150, 175), '1 Player') button_2 = Button(screen, (150, 275), '2 Player') for event in (): if == : () () if == : if button_1.collidepoint(.get_pos()): return 1 elif button_2.collidepoint(.get_pos()): return 2 (10) () '''End screen''' def endInterface(screen, score_left, score_right): clock = () font1 = (, 30) font2 = (, 20) msg = 'Player on left won!' if score_left > score_right else 'Player on right won!' texts = [(msg, True, ), ('Press ESCAPE to quit.', True, ), ('Press ENTER to continue or play again.', True, )] positions = [[120, 200], [155, 270], [80, 300]] while True: ((41, 36, 33)) for event in (): if == : () () if == : if == pygame.K_RETURN: return elif == pygame.K_ESCAPE: () () for text, pos in zip(texts, positions): (text, pos) (10) () '''Run the Game Demo''' def runDemo(screen): # Load game material hit_sound = () goal_sound = () () (-1, 0.0) font = (, 50) # Start screen game_mode = startInterface(screen) # The main game loop # -- left racket (ws control, only in two player mode) score_left = 0 racket_left = Racket(, 'LEFT', cfg) # --Right racket (↑↓ control) score_right = 0 racket_right = Racket(, 'RIGHT', cfg) # -- ball ball = Ball(, cfg) clock = () while True: for event in (): if == : () (-1) ((41, 36, 33)) # Player manipulation pressed_keys = .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_keys[pygame.K_w]: racket_left.move('UP') elif pressed_keys[pygame.K_s]: racket_left.move('DOWN') else: racket_left.automove(ball) # Ball sports scores = (ball, racket_left, racket_right, hit_sound, goal_sound) score_left += scores[0] score_right += scores[1] # Show # -- divider (screen, , (247, 0, 6, 500)) # -- ball (screen) # - # Shoot # racket_left.draw(screen) racket_right.draw(screen) # -- Score ((str(score_left), False, ), (150, 10)) ((str(score_right), False, ), (300, 10)) if score_left == 11 or score_right == 11: return score_left, score_right (100) () '''Main function''' def main(): # Initialization () () screen = .set_mode((, )) .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()
This is the whole content of this article.