python - Pygame 鼠标移动失败

标签 python pygame game-physics

我正在学习pygame教程,目前我正在尝试弄清楚如何选择一个正在发射球的圆圈,其中一个球击中另一个球,进而击倒一个盒子。当球击中盒子时,敲击盒子的效果很好。但是,当我添加鼠标移动以便我可以再次选择球并将其放置在相同位置以便可以再次击中它时,盒子会再次敲击。球只是向后滚动,而不会发射第二个球来敲击盒子。这是之前的代码,适用于一个球发射另一个球,但没有鼠标移动,即不允许选择和拖动球。

import pygame
from pygame.locals import *
from pygame.color import *
import pymunk as pm
from pymunk import Vec2d
import sys
from random import randint

def to_pygame(p):
    """Small hack to convert pymunk to pygame coordinates"""
    return int(p[0]), int(-p[1]+600)


def draw_ball(screen, ball, colour):
    r = ball.radius
    rot = ball.body.rotation_vector
    p = to_pygame(ball.body.position)
    p2 = Vec2d(rot.x, -rot.y) * r * 0.9
    pygame.draw.line(screen, THECOLORS["red"], p, p+p2)
    pygame.draw.circle(screen, colour, p, int(r), 3)


def add_ball(space, x=0, y=130):
    mass = 1.3
    radius = 20
    inertia = pm.moment_for_circle(mass, 0, radius, (0,0))
    body = pm.Body(mass, inertia)
    body.position = (x,y)
    shape = pm.Circle(body, radius, (0,0))
    shape.friction = 10.0
    shape.elasticity = 1.0
    space.add(body, shape)

    return shape


def add_box(space, size, pos, mass=0.3):
    points = [(-size, -size), (-size, size), (size,size), (size, -size)]
    moment = pm.moment_for_poly(int(mass), points, (0,0))

    body = pm.Body(mass, moment)
    body.position = pos

    shape = pm.Poly(body, points, (0,0))
    shape.friction = 1
    space.add(body,shape)

    return shape

def draw_box(screen, box):
    ps = box.get_points()
    ps.append(ps[0])
    newps = [to_pygame(x) for x in ps]
    pygame.draw.polygon(screen, THECOLORS["blue"], newps, 3)


def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    pygame.display.set_caption("Impulsive balls")
    clock = pygame.time.Clock()

    balls = []
    space = pm.Space()
    space.gravity = (0.0, -300.0)

    # ground
    body = pm.Body()
    shape = pm.Segment(body, (0,100), (450,100), .0)
    shape.friction = 6.0
    space.add(shape)

    # hidden ramp
    body = pm.Body()
    slope = pm.Segment(body, (0,100), (180,150), .0)
    space.add(slope)

    balls.append(add_ball(space, 10, 130))
    balls.append(add_ball(space, 100, 150))
    #joint = pm.PinJoint(balls[0].body, balls[1].body)
    #joint.distance = 90

    mass = 1.0
    size = 20
    box = add_box(space, size, (400,100+size), mass)
    count = 0

    while 1:
        space.step(1/30.0)
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)

        if count == 10:
            pm.Body.apply_impulse(balls[0].body, (450,0))

        screen.fill(THECOLORS["white"])
        pygame.draw.line(screen, THECOLORS["red"], to_pygame((0,100)), to_pygame((450,100)), 3)

        pygame.draw.line(screen, THECOLORS["red"], to_pygame((0,100)), to_pygame((180,150)), 3)

        draw_box(screen, box)
        for ball in balls:
            draw_ball(screen, ball, THECOLORS["green"])

        pygame.display.flip()
        count += 1

if __name__ == '__main__':
    main()

这是我添加鼠标移动代码的第二个版本

import pygame
from pygame.locals import *
from pygame.color import *
import pymunk as pm
from pymunk import Vec2d
import sys
from random import randint

def to_pygame(p):
    """Small hack to convert pymunk to pygame coordinates"""
    return int(p[0]), int(-p[1]+600)
