Python实现打乒乓小游戏
作者:红目香薰 时间:2023-07-20 04:48:53
本文实例为大家分享了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()
来源:https://laoshifu.blog.csdn.net/article/details/120425170
标签:Python,打乒乓
0
投稿
猜你喜欢
oracle查看被锁的表和被锁的进程以及杀掉这个进程
2024-01-15 12:04:15
Django框架的中的setting.py文件说明详解
2022-11-05 13:26:24
python生成式的send()方法(详解)
2022-09-02 23:50:56
Python如何实现小程序 无限求和平均
2023-04-13 20:07:40
asp.net上传图片保存到数据库的代码
2024-01-16 05:00:37
Mysql Innodb存储引擎之索引与算法
2024-01-18 02:09:39
基于Vue实现页面切换左右滑动效果
2023-07-02 16:55:10
sql中的 where 、group by 和 having 用法解析
2024-01-14 05:12:13
Vue组件之事件总线和消息发布订阅详解
2024-05-02 16:55:42
SQLServer 存储过程简介与使用方法
2009-07-07 14:06:00
matplotlib调整子图间距,调整整体空白的方法
2021-12-17 15:06:58
python中import与from方法总结(推荐)
2021-12-19 12:31:06
python中defaultdict的用法详解
2021-05-02 11:11:07
在Python中通过getattr获取对象引用的方法
2023-08-24 23:32:33
sql 修改表的所有者
2024-01-14 15:05:19
使用pyqt5 tablewidget 单元格设置正则表达式
2022-12-01 22:26:54
python virtualenv虚拟环境配置与使用教程详解
2023-01-21 23:06:56
Keras设定GPU使用内存大小方式(Tensorflow backend)
2021-12-20 22:15:12
如何在Linux 系统中使用apt 包管理器安装 Git LFS
2022-08-29 15:33:30
golang NewRequest/gorequest实现http请求的示例代码
2024-04-30 10:03:26