如何从屏幕上删除单个 pygame 绘图?

当大圆圈接触小圆圈时,我希望它接触的小圆圈从屏幕上消失。然而,我不知道你到底是如何删除 pygame 中的单个绘图的。我该如何解决这个问题?pygame有内置这个功能吗?


from pygame import *

import random as rd

import math as m


init()

screen = display.set_mode((800, 600))


p_1_x = 200

p_1_y = 200

p_1_change_x = 0

p_1_change_y = 0


def p_1(x, y):

    player_1 = draw.circle(screen, (0, 0, 0), (x, y), 15)


def pick_up(x, y, xx, yy):

    distance = m.sqrt(m.pow(xx - x, 2) + m.pow(yy - y, 2))

    if distance < 19:

        # I think the code to delete should go here

        pass


dots = []

locations = []


for i in range(5):

    x = rd.randint(100, 700)

    y = rd.randint(100, 500)

    locations.append((x, y))


while True:

    screen.fill((255, 255, 255))

    for events in event.get():

        if events.type == QUIT:

            quit()

        if events.type == KEYDOWN:

            if events.key == K_RIGHT:

                p_1_change_x = 1

            if events.key == K_LEFT:

                p_1_change_x = -1

            if events.key == K_UP:

                p_1_change_y += 1

            if events.key == K_DOWN:

                p_1_change_y -= 1


        if events.type == KEYUP:

            if events.key == K_RIGHT or K_LEFT or K_UP or K_DOWN:

                p_1_change_x = 0

                p_1_change_y = 0


    p_1_x += p_1_change_x

    p_1_y -= p_1_change_y

    for i, locate in enumerate(locations):

        dot = draw.circle(screen, (0, 0, 0), locate, 5)

        dots.append(dot)

        for l in enumerate(locate):

            pick_up(p_1_x, p_1_y, locate[0], locate[1])


    p_1(p_1_x, p_1_y)

    display.update()


Smart猫小萌
浏览 233回答 2
2回答

拉风的咖菲猫