def from_pygame(p):
    return to_pygame(p)

def draw_ball(screen, ball, colour):
    r = ball.radius
    rot = ball.body.rotation_vector
    p = to_pygame(ball.body.position)
    p2 = Vec2d(rot.x, -rot.y) * r * 0.9
    pygame.draw.line(screen, THECOLORS["blue"], p, p+p2)
    pygame.draw.circle(screen, colour, p, int(r), 3)


def add_ball(space, x=0, y=130):
    mass = 1.3 #1.5
    radius = 20
    inertia = pm.moment_for_circle(mass, 0, radius, (0,0))
    body = pm.Body(mass, inertia)
    body.position = (x,y)
    shape = pm.Circle(body, radius, (0,0))
    shape.friction = 10.0
    shape.elasticity = 1.0
    space.add(body, shape)

    return shape


def add_box(space, size, pos, mass=0.3):
    points = [(-size, -size), (-size, size), (size,size), (size, -size)]
    moment = pm.moment_for_poly(int(mass), points, (0,0))

    body = pm.Body(mass, moment)
    body.position = pos

    shape = pm.Poly(body, points, (0,0))
    shape.friction = 1
    space.add(body,shape)

    return shape

def draw_box(screen, box):
    ps = box.get_points()
    ps.append(ps[0])
    newps = [to_pygame(x) for x in ps]
    pygame.draw.polygon(screen, THECOLORS["blue"], newps, 3)


def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    pygame.display.set_caption("Impulsive balls")
    clock = pygame.time.Clock()
    body = []
    selected = None
    balls = []
    space = pm.Space()
    space.gravity = (0.0, -300.0)

    # ground
    body = pm.Body()
    shape = pm.Segment(body, (0,100), (450,100), .0)
    shape.friction = 6.0
    space.add(shape)

    # hidden ramp
    body = pm.Body()
    slope = pm.Segment(body, (0,100), (180,150), .0)
    space.add(slope)

    balls.append(add_ball(space, 10, 130))
    balls.append(add_ball(space, 100, 150))
    #joint = pm.PinJoint(balls[0].body, balls[1].body)
    #joint.distance = 90

    mass = 1.0
    size = 20
    box = add_box(space, size, (400,100+size), mass)
    count = 0

    while 1:
        space.step(1/30.0)
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)
                if count == 10:
                    pm.Body.apply_impulse(balls[0].body, (450,0))
                if event.key == K_p:
                    balls[0].body.apply_impulse((450,0))
                if event.key == K_s:
                    balls[0].body.apply_impulse((-450,0))



            elif event.type == MOUSEBUTTONDOWN:
                p = from_pygame(Vec2d(event.pos))
                selected = space.point_query_first(p)
            elif event.type == MOUSEBUTTONUP:
                if selected != None:
                    selected = None
            elif event.type == MOUSEMOTION:
                if selected != None:
                    selected.body.position = from_pygame(event.pos)

        screen.fill(THECOLORS["white"])
        pygame.draw.line(screen, THECOLORS["red"], to_pygame((0,100)), to_pygame((450,100)), 3)

        pygame.draw.line(screen, THECOLORS["red"], to_pygame((0,100)), to_pygame((180,150)), 3)

        draw_box(screen, box)
        for ball in balls:
            draw_ball(screen, ball, THECOLORS["green"])

        pygame.display.flip()
        count += 1

if __name__ == '__main__':
    main()

另外,我怎样才能让球处于同一位置,以便我可以拖动它并插入另一个球来敲击盒子,而不是让球滚回来,以便稍后我可以再次选择发射的球并将其放置紧邻球而不回滚

最佳答案

对于您的第一个问题,球在第二个文件中没有发射。问题是您已将该代码放入事件 block 中,只有在触发外部事件(例如按下某个键)时才会发生这种情况。要修复此问题,需要将该 block 移出 for 循环,如下所示:

