1

マウスボタンが上にあるときに機能を作ろうとしています。幽霊の写真を1つの画像に変更します。

問題は、何を呼び出すべきかわかりません(したがって、スクリプトの ??? です)。お化けはループで作るので大変です。誰でも助けることができますか?ゴーストをスプライトに変更する必要があるのでしょうか?あなたもそれを手伝ってもらえますか?

import pygame
import random
import sys

class Ball:
    def __init__(self,X,Y,imagefile):
        self.velocity = [3,3]
        self.ball_image = pygame.image.load (imagefile). convert() ### i want this image to change
        self.ball_boundary = self.ball_image.get_rect (center=(X,Y))
        self.sound = pygame.mixer.Sound ('Thump.wav')

if __name__ =='__main__':
    width = 800
    height = 600
    background_colour = 0,0,0
    GHOST_IMAGE = ["images/blue-right.png", "images/red-right.png", "images/orange-right.png", "images/pink-right.png"]
    GHOST_IMAGETWO = ["images/blue-left.png", "images/red-left.png", "images/orange-left.png", "images/pink-left.png"]
    pygame.init()
    frame = pygame.display.set_mode((width, height))
    pygame.display.set_caption("Bouncing Ball animation")
    num_balls = 4
    ball_list = []
    for i in range(num_balls):
        ball_list.append( Ball(random.randint(0, width),random.randint(0, height), (GHOST_IMAGE[i]) ))
    while True:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT:
                sys.exit(0)
            elif event.type == pygame.MOUSEBUTTONUP:
                ??? = pygame.image.load("images/vulnerable.png").convert() ###i know that this is where and what i need to change it to, but dont know what instance name to call upon.
        frame.fill(background_colour)
        for ball in ball_list:
            if ball.ball_boundary.left < 0 or ball.ball_boundary.right > width:
                ball.sound.play()
                ball.velocity[0] = -1 * ball.velocity[0]


            if ball.ball_boundary.top < 0 or ball.ball_boundary.bottom > height:
                ball.sound.play()
                ball.velocity[1] = -1 * ball.velocity[1]

            ball.ball_boundary = ball.ball_boundary.move (ball.velocity)
            frame.blit (ball.ball_image, ball.ball_boundary)
        pygame.display.flip()
4

1 に答える 1

0

1 つの方法は、 を反復処理してball_list各ボールを変更することです。

elif event.type == pygame.MOUSEBUTTONUP:
    image = pygame.image.load("images/vulnerable.png").convert()
    for ball in ball_list:
        ball.ball_image = image

Ballもう 1 つの方法は、画像を変更する動作をクラスに直接実装することです。

class Ball:
    def __init__(self,X,Y,imagefile):
        self.vulnerable = False
        self.velocity = [3,3]
        self.normal_ball_image = pygame.image.load (imagefile). convert()
        self.v_ball_image = pygame.image.load("images/vulnerable.png").convert()
        self.ball_image = self.normal_ball_image
        self.ball_boundary = self.ball_image.get_rect (center=(X,Y))
        self.sound = pygame.mixer.Sound ('Thump.wav')

    def toggle(self):
        self.vulnerable = not self.vulnerable
        self.ball_image = self.v_ball_image if self.vulnerable else self.normal_ball_image

そしてあなたのループで:

elif event.type == pygame.MOUSEBUTTONUP:
    for ball in ball_list:
        ball.toggle()
于 2013-03-20T07:48:13.790 に答える