猿问

pygame中的落沙,每次按下鼠标按钮时都会创建和掉落粒子

我试图制作一个代码,每次点击鼠标,都会绘制一个圆圈并将其放到底部。我对此有疑问,因为我想不出一个新粒子在下落时如何绘制。我不知道如何编码,因此我至少需要知道如何做到这一点。这就是我所要做的,但我很困惑下一步该做什么。


import pygame, time


class Material(object):


    def __init__(self, name, radius, gravity):

        self.name = name

        self.radius = radius

        self.gravity = gravity



sand = Material("Sand", 5, 2)


def game():

    pygame.init()

    win = pygame.display.set_mode((1000, 650))

    pygame.display.set_caption("***Sandbox***")

    event = pygame.event.wait()

    velocity = 0

    while True:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                pygame.quit() 

            if pygame.mouse.get_pressed()[0]:

                x, y = event.pos

                print(event.pos)

                pygame.draw.circle(win, (194, 178, 128), (x, y), sand.radius)

                pygame.display.update()

                while y >= 0 and y < 645:

                    win.fill((0,0,0,))

                    y += velocity

                    velocity += sand.gravity

                    print(x, y)

                    if y > 645:

                        y = 645

                        velocity = 0

                    pygame.draw.circle(win, (194, 178, 128), (x, y), sand.radius)

                    pygame.display.update()



game()


炎炎设计
浏览 150回答 1
1回答

白衣非少年

在这种情况下,面向对象编程是你的朋友。你已经创建了一个类Material,但你并没有充分利用它,因为你从不创建多个类的实例。首先,让你的类存储落下的沙子的坐标,我想说这是本例中最重要的属性,因为每个沙子都有自己的坐标。class&nbsp;Material(object): &nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;name,&nbsp;pos,&nbsp;radius,&nbsp;gravity): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name&nbsp;=&nbsp;name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.x&nbsp;=&nbsp;pos[0] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.y&nbsp;=&nbsp;pos[1] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.radius&nbsp;=&nbsp;radius &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.gravity&nbsp;=&nbsp;gravity然后在您的代码中创建一个列表(开头为空),您将在其中存储Material在程序执行期间创建的所有实例。您的game函数需要多次编辑:首先,使用事件pygame.MOUSEBUTTONUP,而不是pygame.mouse.get_pressed()。由于您处于事件循环中,因此最好使用 pygame 事件系统。绘制材料的逻辑应该在事件循环之外。事件循环应该只创建一个Material实例并将其添加到列表中。在主循环中移动绘图逻辑。不要用循环将每个粒子一直移动到屏幕底部,而是在粒子列表上循环以将每个粒子向底部移动一步(即添加gravity到y坐标)。pygame.quit()关闭 pygame,但不关闭程序。用于sys.exit()无错误退出程序。添加pygame.time.Clock以减慢动画速度。所以你的最终代码应该是这样的:import sysimport pygameclass Material(object):&nbsp; &nbsp; def __init__(self, name, pos, radius, gravity):&nbsp; &nbsp; &nbsp; &nbsp; self.name = name&nbsp; &nbsp; &nbsp; &nbsp; self.x = pos[0]&nbsp; &nbsp; &nbsp; &nbsp; self.y = pos[1]&nbsp; &nbsp; &nbsp; &nbsp; self.radius = radius&nbsp; &nbsp; &nbsp; &nbsp; self.gravity = gravityallsands = []def game():&nbsp; &nbsp; pygame.init()&nbsp; &nbsp; win = pygame.display.set_mode((1000, 650))&nbsp; &nbsp; pygame.display.set_caption("***Sandbox***")&nbsp; &nbsp; clock = pygame.time.Clock()&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.exit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.MOUSEBUTTONUP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(event.pos)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allsands.append(Material("Sand", event.pos, 5, 2))&nbsp; &nbsp; &nbsp; &nbsp; win.fill((0,0,0,))&nbsp; &nbsp; &nbsp; &nbsp; for sand in allsands:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.draw.circle(win, (194, 178, 128), (sand.x, sand.y), sand.radius)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if sand.y >=0 and sand.y < 645:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sand.y += sand.gravity&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp; clock.tick(50)game()这是结果:原则上可以进行进一步的编辑以使您Material成为真正的精灵并使用精灵组而不是列表来存储它们,但我试图不完全重写您的代码。
随时随地看视频慕课网APP

相关分类

Python
我要回答