Python实现打乒乓小游戏
程序员文章站
2022-03-04 15:25:04
本文实例为大家分享了python实现打乒乓小游戏的具体代码,供大家参考,具体内容如下源码奉上:import sysimport cfgimport pygamefrom modules import...
本文实例为大家分享了python实现打乒乓小游戏的具体代码,供大家参考,具体内容如下
源码奉上:
import sys import cfg import pygame from modules import * '''定义按钮''' 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: 开始界面 input: --screen: 游戏界面 return: --game_mode: 1(单人模式)/2(双人模式) ''' 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() '''结束界面''' 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 won!' if score_left > score_right else 'player on right won!' 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() '''运行游戏demo''' def rundemo(screen): # 加载游戏素材 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, 0.0) font = pygame.font.font(cfg.fontpath, 50) # 开始界面 game_mode = startinterface(screen) # 游戏主循环 # --左边球拍(ws控制, 仅双人模式时可控制) score_left = 0 racket_left = racket(cfg.racketpicpath, 'left', cfg) # --右边球拍(↑↓控制) score_right = 0 racket_right = racket(cfg.racketpicpath, 'right', cfg) # --球 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)) # 玩家操作 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_keys[pygame.k_w]: racket_left.move('up') elif pressed_keys[pygame.k_s]: racket_left.move('down') else: racket_left.automove(ball) # 球运动 scores = ball.move(ball, racket_left, racket_right, hit_sound, goal_sound) score_left += scores[0] score_right += scores[1] # 显示 # --分隔线 pygame.draw.rect(screen, cfg.white, (247, 0, 6, 500)) # --球 ball.draw(screen) # --拍 racket_left.draw(screen) racket_right.draw(screen) # --得分 screen.blit(font.render(str(score_left), false, cfg.white), (150, 10)) screen.blit(font.render(str(score_right), false, cfg.white), (300, 10)) if score_left == 11 or score_right == 11: return score_left, score_right clock.tick(100) pygame.display.update() '''主函数''' def main(): # 初始化 pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((cfg.width, cfg.height)) pygame.display.set_caption('pingpong —— 九歌') # 开始游戏 while true: score_left, score_right = rundemo(screen) endinterface(screen, score_left, score_right) '''run''' if __name__ == '__main__': main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。