Python利用物理引擎Pymunk編寫一個(gè)解壓小游戲
用鼠標(biāo)創(chuàng)建小球,一個(gè)蹦來蹦去的解壓小游戲……

本次需要的外置包:pygame,pymunk,cmd運(yùn)行該命令安裝:
pip install pygame pip install pymunk
首先,導(dǎo)入
import pymunk import pygame from pygame.locals import * import sys import random as rd
結(jié)合pygame,創(chuàng)建若干障礙,并設(shè)置重力、彈跳力等參數(shù)
class Demo:
WIDTH=800
HEIGHT=800
def __init__(self):
pygame.init()
self.screen=pygame.display.set_mode((self.WIDTH,self.HEIGHT))
pygame.display.set_caption("BALLS!")
self.balls=[]
self.space=pymunk.Space()
self.space.gravity=(0,280)
self.elasticity=0.9
self.segment_1_pos=((0,self.HEIGHT-125),(self.WIDTH,self.HEIGHT-65))
self.segment_1_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.segment_1_shape=pymunk.Segment(self.segment_1_body,self.segment_1_pos[0],self.segment_1_pos[1],10)
self.segment_1_shape.elasticity=self.elasticity
self.space.add(self.segment_1_body,self.segment_1_shape)
self.segment_2_pos=((0,self.HEIGHT-500),(150,self.HEIGHT-400))
self.segment_2_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.segment_2_shape=pymunk.Segment(self.segment_2_body,self.segment_2_pos[0],self.segment_2_pos[1],10)
self.segment_2_shape.elasticity=self.elasticity
self.space.add(self.segment_2_body,self.segment_2_shape)
self.segment_3_pos=((self.WIDTH,self.HEIGHT-500),(self.WIDTH-150,self.HEIGHT-400))
self.segment_3_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.segment_3_shape=pymunk.Segment(self.segment_3_body,self.segment_3_pos[0],self.segment_3_pos[1],10)
self.segment_3_shape.elasticity=self.elasticity
self.space.add(self.segment_3_body,self.segment_3_shape)
self.circle_1_pos=(self.WIDTH/2,self.HEIGHT/2)
self.circle_1_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.circle_1_shape=pymunk.Circle(self.circle_1_body,30,self.circle_1_pos)
self.circle_1_shape.elasticity=self.elasticity
self.space.add(self.circle_1_body,self.circle_1_shape)寫一個(gè)自動(dòng)創(chuàng)建新球的函數(shù)
def newBall(self,x,y,r):
body=pymunk.Body(1,100,body_type=pymunk.Body.DYNAMIC)
body.position=x,y
shape=pymunk.Circle(body,r)
shape.elasticity=self.elasticity
self.space.add(body,shape)
self.balls.append((shape,r))
事件監(jiān)聽
def listen(self):
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
if event.type==MOUSEBUTTONDOWN:
self.newBall(*pygame.mouse.get_pos(),rd.randint(5,10))
繪制并檢測物體跳出邊界并刪除
def draw(self):
self.screen.fill((255,255,255))
pygame.draw.line(self.screen,(0,0,0),self.segment_1_pos[0],self.segment_1_pos[1],10)
pygame.draw.line(self.screen,(0,0,0),self.segment_2_pos[0],self.segment_2_pos[1],10)
pygame.draw.line(self.screen,(0,0,0),self.segment_3_pos[0],self.segment_3_pos[1],10)
pygame.draw.circle(self.screen,(0,0,0),self.circle_1_pos,30)
for ball,r in self.balls:
pygame.draw.circle(self.screen,(255,0,0),(ball.body.position.x,ball.body.position.y),r)
c=0
while c<len(self.balls) and len(self.balls):
x,y=self.balls[c][0].body.position
if x<0 or x>self.WIDTH or y>self.HEIGHT:
self.space.remove(self.balls[c][0])
self.balls.pop(c)
c-=1
c+=1主循環(huán)
def run(self):
while True:
self.listen()
self.draw()
self.space.step(0.001)
pygame.display.update()
啟動(dòng)
if __name__ == '__main__':
demo=Demo()
demo.run()
最終代碼
import pymunk
import pygame
from pygame.locals import *
import sys
import random as rd
class Demo:
WIDTH=800
HEIGHT=800
def __init__(self):
pygame.init()
self.screen=pygame.display.set_mode((self.WIDTH,self.HEIGHT))
pygame.display.set_caption("BALLS!")
self.balls=[]
self.space=pymunk.Space()
self.space.gravity=(0,280)
self.elasticity=0.9
self.segment_1_pos=((0,self.HEIGHT-125),(self.WIDTH,self.HEIGHT-65))
self.segment_1_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.segment_1_shape=pymunk.Segment(self.segment_1_body,self.segment_1_pos[0],self.segment_1_pos[1],10)
self.segment_1_shape.elasticity=self.elasticity
self.space.add(self.segment_1_body,self.segment_1_shape)
self.segment_2_pos=((0,self.HEIGHT-500),(150,self.HEIGHT-400))
self.segment_2_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.segment_2_shape=pymunk.Segment(self.segment_2_body,self.segment_2_pos[0],self.segment_2_pos[1],10)
self.segment_2_shape.elasticity=self.elasticity
self.space.add(self.segment_2_body,self.segment_2_shape)
self.segment_3_pos=((self.WIDTH,self.HEIGHT-500),(self.WIDTH-150,self.HEIGHT-400))
self.segment_3_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.segment_3_shape=pymunk.Segment(self.segment_3_body,self.segment_3_pos[0],self.segment_3_pos[1],10)
self.segment_3_shape.elasticity=self.elasticity
self.space.add(self.segment_3_body,self.segment_3_shape)
self.circle_1_pos=(self.WIDTH/2,self.HEIGHT/2)
self.circle_1_body=pymunk.Body(body_type=pymunk.Body.STATIC)
self.circle_1_shape=pymunk.Circle(self.circle_1_body,30,self.circle_1_pos)
self.circle_1_shape.elasticity=self.elasticity
self.space.add(self.circle_1_body,self.circle_1_shape)
def newBall(self,x,y,r):
body=pymunk.Body(1,100,body_type=pymunk.Body.DYNAMIC)
body.position=x,y
shape=pymunk.Circle(body,r)
shape.elasticity=self.elasticity
self.space.add(body,shape)
self.balls.append((shape,r))
def listen(self):
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
if event.type==MOUSEBUTTONDOWN:
self.newBall(*pygame.mouse.get_pos(),rd.randint(5,10))
def draw(self):
self.screen.fill((255,255,255))
pygame.draw.line(self.screen,(0,0,0),self.segment_1_pos[0],self.segment_1_pos[1],10)
pygame.draw.line(self.screen,(0,0,0),self.segment_2_pos[0],self.segment_2_pos[1],10)
pygame.draw.line(self.screen,(0,0,0),self.segment_3_pos[0],self.segment_3_pos[1],10)
pygame.draw.circle(self.screen,(0,0,0),self.circle_1_pos,30)
for ball,r in self.balls:
pygame.draw.circle(self.screen,(255,0,0),(ball.body.position.x,ball.body.position.y),r)
c=0
while c<len(self.balls) and len(self.balls):
x,y=self.balls[c][0].body.position
if x<0 or x>self.WIDTH or y>self.HEIGHT:
self.space.remove(self.balls[c][0])
self.balls.pop(c)
c-=1
c+=1
def run(self):
while True:
self.listen()
self.draw()
self.space.step(0.001)
pygame.display.update()
if __name__ == '__main__':
demo=Demo()
demo.run()現(xiàn)在,啟動(dòng)程序,移動(dòng)你的鼠標(biāo),點(diǎn)擊鼠標(biāo)創(chuàng)建一個(gè)個(gè)不同大小的球吧!
(p.s. 滑動(dòng)滾輪也可以喲~~~)
到此這篇關(guān)于Python利用物理引擎Pymunk編寫一個(gè)解壓小游戲的文章就介紹到這了,更多相關(guān)Python Pymunk解壓游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決pycharm運(yùn)行出錯(cuò),代碼正確結(jié)果不顯示的問題
今天小編就為大家分享一篇解決pycharm運(yùn)行出錯(cuò),代碼正確結(jié)果不顯示的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python 利用pandas和mysql-connector獲取Excel數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫
在實(shí)際應(yīng)用中,我們可能需要將Excel表格中的數(shù)據(jù)導(dǎo)入到MySQL數(shù)據(jù)庫中,以便于進(jìn)行進(jìn)一步的數(shù)據(jù)分析和處理,本文將介紹如何使用Python將Excel表格中的數(shù)據(jù)插入到MySQL數(shù)據(jù)庫中,需要的朋友可以參考下2023-10-10
Python簡單實(shí)現(xiàn)socket信息發(fā)送與監(jiān)聽功能示例
這篇文章主要介紹了Python簡單實(shí)現(xiàn)socket信息發(fā)送與監(jiān)聽功能,結(jié)合實(shí)例形式分析了Python基于socket構(gòu)建客戶端與服務(wù)器端通信相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
探索Python fcntl模塊文件鎖和文件控制的強(qiáng)大工具使用實(shí)例
這篇文章主要介紹了Python fcntl模塊文件鎖和文件控制的強(qiáng)大工具使用實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python實(shí)現(xiàn)植物大戰(zhàn)僵尸游戲?qū)嵗a
這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)植物大戰(zhàn)僵尸游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
python讀寫修改Excel之xlrd&xlwt&xlutils
這篇文章主要介紹了python讀寫修改Excel之xlrd&xlwt&xlutils,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
深入解析Python?3中Hash鍵值存儲(chǔ)的優(yōu)勢與應(yīng)用
這篇文章主要介紹了深入解析Python?3中Hash鍵值存儲(chǔ)的優(yōu)勢與應(yīng)用的相關(guān)資料,需要的朋友可以參考下2023-11-11

