如何使我的形象下降

如何使我的形象下降

本文介绍了Pygame:如何使我的形象下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使物体掉落,就像它们掉到地上一样。我已经让它们在想要它们的地方迷住了,但我似乎无法为它们设置动画。

I am trying to make objects fall like they would on earth. I already got them to blit where I wanted them to but I can't seem to animate them.

这是我想要掉落的物体

import pygame

class circle():

    def __init__(self, screen):
        planet_color = (255,0,0)
        planet_radius = 20
        self.screen = screen
        ev = pygame.event.get()

        self.image = pygame.image.load('../images/jupiter.bmp')
        self.image = pygame.transform.scale(self.image, (80, 80))


    def blitme(self):
        self.x = pygame.mouse.get_pos()
        self.rect = self.image.get_rect()
        self.rect.center = self.x
        self.screen.blit(self.image, self.rect)

这是运行它的代码。单击鼠标时,会在单击鼠标的位置生成一张木星的小图片。

And this is the code that runs it. When the mouse is clicked a little picture of Jupiter is made where the mouse was clicked. How do I get this image to fall?

import pygame
import gravfunc as gf
from gravfunc import circle
import sys


def run_game():
    screen_height = 670
    screen_width = 1270
    pygame.init()
    screen = pygame.display.set_mode((screen_width, screen_height))
    screen.fill((10,10,30))
    running = True

    circ = circle(screen)


    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                circ.blitme()


        pygame.display.flip()


run_game()


推荐答案

给班级一个自己。 speed_y 属性,并在每帧中添加 GRAVITY 以加速对象。我还添加了 self.pos_y 属性,因为 pygame.Rect 不能将浮点数作为它们的坐标。因此,

Give your class a self.speed_y attribute and add the GRAVITY to it each frame to accelerate the object. I've also added a self.pos_y attribute because pygame.Rects can't have floating point numbers as their coordinates. So,


  1. 提高速度

  2. 将速度添加到位置( self.pos_y

  3. self.pos_y 分配给 self。 rect.y

  1. increase the speed
  2. add the speed to the position (self.pos_y)
  3. assign the self.pos_y to self.rect.y.

由于您已经在使用课程,因此我建议将其设为pygame sprite子类(继承自 pygame.sprite.Sprite )。然后,您可以将所有圈子添加到 pygame.sprite.Group 并通过调用 sprite_group.update()和 sprite_grop.draw(screen)

Since you are already using a class, I recommend to make it a pygame sprite subclass (inherit from pygame.sprite.Sprite). Then you can add all circles to a pygame.sprite.Group and update and draw them by calling sprite_group.update() and sprite_grop.draw(screen).

import pygame


GRAVITY = .2  # Pretty low gravity.

class Circle(pygame.sprite.Sprite):

    def __init__(self, pos, screen):
        super().__init__()
        self.screen = screen
        self.image = pygame.Surface((80, 80), pygame.SRCALPHA)
        pygame.draw.circle(self.image, (30, 90, 150), (40, 40), 40)
        self.rect = self.image.get_rect(center=pos)
        self.pos_y = pos[1]
        self.speed_y = 0

    def update(self):
        self.speed_y += GRAVITY
        self.pos_y += self.speed_y
        self.rect.y = self.pos_y

        if self.pos_y > self.screen.get_height():
            self.kill()  # Remove off-screen circles.


def run_game():
    pygame.init()
    screen = pygame.display.set_mode((1270, 670))
    clock = pygame.time.Clock()
    running = True
    circles = pygame.sprite.Group(Circle((600, 0), screen))

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                circles.add(Circle(event.pos, screen))

        circles.update()

        screen.fill((10, 10, 30))
        circles.draw(screen)

        pygame.display.flip()
        clock.tick(60)


run_game()
pygame.quit()

这篇关于Pygame:如何使我的形象下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:49