怎么使用Python的Pymunk实现用鼠标创建小球

发布时间:2023-01-05 09:28:38 作者:iii
来源:亿速云 阅读:120

这篇文章主要介绍“怎么使用Python的Pymunk实现用鼠标创建小球”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么使用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()

关于“怎么使用Python的Pymunk实现用鼠标创建小球”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. 怎样用常规密码学解加密python脚本
  2. python如何使用choice()函数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Jupyter Notebook读取csv文件出现问题如何解决

下一篇:Hooks怎么封装与使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》