如何在 Pygame 中移动 Sprite [英] How to move Sprite in Pygame

查看:57
本文介绍了如何在 Pygame 中移动 Sprite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的图像(鸟)在屏幕上上下移动,但我无法弄清楚如何在此处执行此操作,这是我尝试过的方法,我确定它的出路,但我试图弄清楚是否有人可以提供帮助那太好了!

im trying to get my image (bird) to move up and down on the screen but i cant figure out how to do it here is what i tried im sure its way off but im trying to figure it out if anyone can help that would be great!

import pygame
import os

screen = pygame.display.set_mode((640, 400))

running = 1

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0
    screen.fill([255, 255, 255])
    clock = pygame.time.Clock()
    clock.tick(0.5)
    pygame.display.flip()

    bird = pygame.image.load(os.path.join('C:\Python27', 'player.png'))
    screen.blit( bird, ( 0, 0 ) )

    pygame.display.update()

class game(object):
    def move(self, x, y):
        self.player.center[0] += x
        self.player.center[1] += y


    if event.key == K_UP:
        player.move(0,5)
    if event.key == K_DOWN:
        player.move(0,-5)

game()

我试图让它在按下向下按钮时向下移动,并在按下向上键时向上移动

im trying to get it to move down on the down button press and up on the UP key press

推荐答案

正如 ecline6 所说,鸟是你目前最不担心的.

As stated by ecline6, bird is the least of your worries at this point.

考虑阅读这本书..

现在,首先让我们清理您的代码...

For now, First let's clean up your code...

import pygame
import os

# let's address the class a little later..

pygame.init()
screen = pygame.display.set_mode((640, 400))
# you only need to call the following once,so pull them out of the  while loop.
bird = pygame.image.load(os.path.join('C:\Python27', 'player.png'))
clock = pygame.time.Clock()

running = True
while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = False

    screen.fill((255, 255, 255)) # fill the screen
    screen.blit(bird, (0, 0))    # then blit the bird

    pygame.display.update() # Just do one thing, update/flip.

    clock.tick(40) # This call will regulate your FPS (to be 40 or less)

现在你的鸟"没有移动的原因是:
当您对图像进行 blit 时,即: screen.blit(bird, (0, 0)),
(0,0) 是常数,所以它不会移动.

Now the reason that your "bird" is not moving is:
When you blit the image, ie: screen.blit(bird, (0, 0)),
The (0,0) is constant, so it won't move.

这是最终代码,带有您想要的输出(尝试一下)并阅读评论:

Here's the final code, with the output you want (try it) and read the comments:

import pygame
import os

# it is better to have an extra variable, than an extremely long line.
img_path = os.path.join('C:\Python27', 'player.png')

class Bird(object):  # represents the bird, not the game
    def __init__(self):
        """ The constructor of the class """
        self.image = pygame.image.load(img_path)
        # the bird's position
        self.x = 0
        self.y = 0

    def handle_keys(self):
        """ Handles Keys """
        key = pygame.key.get_pressed()
        dist = 1 # distance moved in 1 frame, try changing it to 5
        if key[pygame.K_DOWN]: # down key
            self.y += dist # move down
        elif key[pygame.K_UP]: # up key
            self.y -= dist # move up
        if key[pygame.K_RIGHT]: # right key
            self.x += dist # move right
        elif key[pygame.K_LEFT]: # left key
            self.x -= dist # move left

    def draw(self, surface):
        """ Draw on surface """
        # blit yourself at your current position
        surface.blit(self.image, (self.x, self.y))


pygame.init()
screen = pygame.display.set_mode((640, 400))

bird = Bird() # create an instance
clock = pygame.time.Clock()

running = True
while running:
    # handle every event since the last frame.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit() # quit the screen
            running = False

    bird.handle_keys() # handle the keys

    screen.fill((255,255,255)) # fill the screen with white
    bird.draw(screen) # draw the bird to the screen
    pygame.display.update() # update the screen

    clock.tick(40)

这篇关于如何在 Pygame 中移动 Sprite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