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

Python 如何从屏幕上删除单个pygame绘图?,python,pygame,draw,Python,Pygame,Draw,当大圆圈碰到小圆圈时,我希望它碰到的小圆圈从屏幕上消失。然而,我不知道你是如何在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

当大圆圈碰到小圆圈时,我希望它碰到的小圆圈从屏幕上消失。然而,我不知道你是如何在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()
从pygame导入*
将随机导入为rd
导入数学为m
init()
屏幕=显示。设置_模式((800600))
p_1_x=200
p_1_y=200
p_1_变化_x=0
p_1_变化_y=0
def p_1(x,y):
玩家_1=画圆(屏幕,(0,0,0),(x,y),15)
def拾取(x,y,xx,yy):
距离=m.sqrt(m.pow(xx-x,2)+m.pow(yy-y,2))
如果距离小于19:
#我认为要删除的代码应该放在这里
通过
点=[]
地点=[]
对于范围(5)中的i:
x=rd.randint(100700)
y=rd.randint(100500)
位置。附加((x,y))
尽管如此:
屏幕填充((255、255、255))
对于event.get()中的事件:
如果events.type==退出:
退出
如果events.type==KEYDOWN:
如果events.key==K_RIGHT:
p_1_变化_x=1
如果events.key==K_LEFT:
p_1_change_x=-1
如果events.key==K\u UP:
p_1_变化_y+=1
如果events.key==K_DOWN:
p_1_变化_y-=1
如果events.type==KEYUP:
如果events.key==K_RIGHT或K_LEFT或K_UP或K_DOWN:
p_1_变化_x=0
p_1_变化_y=0
p_1_x+=p_1_变化
p_1_y-=p_1_变化
对于i,在枚举中查找(位置):
点=绘制圆(屏幕,(0,0,0),定位,5)
点。追加(点)
对于枚举中的l(定位):
拾取(p_1_x,p_1_y,定位[0],定位[1])
p_1(p_1_x,p_1_y)
display.update()

代码应将其从
位置列表中删除,以便以后不再重新绘制。您在每一帧清除屏幕,因此清除+不重画是“删除”

假设您修改了pick_up()
,只返回True或False:

def pick_up(x, y, xx, yy):
    result = False
    distance = m.sqrt(m.pow(xx - x, 2) + m.pow(yy - y, 2))
    if distance < 19:
        result = True              # It was picked
    return result

你的代码非常混乱,很难维护,首先我为Balls&Dots创建了两个类。 我通过
pygame.Rect.collide Rect
检测碰撞,首先我制作2个矩形,然后像这样检查碰撞:

def pick_up(ball, dot):
    ball_rect = Rect( ball.x - ball.SIZE , ball.y - ball.SIZE , ball.SIZE*2, ball.SIZE*2)
    dot_rect = Rect( dot.x - dot.SIZE , dot.y - dot.SIZE , dot.SIZE*2, dot.SIZE*2)
    if ball_rect.colliderect(dot_rect): 
        return True
    return False
如果检测到碰撞,我将其从
循环中的dots数组中删除,而
循环:

for dot in dots:
    if pick_up(ball, dot): # if dot in range ball
            dots.remove(dot)
    dot.draw()
以下是全部资料来源:

from pygame import *
import random as rd

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
NUMBER_OF_DOTS = 5

class Ball():
    SIZE = 15
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def draw(self):
        draw.circle(screen, (0, 0, 0), (self.x, self.y), Ball.SIZE)
    def move(self, vx, vy):
        self.x += vx
        self.y += vy

class Dot():
    SIZE = 5    
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def draw(self):
        draw.circle(screen, (0, 0, 0), (self.x, self.y), Dot.SIZE)


def pick_up(ball, dot):
    ball_rect = Rect( ball.x - ball.SIZE , ball.y - ball.SIZE , ball.SIZE*2, ball.SIZE*2)
    dot_rect = Rect( dot.x - dot.SIZE , dot.y - dot.SIZE , dot.SIZE*2, dot.SIZE*2)
    if ball_rect.colliderect(dot_rect): 
        return True
    return False



init()
screen = display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

dots = []
ball = Ball(200,200)

# generate dots
for i in range(NUMBER_OF_DOTS):
    x = rd.randint(100, 700)
    y = rd.randint(100, 500)
    dots.append(Dot(x,y))

# the main game loop
while True:
    screen.fill((255, 255, 255))
    keys=key.get_pressed()

    for events in event.get():
        keys=key.get_pressed()
        if events.type == QUIT:
            quit()

    if keys[K_RIGHT]:
        ball.move(+1,0)
    if keys[K_LEFT]:
        ball.move(-1,0)
    if keys[K_UP]:
        ball.move(0,-1)
    if keys[K_DOWN]:
        ball.move(0,+1)

    for dot in dots:
        dot.draw()
        
        if pick_up(ball, dot):
                dots.remove(dot)

    ball.draw()
    display.update()
    time.delay(1) # Speed down
更新1: PyGame矩形碰撞

更新2: 我在github中进行了回购,并做了一些更改,
圆点是五颜六色的,新圆点的颜色是随机的,只要吃了一个圆点,球就会变大。


为什么有时会说索引超出范围?@mr.bug-啊,也许
sorted()
没有像我想象的那样工作?它应该首先删除最大的元素,以便将它们从列表的“远端”中删除。啊,或者在列表的末尾有多次碰撞?您可以添加一个
print()
,以查看正在删除的内容的索引。
from pygame import *
import random as rd

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
NUMBER_OF_DOTS = 5

class Ball():
    SIZE = 15
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def draw(self):
        draw.circle(screen, (0, 0, 0), (self.x, self.y), Ball.SIZE)
    def move(self, vx, vy):
        self.x += vx
        self.y += vy

class Dot():
    SIZE = 5    
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def draw(self):
        draw.circle(screen, (0, 0, 0), (self.x, self.y), Dot.SIZE)


def pick_up(ball, dot):
    ball_rect = Rect( ball.x - ball.SIZE , ball.y - ball.SIZE , ball.SIZE*2, ball.SIZE*2)
    dot_rect = Rect( dot.x - dot.SIZE , dot.y - dot.SIZE , dot.SIZE*2, dot.SIZE*2)
    if ball_rect.colliderect(dot_rect): 
        return True
    return False



init()
screen = display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

dots = []
ball = Ball(200,200)

# generate dots
for i in range(NUMBER_OF_DOTS):
    x = rd.randint(100, 700)
    y = rd.randint(100, 500)
    dots.append(Dot(x,y))

# the main game loop
while True:
    screen.fill((255, 255, 255))
    keys=key.get_pressed()

    for events in event.get():
        keys=key.get_pressed()
        if events.type == QUIT:
            quit()

    if keys[K_RIGHT]:
        ball.move(+1,0)
    if keys[K_LEFT]:
        ball.move(-1,0)
    if keys[K_UP]:
        ball.move(0,-1)
    if keys[K_DOWN]:
        ball.move(0,+1)

    for dot in dots:
        dot.draw()
        
        if pick_up(ball, dot):
                dots.remove(dot)

    ball.draw()
    display.update()
    time.delay(1) # Speed down