当前位置: 首页 > 工具软件 > pyGlet > 使用案例 >

Pyglet + Pymunk 游戏制作常用代码

邵兴怀
2023-12-01
import pyglet
from pyglet import *
from pyglet.window import key

"""
Pymunk区域开始 
"""
import pymunk               # Import pymunk..
import math

space = pymunk.Space()      # Create a Space which contain the simulation
space.gravity = 0,-100     # Set its gravity

#创建矩形的Pymunk body和shape
mass = 1
moment = pymunk.moment_for_box(mass, (100,60))
body = pymunk.Body(mass, moment)  # Create a Body with mass and moment
body.position = 50,500      # Set the position of the body
poly = pymunk.Poly.create_box(body, size=(100,60)) # Create a box shape and attach to body
space.add(body, poly)       # Add both body and shape to the simulation

#创建圆的Pymunk body和shape
radius = 20
mass = 1000
moment = pymunk.moment_for_circle(mass, 0, radius)
cbody = pymunk.Body(mass, moment)
cbody.position = 300,450
c = pymunk.Circle(cbody, radius)
space.add(cbody, c)

#创建文本框的Pymunk body和shape
twidth = 200
theight = 100
mass = 20
moment = pymunk.moment_for_box(mass, (twidth,theight))
tbody = pymunk.Body(mass, moment)  # Create a Body with mass and moment
tbody.position = 250,500      # Set the position of the body
t = pymunk.Poly.create_box(tbody, size=(twidth, theight)) # Create a box shape and attach to body
space.add(tbody,t)       # Add both body and shape to the simulation

#print_options = pymunk.SpaceDebugDrawOptions() # For easy printing


#创建静止线段1
static_body = pymunk.Body(body_type=pymunk.Body.STATIC)
static_shape = pymunk.Segment(static_body, [0, 360], [300, 80], 10)
static_shape.friction = 0.4
space.add(static_body, static_shape)

#创建静止线段2
static_body2 = pymunk.Body(body_type=pymunk.Body.STATIC)
static_shape2 = pymunk.Segment(static_body2, [300, 80], [500, 50], 10)
static_shape2.friction = 0.8
space.add(static_body2, static_shape2)

#创建静止线段3
static_body3 = pymunk.Body(body_type=pymunk.Body.STATIC)
static_shape3 = pymunk.Segment(static_body3, [500, 50], [600, 200], 10)
static_shape3.friction = 1 
space.add(static_body3, static_shape3)

"""
Pymunk区域结束
"""

win = window.Window(600,600,"这是一个游戏界面")#,fullscreen=True)
batch = graphics.Batch()

#win2 = window.Window(600,400,"Second window", visible=False)
label = text.Label("Hello", 
        font_size=40,
        x=win.width//2,y=win.height//2,
        font_name='Courier')

label2 = text.Label("World", 
        font_size=40,
        x=win.width//2,y=win.height//2,
        font_name='Courier')
label2.anchor_x = "center"
label2.anchor_y =  "center"



img = image.load("test.pyglet.png")
img.get_region(0,0,100,100)

snd = media.load('test.pyglet.mp3')

#video = media.load('test.pyglet.mp4')
player = media.Player()
#player.queue(video)
player.queue(snd)
playing = False

circle = shapes.Circle(x=100, y=100, radius=20, color=(50, 225, 30))
square = shapes.Rectangle(x=200, y=200, width=100, height=60,color=(55, 55, 255))
square.anchor_x = square.width//2
square.anchor_y = square.height//2

def update(delta_time):
    '''
    让屏幕元素时时与Pymunk对应的body, shape一致
    '''
    print("圆的位置:", c.body.position, c.body.angle)
    print("矩形的位置:", poly.body.position, poly.body.angle)
    circle.x = c.body.position.x
    circle.y = c.body.position.y
    circle.rotation = -180 * c.body.angle / math.pi
    square.x = poly.body.position.x
    square.y = poly.body.position.y
    square.rotation = -180 * poly.body.angle / math.pi
    label2.x = t.body.position.x
    label2.y = t.body.position.y
    label2.rotation = -180 * t.body.angle / math.pi

    space.step(delta_time)        # Step the simulation one step forward
    #space.debug_draw(print_options) # Print the state of the simulation

#使用时钟
clock.schedule(update)

@win.event
def on_draw():
    win.clear()
    img.blit(0,0)
    if playing:
        player.get_texture().blit(50,50)
    label.draw()
    label2.draw()
    circle.draw()
    square.draw()

    #画下面静止的三条线段
    pv1 = static_body.position + static_shape.a.rotated(static_body.angle)
    pv2 = static_body.position + static_shape.b.rotated(static_body.angle)
    line = shapes.Line(pv1.x,pv1.y,pv2.x,pv2.y, width=10, batch=batch)
    pv3 = static_body2.position + static_shape2.a.rotated(static_body2.angle)
    pv4 = static_body2.position + static_shape2.b.rotated(static_body2.angle)
    line2 = shapes.Line(pv3.x,pv3.y,pv4.x,pv4.y, width=10, batch=batch)
    pv5 = static_body3.position + static_shape3.a.rotated(static_body3.angle)
    pv6 = static_body3.position + static_shape3.b.rotated(static_body3.angle)
    line3 = shapes.Line(pv5.x,pv5.y,pv6.x,pv6.y, width=10, batch=batch)
    batch.draw()


#各种事件处理

@win.event
def on_key_press(symbol, modifiers): #键盘按下
    print("按下", symbol, modifiers)

    if symbol == key.W:
        player.play()
        #win2.set_visible()
        playing = True
        print('前进')

    elif symbol == key.S:
      print('后退')

    elif symbol == key.A:
      print('左移')

    elif symbol == key.D:
      print('右移')

    elif symbol == key.ESCAPE:
        print("Byebye")

    if modifiers & key.MOD_CTRL:
      print('over')

@win.event
def on_key_release(symbol, modifiers):#键盘释放
    print("松开", symbol, modifiers)

@win.event
def on_mouse_motion(x,y,dx,dy):#鼠标移动
    print('移动',x,y)

@win.event
def on_mouse_press(x, y, button, modifiers): #鼠标按住
    cursor = win.get_system_mouse_cursor(win.CURSOR_HELP)
    win.set_mouse_cursor(cursor)
    print('按下鼠标',x,y,button,modifiers)

@win.event
def on_mouse_release(x, y, button, modifiers):#鼠标释放
    cursor = win.get_system_mouse_cursor(win.CURSOR_DEFAULT)
    win.set_mouse_cursor(cursor)
    print('松开鼠标')

@win.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):#鼠标拖动
    print('拖动鼠标')

@win.event
def on_mouse_enter(x, y): #鼠标进入窗口
    print('进入窗口')

@win.event
def on_mouse_leave(x, y): #鼠标离开窗口
    print('离开窗口')

@win.event
def on_mouse_scroll(x, y, scroll_x, scroll_y):#鼠标滚轮
    print('鼠标滚轮')

@win.event
def on_text(text): #输入文本事件
    print('输入文本', text)

@win.event
def on_text_motion(motion):
    print("输入方向键", motion)

@win.event
def on_resize(width, height):
    print("窗口大小改变", width, height)

app.run()

 类似资料: