精灵是否有可能对特定颜色做出反应(pygame)

问题描述 投票:0回答:1

我有一个程序,如果您在屏幕上单击鼠标左键,会在上面画一条线,然后在2秒钟后,该线消失。在这2秒钟内无法绘制任何新线,如果在这2秒钟内单击鼠标左键,则会出现一个白点。如果您想运行它并亲自查看,这是我的代码。这些白点在几秒钟后消失。无论如何,是否有可能使一个像吃豆人的精灵“吃掉”这些点,而不是自己消失?我的想法是,如果屏幕上存在白点,那么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()
python pygame
1个回答
0
投票

[...] pacman精灵将被激活并移向白色点,然后,如果精灵到达该点,该点就会消失,甚至可能吗?

是的,但不是您的方法。您将不得不重写整个应用程序。

关键是在每一帧中重新绘制整个屏幕。场景中的所有对象都必须以数据结构,分别以pygame.sprite.Spritepygame.sprite.Sprite的形式存储在对象和列表中。通过这种方法,可以通过从数据结构(分别为列表和组)中添加和删除对象来绘制动态动画对象并向场景中添加和删除对象。

注意,基本的动态应用程序具有一个循环,该循环不断重绘场景。主应用程序循环必须:

  • 通过pygame.sprite.Grouppygame.sprite.Group处理事件。
  • 根据输入事件和时间(分别为帧)更新游戏状态和对象的位置。例如Sprite位置,从Sprite工作表中选择的用于动画的图像等。
  • 清除整个显示或绘制背景
  • 绘制整个场景(blitdraw所有对象和精灵)
  • 通过pygame.event.pump()pygame.event.pump()更新显示
pygame.event.get()
© www.soinside.com 2019 - 2024. All rights reserved.