python-2.7 - 在pygame中让多个球移动

标签 python-2.7 pygame

我需要让这段代码中的球随机移动。我不确定我做错了什么。我是编程新手,这可能编码得很糟糕,所以任何帮助将不胜感激。现在,代码在屏幕中间创建一个可以上下移动的角色,并在屏幕左上角创建一个不移动的球。我希望屏幕上有多个球,最终我希望这些球随机移动,如果它们与角色碰撞,你就会输掉这个简单的游戏。

from pygame import *
import random

class Ball(sprite.Sprite):
    def __init__(self, numballs, balls = []):
        sprite.Sprite.__init__(self)
        self.image = image.load('ball.png')
        self.rect = self.image.get_rect()
        self.numballs = numballs
        self.balls = balls


    def multipleBalls(self):
        for count in range(self.numballs):
            self.balls.append(dict)
            self.balls[count] = {'x': 0, 'y': 0, 'xmove': random.randint(1,2), 'ymove':random.randint(1,2)}

    def ballMove(self):
        for count in range(self.numballs):
            self.balls[count]['x'] = self.balls[count]['x'] + self.balls[count]['xmove']
            self.balls[count]['y'] = self.balls[count]['y'] + self.balls[count]['ymove']

    def ballsOnScreen(self):
        for count in range(self.numballs):
            self.screen.blit(self.image, (self.balls[count]['x'], self.balls[count]['y']))


    def ballBarrier(self):
        for count in range(self.numballs):
            if self.balls[count]['x'] > 620:
                self.balls[count]['xmove'] = random.randint(-2, 0)
            if self.balls[count]['x'] < -10:
                self.balls[count]['xmove'] = random.randint(0, 2)
            if self.balls[count]['y'] > 470:
                self.balls[count]['ymove'] = random.randint(-2, 0)
            if self.balls[count]['y'] < -10:
                self.balls[count]['ymove'] = random.randint(0, 2)

    def manageBall(self):
        self.multipleBalls()
        self.ballsOnScreen()
        self.ballMove()
        self.ballBarrier()

class Character(sprite.Sprite):
    def __init__(self, xy):
        sprite.Sprite.__init__(self)
        self.image = image.load('character.png')
        self.rect = self.image.get_rect()
        self.rect.centerx, self.rect.centery = xy
        self.movementspeed = 1
        self.velocity = 0


    def down(self):
        self.velocity += self.movementspeed

    def up(self):
        self.velocity -= self.movementspeed

    def characterMove(self, dy):
        if self.rect.bottom + dy > 480:
            self.rect.bottom = 480
        elif self.rect.top + dy < 0:
            self.rect.top = 0
        else:
            self.rect.y += dy

    def update(self):
        self.characterMove(self.velocity)


class Game(object):
    def __init__(self):
        init()
        key.set_repeat(1, 30)
        self.screen = display.set_mode((640, 480))
        self.clock = time.Clock()
        display.set_caption('Game')
        event.set_allowed([QUIT, KEYDOWN, KEYUP])
        self.background = Surface((640, 480))
        self.background.fill((0,0,0))
        self.screen.blit(self.background, (0,0))
        display.flip()
        self.sprites = sprite.RenderUpdates()
        self.character = Character((320, 240))
        self.sprites.add(self.character)
        self.ball = Ball(5)
        self.sprites.add(self.ball)

    def run(self):
        running = True
        while running == True:
            self.clock.tick(60)
            running = self.handleEvents()
            display.set_caption('game %d fps' % self.clock.get_fps())
            for sprite in self.sprites:
                sprite.update()
            self.sprites.clear(self.screen, self.background)
            dirty = self.sprites.draw(self.screen)
            display.update(dirty)
            self.ball.manageBall()

    def handleEvents(self):
        for e in event.get():
            if e.type == QUIT:
                return False

            elif e.type == KEYDOWN:
                if e.key == K_ESCAPE:
                    return False
                if e.key == K_UP:
                    self.character.up()
                if e.key == K_DOWN:
                    self.character.down()

        return True



def main():
    game = Game()
    game.run()
main()

最佳答案

我以前从未使用过 Sprite 类,所以这是一个很好的练习。 :)

我修复了您的代码,只需将图像路径更改回您的图标位置,您就可以开始了。如果您运行的是旧版本的 Python,则打印语句可能需要重新格式化。

我取消了对 Ball 和 Character 类中 Sprite init 方法的强制调用,它不允许我将它们添加到 game.sprites 渲染组中。不知道为什么。

我正在运行 Python 3.2.2 和 Pygame 1.9.2pre 所以 YMMV。 :)

控制:

向上/向下=移动你的家伙。

R = 添加一个新的随机球。

from pygame import *
import random

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

FPS = 60

