簡體   English   中英

精靈是否有可能對特定顏色做出反應

[英]Is it possible for a sprite to react to a specific color

我有一個程序,如果您在屏幕上單擊鼠標左鍵,則會畫一條線,然后在 2 秒后,這條線就會消失。 在這 2 秒內不能繪制新線,如果在這 2 秒內左鍵單擊,則會出現一個白點。 如果您想運行它並親自查看,這是我的代碼。 這些白點在幾秒鍾后消失。 無論如何,是否有可能制作一個類似 pacman 的精靈“吃掉”這些點而不是自行消失? 我的想法是,如果屏幕上有一個白點,那么 pacman 精靈將被激活並朝白點移動,然后如果精靈到達該點,該點就會消失,如果有,精靈就會移出屏幕沒有更多的點了。 如果還有點,那么它會直接到下一個點。 這甚至可能嗎?

from pygame import * 

init()
size = width, height = 650, 650
screen = display.set_mode(size)

WHITE = (255,255,255)
BLACK = (0, 0, 0)
RED = (255, 100, 94)
BLUE = (112, 219, 255)
GREEN = (138, 255, 142)
YELLOW = (255, 255, 158)
color = GREEN
start_time1 = None
start_time2 = None
color = GREEN
col = WHITE

running = True
myClock = time.Clock()
cur_pos = None 
prev_pos = None
start_pos = None
startt_pos = None
lines = 0
start_timer=None


while running:
    def backround():
        draw.circle(screen, YELLOW, (500,120), 75)
        draw.circle(screen, BLACK, (530,120), 50)
    backround()
    for e in event.get(): 
        if e.type == QUIT:
            running = False
        if e.type == MOUSEBUTTONDOWN:

            if e.button == 1:
                if prev_pos == None:
                    cur_pos = e.pos
                    start_pos = (0,0)
                    draw.line(screen, color, start_pos, cur_pos)
                    lines = lines+1
                    draw.circle(screen, color, e.pos, 5)
                    start_time = time.get_ticks()  #starts timer when the first line is drawn (from 0,0)
                    prev_pos = cur_pos

                if prev_pos != None: 
                    if time.get_ticks() - start_time >= 2000:   #if it has been 2 seconds, draw a line
                        lines = lines+1
                        draw.line(screen, color, prev_pos, e.pos) 
                        draw.circle(screen, color, e.pos, 5)
                        start_time = time.get_ticks()     #restart the timer
                        prevv_pos = prev_pos
                        prev_pos = e.pos



                    if time.get_ticks() - start_time <=2000 and time.get_ticks() - start_time > 0 and time.get_ticks() - start_time <=2500:
                        draw.circle(screen, col, e.pos, 5)



            if e.button == 3:      
                if color == GREEN:
                    color = BLUE
                elif color == BLUE:
                    color = RED
                elif color == RED:
                    color = GREEN              

    if lines >= 1:
        if lines==1: 
            start_timer = time.get_ticks() 

        if time.get_ticks() - start_timer >= 2000:
            if lines == 2:
                screen.fill(BLACK)
                draw.line(screen, color, prevv_pos, prev_pos)    
                lines = lines-1
                draw.circle(screen, color, prevv_pos, 5)
                draw.circle(screen, color, prev_pos, 5)
                backround()




    display.flip()
    myClock.tick(60)
    backround()

quit()

[...] pacman sprite 將被激活並向白點移動,然后如果 sprite 到達該點,該點就會消失,[...] 這甚至可能嗎?

是的,但不是你的方法。 您將不得不重寫整個應用程序。

關鍵是在每一幀中重繪整個屏幕。 場景中的所有 object 都必須分別存儲在數據結構、對象和列表中pygame.sprite.Spritepygame.sprite.Group 屏幕在每一幀中都被清除,對象和精靈在當前 position 和外觀處繪制。
使用這種方法,可以通過從數據結構(分別列出組)中添加和刪除對象來繪制動態動畫對象並向場景添加和刪除對象。

注意,一個基本的動態應用程序有一個循環,它不斷地重繪場景。 主應用程序循環必須:

window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

# main application loop
run = True
while run:
    clock.tick(60)

    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        # [...]

    # update the objects
    # [...]

    # clear the display or draw the background
    window.fill(0)

    # draw the scene   
    # [...]

    # update the display
    pygame.display.flip()

最小的例子: repl.it/@Rabbid76/PyGame-MinimalApplicationLoop

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM