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 =跟單網gendan5.com 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()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2912749/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python專案例項原始碼-32個Python爬蟲實戰專案,滿足你的專案慌(帶原始碼)Python原始碼爬蟲
- 7個Python實戰專案(附原始碼),拿走就用Python原始碼
- 吐血總結!10個Python實戰專案(附原始碼)Python原始碼
- 精選了20個Python實戰專案(附原始碼),拿走就用!Python原始碼
- 520專屬Python程式碼分享Python
- 小程式入門到實戰(二)--案例原始碼分享原始碼
- 《整潔程式碼cleancode》的javascript原始碼實現專案JavaScript原始碼
- 7個Python實戰專案程式碼,讓你分分鐘晉級大神!Python
- 2021風口紅利專案—短視訊同城分享碼
- 【RAG 專案實戰 05】重構:封裝程式碼封裝
- Java專案:醫院分診管理系統,HIS原始碼 原始碼分享Java原始碼
- python畢業設計專案原始碼Python原始碼
- AMP專案實戰分享
- python實戰專案Python
- 短影片程式原始碼,如何實現短影片的熱門頁面原始碼
- 分享《TensorFlow實戰》中文版PDF+原始碼原始碼
- rocketmq-spring : 實戰與原始碼解析一網打盡MQSpring原始碼
- 70個Python經典實用練手專案(附原始碼)Python原始碼
- 整理了70個Python實戰專案案例,教程+原始碼+筆記。從基礎到深入Python原始碼筆記
- python 坦克大戰原始碼Python原始碼
- 《Python Web開發實戰》隨書原始碼PythonWeb原始碼
- Python開發爬蟲專案+程式碼Python爬蟲
- 7個Python實戰專案程式碼,讓你感受下大神是如何起飛的!Python
- ASP.NET綜合工程專案管理,原始碼分享ASP.NET專案管理原始碼
- DateUtils(話不多說直接上程式碼)
- 【Python】Python抓取分享頁面的原始碼示例Python原始碼
- 短視訊程式原始碼,PageSlider實現滑動頁面原始碼IDE
- 餓了麼手機版-VUE2,專案實戰程式碼Vue
- Docker 實戰(3)- 搭建 Gitlab 容器並上傳本地專案程式碼DockerGitlab
- 實戰複雜低程式碼專案React從架構到拆解React架構
- TensorFlow 2.0 程式碼實戰專欄開篇
- 「實戰篇」開源專案docker化運維部署-原始碼介紹(二)Docker運維原始碼
- Jenkins部署Python專案實戰JenkinsPython
- Python專案實戰例項Python
- PHP檔案分享系統原始碼PHP原始碼
- Python程式碼混淆工具,Python原始碼保密、加密、混淆Python原始碼加密
- 例項程式碼分享Python實現Linux監控PythonLinux
- 用python實現植物大戰殭屍(遊戲截圖+動態演示+原始碼分享)Python遊戲原始碼