class Ball(sprite.Sprite):
    def __init__(self, xy = (0,0), xm = 0, ym = 0):
        sprite.Sprite.__init__(self)
        self.img_load('evil_balloon_32x32.png')
        self.rect.centerx, self.rect.centery = xy

        self.xmove = xm
        self.ymove = ym

    def update(self):
        self.move()
        self.ballBarrier()

    def move(self):
        self.rect.x += self.xmove
        self.rect.y += self.ymove

    def img_load(self, filename):
        self.image = image.load(filename)
        self.rect = self.image.get_rect()

    def ballBarrier(self):
        """
        Checks to make sure ball is within bounds, adjusts movement speed if it's not
        """
        if self.rect.right > SCREEN_WIDTH:
            self.xmove = random.randint(-2, 0)
        if self.rect.left < 0:
            self.xmove = random.randint(0, 2)
        if self.rect.bottom > SCREEN_HEIGHT:
            self.ymove = random.randint(-2, 0)
        if self.rect.top < 0:
            self.ymove = random.randint(0, 2)

class ball_manager():
    def __init__(self, numballs = 5, balls = []):      
        self.blist = balls

        if numballs > 0:
            self.multipleBalls(numballs) # moved this here so balls get init'd only once

    def update(self):
        """
        Update position of all balls
        """
        for ball in self.blist:
            self.ballMove(ball)

    def add_ball(self, xy = (0,0), xm = 0, ym = 0):
        self.blist.append(Ball(xy, xm, ym)) # appends a random ball

    def multipleBalls(self, numballs):
        for i in range(numballs):
            self.add_ball((random.randint(0, SCREEN_WIDTH),
                          random.randint(0, SCREEN_HEIGHT)),
                          random.randint(-2,2),
                          random.randint(-2,2))

class Character(sprite.Sprite):
    def __init__(self, xy):
        sprite.Sprite.__init__(self)
        self.img_load()

        self.rect.centerx, self.rect.centery = xy

        self.movementspeed = 1
        self.velocity = 0

    def down(self):
        self.velocity += self.movementspeed

    def up(self):
        self.velocity -= self.movementspeed

    def characterMove(self, dy):
        if self.rect.bottom + dy > SCREEN_HEIGHT:
            self.rect.bottom = SCREEN_HEIGHT
            self.velocity = 0
        elif self.rect.top + dy < 0:
            self.rect.top = 0
            self.velocity = 0
        else:
            self.rect.y += dy

    def update(self):
        self.characterMove(self.velocity)

    def img_load(self):
        self.image = image.load("scary_clown_32x32.png")
        self.rect = self.image.get_rect()

class Game(object):
    def __init__(self):
        init()
        key.set_repeat(1, 30)
        self.screen = display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        self.clock = time.Clock()
        display.set_caption('Game')

        event.set_allowed([QUIT, KEYDOWN, KEYUP])
        self.background = Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        self.background.fill((0,0,0))
        self.screen.blit(self.background, (0,0))
        display.flip()

        self.sprites = sprite.RenderUpdates()

        self.character = Character((SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))

        self.sprites.add(self.character)

        self.balls = ball_manager(5)

        for ball in self.balls.blist:
            self.sprites.add(ball)

    def run(self):
        running = True
        while running == True:
            self.clock.tick(FPS)
            running = self.handleEvents()
            display.set_caption('game %d fps' % self.clock.get_fps())

            self.sprites.clear(self.screen, self.background)

            for sprite in self.sprites:
                sprite.update()

            dirty = self.sprites.draw(self.screen)
            display.update(dirty)


    def handleEvents(self):
        for e in event.get():
            if e.type == QUIT:
                return False

            elif e.type == KEYDOWN:
                if e.key == K_ESCAPE:
                    return False
                if e.key == K_UP:
                    self.character.up()
                if e.key == K_DOWN:
                    self.character.down()
                if e.key == K_r:
                   self.sprites.add(Ball((random.randint(0, SCREEN_WIDTH),
                          random.randint(0, SCREEN_HEIGHT)),
                          random.randint(-2,2),
                          random.randint(-2,2))) 


        return True

def main():
    game = Game()
    game.run()
    quit()

main()
sys.exit()

关于python-2.7 - 在pygame中让多个球移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10357466/

相关文章:

python - 在pygame中按多次键来移动图像

python - Pygame 碰撞检测有缺陷

python - 在pygame中随机创建图像

python - pygame-永久调整主音量

python - 扫描类名相同的行元素

python - pip 或 pip3 为 Python 3 安装包?

python - 洗牌的程序

python - 加载/解析数学编程系统文件

python - 同时求和 4 维矩阵的 3 个维度

python - PyGame GUI 出现问题 - Raspberry Pi