視頻
pygame——游戲視頻
前言
簡單的來寫一個(gè)pygame小游戲,我的畫面比較卡哇伊各位可以自己換圖片哈。
一、作品簡介
就是一個(gè)最基本的pygame小游戲,可以控制人物,攻擊敵人,打到敵人使敵人消失,如果敵人到達(dá)邊緣仍然沒有被消滅,游戲就會(huì)失敗。
二、作品操作
1.鼠標(biāo)移動(dòng)人物跟隨移動(dòng),播放背景音樂,可以摁下 Enter 進(jìn)入游戲的主體界面,或者點(diǎn)擊 End 退出游戲
2.用鍵盤控制人物移動(dòng), w 向上移動(dòng), s 向下移動(dòng), a 向左移動(dòng), d 向右移動(dòng),當(dāng)點(diǎn)擊空格時(shí)人物發(fā)射光球,如果光球打中敵人敵人消失,如果敵人到達(dá)邊界線,游戲失敗
3.游戲失敗后彈出結(jié)束圖片,并輸出 Game Over 退出游戲.
一、pygame是什么
pygame是跨平臺(tái)Python模塊,專門為電子游戲設(shè)計(jì),包含圖像、聲音等;簡單的說它是別人已經(jīng)編寫好的程序,并放在了一個(gè)類似庫里,專門給別人使用的;
pygame的圖形接口簡書上這篇是我看到的最全的:(https://www.jianshu.com/p/352f28c32865)
pygame常用模塊可以看這篇:(https://cloud.tencent.com/developer/article/1661777)
二、游戲代碼+注釋
代碼如下:
import pygame
#初始化游戲
pygame.init()
#創(chuàng)建游戲窗口
window=pygame.display.set_mode((800,500))
#設(shè)置游戲上方標(biāo)題
pygame.display.set_caption('瀟瀟的游戲')
#設(shè)置游戲背景顏色
window.fill((255,255,255))
#游戲初始頁面圖片===========
#1.創(chuàng)建圖片
image1=pygame.image.load('files/皮卡丘.jpg')
#渲染圖片
window.blit (image1,(0,0))
#刷新(1) update(一次后)
pygame.display.flip()
#按鈕===================
font1=pygame.font.Font('files/bb.ttf',29)
pygame.draw.rect(window,(0,255,0),(600,300,100,40)) #確定
text1 = font1.render('Enter', True, (255,255,255))
window.blit(text1, (610,305))
pygame.draw.rect(window,(255,0,0),(600,400,100,40))#取消
text2 = font1.render('End', True, (255,255,255))
window.blit(text2,(620,405))
pygame.display.flip()
#顯示文字================
# 1.創(chuàng)建字體
font=pygame.font.Font('files/文字包.ttf',60)
#2.創(chuàng)建文字render('文字', True,文字顏色,背景顏色)
text = font.render('皮卡丘 GO!', True, (0, 0, 0))
#渲染
window.blit(text, (450,110))
pygame.display.flip()
#動(dòng)畫效果================
#y=100
#pygame.draw.circle(window,(255,0,0),(100,y),50)
#pygame.display.update()
#while True:
# y=y+10
# pygame.draw.circle(window,(255,0,0),(100,y),50)
# pygame.display.update()
#運(yùn)行狀態(tài)loop
while True:
#=======游戲動(dòng)態(tài)刷新=========
#檢測事件
for event in pygame.event.get():
if event.type == pygame.QUIT:#如果點(diǎn)擊×
exit()#退出
if event.type == pygame.MOUSEBUTTONDOWN:
#1鼠標(biāo)事件===============
print("鼠標(biāo)摁下",event.pos)
mx, my = event.pos
if 600<=mx<=700 and 300<=my<=340:
pygame.draw.rect(window,(0,0,255),(600,300,100,40))
window.blit(text1,(610,305))
pygame.display.flip()
if event.type == pygame.MOUSEBUTTONDOWN:
print("鼠標(biāo)抬起")
pygame.draw.rect(window, (0, 255, 0), (600, 300, 100, 40)) # 確定
text1 = font1.render('Enter', True, (255, 255, 255))
window.blit(text1, (610, 305))
pygame.display.flip()
#事件開始===================================
PKQ_up=pygame.image.load()
if 600<=mx<=700 and 400<=my<=440:
pygame.draw.rect(window, (200, 200, 200), (600, 400, 100, 40))
window.blit(text2, (610, 405))
pygame.display.flip()
if event.type == pygame.MOUSEBUTTONDOWN:
print("鼠標(biāo)抬起")
pygame.draw.rect(window, (255, 0, 0), (600, 400, 100, 40)) # 確定
text1 = font1.render('End', True, (255, 255, 255))
window.blit(text1, (620, 405))
pygame.display.flip()
print("End")
exit()
if event.type == pygame.MOUSEMOTION:
print("鼠標(biāo)移動(dòng)")
mx, my = event.pos
# 1.創(chuàng)建圖片
image1 = pygame.image.load('files/6.png')
# 渲染圖片
window.blit(image1, (mx-40, my-40))
# 刷新(1) update(一次后)
pygame.display.flip()
#2鍵盤事件================
if event.type == pygame.KEYDOWN:
三、效果展示
四、代碼知識(shí)簡單描述
文章來源:http://www.zghlxwxcb.cn/news/detail-510547.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-510547.html
到了這里,關(guān)于pygame自制小游戲的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!