欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2023年01月04日 15:42:45   作者:Leleprogrammer  
這篇文章主要為大家詳細介紹了Python如何利用物理引擎Pymunk編寫一個解壓小游戲,文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下

用鼠標創(chuàng)建小球,一個蹦來蹦去的解壓小游戲…… 

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

pip install pygame
pip install pymunk

首先,導入

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

結合pygame,創(chuàng)建若干障礙,并設置重力、彈跳力等參數(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)

寫一個自動創(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()

啟動

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)在,啟動程序,移動你的鼠標,點擊鼠標創(chuàng)建一個個不同大小的球吧!

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

到此這篇關于Python利用物理引擎Pymunk編寫一個解壓小游戲的文章就介紹到這了,更多相關Python Pymunk解壓游戲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 基于Python實現(xiàn)下載網(wǎng)易音樂代碼實例

    基于Python實現(xiàn)下載網(wǎng)易音樂代碼實例

    這篇文章主要介紹了基于Python實現(xiàn)下載網(wǎng)易音樂代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Python 獲取中文字拼音首個字母的方法

    Python 獲取中文字拼音首個字母的方法

    今天小編就為大家分享一篇Python 獲取中文字拼音首個字母的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python實現(xiàn)拼圖小游戲

    python實現(xiàn)拼圖小游戲

    這篇文章主要為大家詳細介紹了python實現(xiàn)拼圖小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 利用Matlab提取圖片曲線

    利用Matlab提取圖片曲線

    一張圖片,怎樣才可以提取里面曲線的數(shù)據(jù),從而協(xié)助我們完成其他需要的數(shù)據(jù)呢?本文通過示例來進行說明,文中大量的代碼以及圖片都可以幫助小伙伴們了解
    2021-08-08
  • 詳解Django+uwsgi+Nginx上線最佳實戰(zhàn)

    詳解Django+uwsgi+Nginx上線最佳實戰(zhàn)

    這篇文章主要介紹了Django+uwsgi+Nginx上線最佳實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • python字符串Intern機制詳解

    python字符串Intern機制詳解

    這篇文章主要介紹了python字符串Intern機制詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Python實現(xiàn)采用進度條實時顯示處理進度的方法

    Python實現(xiàn)采用進度條實時顯示處理進度的方法

    這篇文章主要介紹了Python實現(xiàn)采用進度條實時顯示處理進度的方法,涉及Python數(shù)學運算結合時間函數(shù)顯示進度效果的相關操作技巧,需要的朋友可以參考下
    2017-12-12
  • 詳談Pandas中iloc和loc以及ix的區(qū)別

    詳談Pandas中iloc和loc以及ix的區(qū)別

    今天小編就為大家分享一篇詳談Pandas中iloc和loc以及ix的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python scp 批量同步文件的實現(xiàn)方法

    python scp 批量同步文件的實現(xiàn)方法

    今天小編就為大家分享一篇python scp 批量同步文件的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • flask循環(huán)導入的問題解決

    flask循環(huán)導入的問題解決

    循環(huán)導入是指兩個文件相互導入對,本文主要介紹了flask循環(huán)導入的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04

最新評論