如何使图像随机显示和消失?

时间:2018-05-17 07:58:52

标签: python pygame

我有一个问题,事实上我的痣图像出现了但并没有消失。我只想要一个痣出现并在10秒后消失,另一个痣出现等等。那是我的代码:

如果我执行了time.delay(10)我的代码崩溃了。 我已经尝试了一百万件事,但我现在被卡住了。你能帮我吗?我不知道该怎么做。

import pygame
import random
import time

pygame.init()    
display_width = 600
display_height = 480

gameDisplay = pygame.display.set_mode((display_width, display_height))
fond = pygame.image.load("fond.bmp").convert()
gameDisplay.blit(fond, (0, 0))
pygame.display.set_caption('Tape Taupe')
clock = pygame.time.Clock()

BAMimg = pygame.image.load('Marteau.png')
gameIcon = pygame.image.load('Taupe.png').convert_alpha()
pygame.display.set_icon(gameIcon)

BAMimg_width = 73

perso1 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso1, (160, 55))

perso2 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso2, (320, 55))

perso3 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso3, (480, 55))

perso4 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 200))

perso5 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 200))

perso6 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 200))

perso7 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 350))

perso8 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 350))

perso9 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 350))

pygame.display.update()


def BAMImg(x, y):
    gameDisplay.blit(BAMImg, (x, y))
    x = (display_width * 0.45)
    y = (display_height * 0.8)

def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -5
                if event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0



                BAMimg(x, y)
                x += x_change
                y += y_change

            Taupe = pygame.image.load("Taupe.png").convert_alpha()
            coordinates = random.choice([[160, 55], [320, 55], [480, 55], [160, 200], [320, 200], [480, 200], [160, 350], [320, 350],[480, 350]])
            gameDisplay.blit(Taupe, coordinates)

            pygame.display.update()
            clock.tick(60)


game_loop()
pygame.quit()

1 个答案:

答案 0 :(得分:0)

您可以使用pygame.time.set_timer函数在指定的时间间隔后向事件队列添加事件。事件类型实际上只是整数,您可以通过以下方式定义自己的事件:CHANGE_COORDINATES = pygame.USEREVENT + 1

然后在事件循环中检查此事件是否在事件队列中并调用random.choice以获取新坐标。

我在下面的示例中修复了一些代码问题:

import pygame
import random


pygame.init()    
display_width = 600
display_height = 480

gameDisplay = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()

Taupe = pygame.Surface((50, 50))
Taupe.fill((30, 90, 230))
BAMimg = pygame.Surface((73, 73))
BAMimg.fill((230, 90, 30))
# We use this custom event for our timer.
CHANGE_COORDINATES = pygame.USEREVENT + 1


# This function had almost the same name as the BAMimg variable
# and you tried to blit the function itself not the image here.
def display_BAMImg(x, y):  
    gameDisplay.blit(BAMimg, (x, y))
    x = display_width * 0.45
    y = display_height * 0.8


def game_loop():
    x = display_width * 0.45
    y = display_height * 0.8

    coordinates_list = [
        [160, 55], [320, 55], [480, 55], [160, 200],
        [320, 200], [480, 200], [160, 350],
        [320, 350], [480, 350],
        ]
    x_change = 0
    y_change = 0
    coordinates = random.choice(coordinates_list)
    # This means after 2000 milliseconds a `CHANGE_COORDINATES`
    # event will be added to the event queue.
    pygame.time.set_timer(CHANGE_COORDINATES, 2000)

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
                elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0
            # Check if a `CHANGE_COORDINATES` event was in the
            # event queue and if yes, call random.choice to get
            # new coordinates.
            elif event.type == CHANGE_COORDINATES:
                coordinates = random.choice(coordinates_list)

        # The following lines should not be in the event loop.
        gameDisplay.fill((30, 30, 30))  # Clear the display each frame.
        display_BAMImg(x, y)
        x += x_change
        y += y_change

        gameDisplay.blit(Taupe, coordinates)

        pygame.display.update()
        clock.tick(60)


game_loop()
pygame.quit()