你的代码是如此混乱且难以维护,首先我为球和点制作了 2 个类。我通过 检测碰撞pygame.Rect.colliderect,首先我制作 2 个矩形,然后检查碰撞,如下所示:def pick_up(ball, dot):&nbsp; &nbsp; ball_rect = Rect( ball.x - ball.SIZE , ball.y - ball.SIZE , ball.SIZE*2, ball.SIZE*2)&nbsp; &nbsp; dot_rect = Rect( dot.x - dot.SIZE , dot.y - dot.SIZE , dot.SIZE*2, dot.SIZE*2)&nbsp; &nbsp; if ball_rect.colliderect(dot_rect):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; return False如果检测到碰撞,我将其从循环中的点数组中删除while:for dot in dots:&nbsp; &nbsp; if pick_up(ball, dot): # if dot in range ball&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dots.remove(dot)&nbsp; &nbsp; dot.draw()这是完整的来源:from pygame import *import random as rdSCREEN_WIDTH = 800SCREEN_HEIGHT = 600NUMBER_OF_DOTS = 5class Ball():&nbsp; &nbsp; SIZE = 15&nbsp; &nbsp; def __init__(self, x, y):&nbsp; &nbsp; &nbsp; &nbsp; self.x = x&nbsp; &nbsp; &nbsp; &nbsp; self.y = y&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def draw(self):&nbsp; &nbsp; &nbsp; &nbsp; draw.circle(screen, (0, 0, 0), (self.x, self.y), Ball.SIZE)&nbsp; &nbsp; def move(self, vx, vy):&nbsp; &nbsp; &nbsp; &nbsp; self.x += vx&nbsp; &nbsp; &nbsp; &nbsp; self.y += vyclass Dot():&nbsp; &nbsp; SIZE = 5&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def __init__(self, x, y):&nbsp; &nbsp; &nbsp; &nbsp; self.x = x&nbsp; &nbsp; &nbsp; &nbsp; self.y = y&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def draw(self):&nbsp; &nbsp; &nbsp; &nbsp; draw.circle(screen, (0, 0, 0), (self.x, self.y), Dot.SIZE)def pick_up(ball, dot):&nbsp; &nbsp; ball_rect = Rect( ball.x - ball.SIZE , ball.y - ball.SIZE , ball.SIZE*2, ball.SIZE*2)&nbsp; &nbsp; dot_rect = Rect( dot.x - dot.SIZE , dot.y - dot.SIZE , dot.SIZE*2, dot.SIZE*2)&nbsp; &nbsp; if ball_rect.colliderect(dot_rect):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; return Falseinit()screen = display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))dots = []ball = Ball(200,200)# generate dotsfor i in range(NUMBER_OF_DOTS):&nbsp; &nbsp; x = rd.randint(100, 700)&nbsp; &nbsp; y = rd.randint(100, 500)&nbsp; &nbsp; dots.append(Dot(x,y))# the main game loopwhile True:&nbsp; &nbsp; screen.fill((255, 255, 255))&nbsp; &nbsp; keys=key.get_pressed()&nbsp; &nbsp; for events in event.get():&nbsp; &nbsp; &nbsp; &nbsp; keys=key.get_pressed()&nbsp; &nbsp; &nbsp; &nbsp; if events.type == QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quit()&nbsp; &nbsp; if keys[K_RIGHT]:&nbsp; &nbsp; &nbsp; &nbsp; ball.move(+1,0)&nbsp; &nbsp; if keys[K_LEFT]:&nbsp; &nbsp; &nbsp; &nbsp; ball.move(-1,0)&nbsp; &nbsp; if keys[K_UP]:&nbsp; &nbsp; &nbsp; &nbsp; ball.move(0,-1)&nbsp; &nbsp; if keys[K_DOWN]:&nbsp; &nbsp; &nbsp; &nbsp; ball.move(0,+1)&nbsp; &nbsp; for dot in dots:&nbsp; &nbsp; &nbsp; &nbsp; dot.draw()&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if pick_up(ball, dot):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dots.remove(dot)&nbsp; &nbsp; ball.draw()&nbsp; &nbsp; display.update()&nbsp; &nbsp; time.delay(1) # Speed down更新1:PyGame 矩形碰撞 http://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect更新2:我在 github 上做了一个 repo 并做了一些更改,点是彩色的,新点的颜色是随机的,每当吃掉一个点时,球就会变大。

小怪兽爱吃肉

代码应该将其从locations列表中删除,以便将来不会重新绘制。你每一帧都清除屏幕,所以清除+不重画就是“删除”。假设您修改pick_up()为仅返回 True 或 False:def pick_up(x, y, xx, yy):&nbsp; &nbsp; result = False&nbsp; &nbsp; distance = m.sqrt(m.pow(xx - x, 2) + m.pow(yy - y, 2))&nbsp; &nbsp; if distance < 19:&nbsp; &nbsp; &nbsp; &nbsp; result = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # It was picked&nbsp; &nbsp; return result然后,当您迭代locations列表绘图并检查是否被拾取时,保存所拾取圆的索引,然后在locations第二步中将它们从列表中删除。使用两步形式意味着如果您在迭代列表时从列表中删除项目,则不必担心意外跳过项目。p_1_x += p_1_change_xp_1_y -= p_1_change_ypicked_up = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# empty list to hold "picked" itemsfor i, locate in enumerate(locations):&nbsp; &nbsp; dot = draw.circle(screen, (0, 0, 0), locate, 5)&nbsp; &nbsp; dots.append(dot)&nbsp; &nbsp; for l in enumerate(locate):&nbsp; &nbsp; &nbsp; &nbsp; if ( pick_up(p_1_x, p_1_y, locate[0], locate[1]) ):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; picked_up.append( i )&nbsp; &nbsp; &nbsp; &nbsp; # save the index of anything "picked"# remove any picked-up circles from the listfor index in sorted( picked_up, reverse=True ):&nbsp; &nbsp;# start with the highest index first&nbsp; &nbsp; print( "Removing circle from location[%d]" % ( index ) )&nbsp; # DEBUG&nbsp; &nbsp; del( locations[ index ] )
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python