0

我是 pygame 的新手,我正在制作一个使用鸟作为角色的横向滚动游戏,它会飞,但我试图让它在屏幕上上下移动,但我不知道如何。

import pygame
from pygame.locals import *
import os
import sys
import time
pygame.init()

class Fly:
    def __init__(self):
        self.last_update = time.clock()

        self.screen = pygame.display.set_mode((700, 400), 0, 32)
        #Load bird
        self.bird_state = 1
        self.bird_frames = []
        for r in xrange(1, 5):
            self.bird_frames.append(pygame.image.load('bird%s.png' % r))
        self.bg = pygame.image.load('bg.png').convert()

        self.Loop()

    def eventLoop(self):
        'Take and process input from perephirals'
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

    def Update(self):
        self.bird_state += 1
        if self.bird_state > 4:
            self.bird_state = 1

    def Draw(self):
        self.screen.blit(self.bg, [0, 0])
        self.screen.blit(self.bird_frames[self.bird_state - 1], (150, 150))

    def Loop(self):
        while 1:
            self.eventLoop()
            if time.clock() - self.last_update > 0.15:
                self.Update()
                self.last_update = time.clock()
            self.Draw()
            pygame.display.update()

Fly()
4

2 回答 2

2
import pygame
from pygame.locals import *
import os
import sys
import time
pygame.init()

class Fly:
    def __init__(self):
        self.last_update = time.clock()

        self.screen = pygame.display.set_mode((700, 400), 0, 32)
        #Load bird
        self.bird_state = 1
        self.bird_frames = []
        for r in xrange(1, 5):
            self.bird_frames.append(pygame.image.load('bird%s.png' % r))
        self.bg = pygame.image.load('bg.png').convert()

        self.Loop()

    def eventLoop(self):
        'Take and process input from perephirals'
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

    def Update(self):
        self.bird_state += 1
        if self.bird_state > 4:
            self.bird_state = 1

    def Draw(self):
        self.screen.blit(self.bg, [0, 0])
        self.screen.blit(self.bird_frames[self.bird_state - 1], (150, 150))

    def Loop(self):
        while 1:
            self.eventLoop()

            k = pygame.key.get_pressed()
            if k[K_DOWN]:
                # Do something with your bird
            if k[K_UP]:
                # Do something with your bird

            if time.clock() - self.last_update > 0.15:
                self.Update()
                self.last_update = time.clock()
            self.Draw()
            pygame.display.update()

Fly()

pygame.key.get_pressed检查您是否按下了按钮。查看参考资料,了解您有哪些选项。http://www.pygame.org/docs/ref/key.html

把它放在你的循环中,因为它是“实时的”。

 k = pygame.key.get_pressed()
 if k[K_DOWN]:
    # Do somthing with your bird
  if k[K_UP]:
    # Do somthing with your bird
于 2013-04-23T22:45:58.213 回答
0

如果您将 Player 视为一个矩形(这可能适用于您游戏中的所有精灵),那么您所要做的就是为您的矩形设置两个参数,称为 self.speedx 和 self.speedy。由于您只寻找上下移动,因此 speedx 不会改变,但您会根据按下的按钮快速调整。这是我的一个游戏中的一段代码。它是我的播放器类的更新定义的一部分。不幸的是,在这个游戏中我只想要横向(左右)移动,但你会明白的。如果您需要更多帮助,请告诉我。这是代码:

    def update(self):
    # Ensures default motion is 0
    self.speedx = 0
    self.speedy = 0
    # This checks to see what key's are pressed
    keystate = pygame.key.get_pressed()

    # This gives control for movement (Can use WASD or arrow keys)
    if keystate[pygame.K_a] or keystate[pygame.K_LEFT]:
        self.speedx = -5
    if keystate[pygame.K_d] or keystate[pygame.K_RIGHT]:
        self.speedx = 5
    if keystate[pygame.K_SPACE]:
        self.shoot(bullet_img)

    # Gives movement based on keys that are pressed
    self.rect.x += self.speedx
    self.rect.y += self.speedy
于 2018-02-13T17:30:06.380 回答