Pygame: How do I make my image fall?

自古美人都是妖i 提交于 2019-12-23 23:26:06

问题


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.

This is the object that I want to fall

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()

回答1:


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. increase the speed
  2. add the speed to the position (self.pos_y)
  3. assign the self.pos_y to self.rect.y.

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()


来源:https://stackoverflow.com/questions/46657585/pygame-how-do-i-make-my-image-fall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!