Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 精灵是否可能对特定颜色产生反应_Python_Pygame - Fatal编程技术网

Python 精灵是否可能对特定颜色产生反应

Python 精灵是否可能对特定颜色产生反应,python,pygame,Python,Pygame,我有一个程序,如果你在屏幕上左键点击,就会画一条线,2秒钟后,这条线就会消失。在这2秒钟内不能绘制新线,如果在这2秒钟内单击鼠标左键,则会出现一个白点。这是我的代码,如果您想自己运行并查看。这些白点几秒钟后就消失了。不管怎样,有没有可能让一个像吃豆人一样的精灵“吃掉”这些点,而不是自己消失?我的想法是,如果屏幕上有一个白点,那么pacman精灵将被激活并向白点移动,然后如果精灵到达该点,该点将消失,如果没有更多的点,精灵将从屏幕上移开。如果还有点,那么它会直接到达下一个点。这可能吗 from p

我有一个程序,如果你在屏幕上左键点击,就会画一条线,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()
从pygame导入*
init()
尺寸=宽度,高度=650
屏幕=显示。设置模式(大小)
白色=(255255)
黑色=(0,0,0)
红色=(2551094)
蓝色=(112219255)
绿色=(138255142)
黄色=(255、255、158)
颜色=绿色
开始时间1=无
开始时间2=无
颜色=绿色
颜色=白色
运行=真
myClock=time.Clock()
当前位置=无
上一个位置=无
开始位置=无
开始位置=无
直线=0
启动计时器=无
运行时:
def backround():
画圆(屏幕,黄色,(500120),75)
画圆(屏幕,黑色,(530120),50)
回溯
对于事件中的e.get():
如果e.type==退出:
运行=错误
如果e.type==MOUSEBUTTONDOWN:
如果e.button==1:
如果上一个位置==无:
cur_pos=e.pos
开始位置=(0,0)
绘制线(屏幕、颜色、开始位置、当前位置)
线条=线条+1
画圆(屏幕、颜色、e.pos、5)
start_time=time.get_ticks()#在绘制第一行时启动计时器(从0,0开始)
上一个位置=当前位置
如果上一个位置!=无:
如果time.get_ticks()
线条=线条+1
绘制线(屏幕、颜色、上一位置、e位置)
画圆(屏幕、颜色、e.pos、5)
start_time=time.get_ticks()#重新启动计时器
前置位置=前置位置
上一个位置=东位置
如果time.get_ticks()-start_time 0和time.get_ticks()-start_time=1:
如果行==1:
start\u timer=time.get\u ticks()
如果time.get_ticks()-启动计时器>=2000:
如果行==2:
屏幕填充(黑色)
绘制线(屏幕、颜色、前置位置、前置位置)
行=行-1
画圈(屏幕、颜色、前置位置、5)
绘制圆(屏幕、颜色、上一位置、5)
回溯
display.flip()
我的时钟滴答(60)
回溯
退出
[…]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()
是的,但不是你的方法。你必须重写整个应用程序

关键是在每一帧中重新绘制整个屏幕。场景中的所有对象都必须分别存储在数据结构、对象和列表中。屏幕在每一帧中被清除,对象和精灵在当前位置和外观绘制。
使用这种方法,通过从数据结构(分别列出组)中添加和删除对象,可以绘制动态动画对象,并向场景中添加和删除对象

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

  • 通过或处理事件
  • 根据输入事件和时间(分别为帧)更新游戏状态和对象位置。例如,精灵位置、动画精灵表中的选定图像等
  • 清除整个显示或绘制背景
  • 绘制整个场景(blit并绘制所有对象和精灵)
  • 通过或更新显示
window=pygame.display.set_模式((500500))
clock=pygame.time.clock()
#主应用程序循环
运行=真
运行时:
时钟滴答(60)
#事件循环
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
# [...]
#更新对象
# [...]
#清除显示或绘制背景
窗口填充(0)
#画场景
# [...]
#更新显示
pygame.display.flip()

最简单的例子: