Python利用物理引擎Pymunk編寫一個解壓小遊戲

用鼠標創建小球,一個蹦來蹦去的解壓小遊戲…… 

本次需要的外置包:pygame,pymunk,cmd運行該命令安裝:

pip install pygame
pip install pymunk

首先,導入

import pymunk
import pygame
from pygame.locals import *
import sys
import random as rd

結合pygame,創建若幹障礙,並設置重力、彈跳力等參數 

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()

最終代碼

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()

現在,啟動程序,移動你的鼠標,點擊鼠標創建一個個不同大小的球吧!

(p.s. 滑動滾輪也可以喲~~~)

到此這篇關於Python利用物理引擎Pymunk編寫一個解壓小遊戲的文章就介紹到這瞭,更多相關Python Pymunk解壓遊戲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: