Python利用物理引擎Pymunk编写一个解压小游戏
Leleprogrammer 人气:0用鼠标创建小球,一个蹦来蹦去的解压小游戏……
本次需要的外置包: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. 滑动滚轮也可以哟~~~)
加载全部内容