...

while 1:
    space.step(1/30.0)
    clock.tick(30)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit(0)
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit(0)
            if event.key == K_p:
                balls[0].body.apply_impulse((450,0))
            if event.key == K_s:
                balls[0].body.apply_impulse((-450,0))
        elif event.type == MOUSEBUTTONDOWN:
            p = from_pygame(Vec2d(event.pos))
            selected = space.point_query_first(p)
        elif event.type == MOUSEBUTTONUP:
            if selected != None:
                selected = None
        elif event.type == MOUSEMOTION:
            if selected != None:
                selected.body.position = from_pygame(event.pos)

    if count == 10:
        pm.Body.apply_impulse(balls[0].body, (450,0))

...

为了防止球移动,我建议将它们放在平坦的地面上。我对 main 进行了以下更改以表明我的意思。请注意,我禁用了球发射,以便您可以看到球保持在原位。我还建议您在屏幕外放置一些隐形墙,以将所有对象固定在框架内。

...

def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    pygame.display.set_caption("Impulsive balls")
    clock = pygame.time.Clock()
    body = []
    selected = None
    balls = []
    space = pm.Space()
    space.gravity = (0.0, -300.0)

    # ground
    body = pm.Body()
    shape = pm.Segment(body, (0,100), (450,100), .0)
    shape.friction = 6.0
    space.add(shape)

    # hidden ramp
    body = pm.Body()
    slope = pm.Segment(body, (20,100), (180,150), .0)
    space.add(slope)
    body = pm.Body()
    slopetop = pm.Segment(body, (180,150), (190,150), .0)
    space.add(slopetop)

    balls.append(add_ball(space, 10, 130))
    balls.append(add_ball(space, 185, 170))
    #joint = pm.PinJoint(balls[0].body, balls[1].body)
    #joint.distance = 90

    mass = 1.0
    size = 20
    box = add_box(space, size, (400,100+size), mass)
    count = 0

    while 1:
        space.step(1/30.0)
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)
                if event.key == K_p:
                    balls[0].body.apply_impulse((450,0))
                if event.key == K_s:
                    balls[0].body.apply_impulse((-450,0))
            elif event.type == MOUSEBUTTONDOWN:
                p = from_pygame(Vec2d(event.pos))
                selected = space.point_query_first(p)
            elif event.type == MOUSEBUTTONUP:
                if selected != None:
                    selected = None
            elif event.type == MOUSEMOTION:
                if selected != None:
                    selected.body.position = from_pygame(event.pos)

        if count == 10 and 0:
            pm.Body.apply_impulse(balls[0].body, (450,0))

        screen.fill(THECOLORS["white"])
        pygame.draw.line(screen, THECOLORS["red"], to_pygame((0,100)), to_pygame((450,100)), 3)
        pygame.draw.line(screen, THECOLORS["red"], to_pygame((20,100)), to_pygame((180,150)), 3)
        pygame.draw.line(screen, THECOLORS["red"], to_pygame((180,150)), to_pygame((190,150)), 3)

        draw_box(screen, box)
        for ball in balls:
            draw_ball(screen, ball, THECOLORS["green"])

        pygame.display.flip()
        count += 1
...

关于python - Pygame 鼠标移动失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16630732/

相关文章:

python - 不可能捕获 asyncio.TimeoutError 吗?

python - 如何修复 "PT006 wrong name(s) type in @pytest.mark.parametrize, expected tuple"?

objective-c - 2d map 中的游戏方 block 移动

python - 如何使用继承,我缺少什么?

swift - 计算 SpriteKit 中的对象在受到脉冲时的轨迹

java - 无法处理简单的 Java.swing 游戏中的移动错误

python - 如何从python导出不同大小的列表到excel

python - Django:在 GenericStackedInline 上禁止 can_delete

python - 如何在 Python 中编写钟摆效果?

python - 如何让蛇超出屏幕后,它会回到屏幕的另一边