第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從屏幕上刪除單個(gè) pygame 繪圖?

如何從屏幕上刪除單個(gè) pygame 繪圖?

Smart貓小萌 2023-08-22 18:21:19
當(dāng)大圓圈接觸小圓圈時(shí),我希望它接觸的小圓圈從屏幕上消失。然而,我不知道你到底是如何刪除 pygame 中的單個(gè)繪圖的。我該如何解決這個(gè)問題?pygame有內(nèi)置這個(gè)功能嗎?from pygame import *import random as rdimport math as minit()screen = display.set_mode((800, 600))p_1_x = 200p_1_y = 200p_1_change_x = 0p_1_change_y = 0def 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        passdots = []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()
查看完整描述

2 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

你的代碼是如此混亂且難以維護(hù),首先我為球和點(diǎn)制作了 2 個(gè)類。我通過 檢測(cè)碰撞pygame.Rect.colliderect,首先我制作 2 個(gè)矩形,然后檢查碰撞,如下所示:


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

如果檢測(cè)到碰撞,我將其從循環(huán)中的點(diǎn)數(shù)組中刪除while:


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 矩形碰撞 http://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect


更新2:

我在 github 上做了一個(gè) repo 并做了一些更改,

點(diǎn)是彩色的,新點(diǎn)的顏色是隨機(jī)的,每當(dāng)吃掉一個(gè)點(diǎn)時(shí),球就會(huì)變大。

https://img1.sycdn.imooc.com//64e48c48000155c006080499.jpg

查看完整回答
反對(duì) 回復(fù) 2023-08-22
?
小怪獸愛吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

代碼應(yīng)該將其從locations列表中刪除,以便將來不會(huì)重新繪制。你每一幀都清除屏幕,所以清除+不重畫就是“刪除”。


假設(shè)您修改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

然后,當(dāng)您迭代locations列表繪圖并檢查是否被拾取時(shí),保存所拾取圓的索引,然后在locations第二步中將它們從列表中刪除。使用兩步形式意味著如果您在迭代列表時(shí)從列表中刪除項(xiàng)目,則不必?fù)?dān)心意外跳過項(xiàng)目。


p_1_x += p_1_change_x

p_1_y -= p_1_change_y

picked_up = []                           # empty list to hold "picked" items

for i, locate in enumerate(locations):

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

    dots.append(dot)

    for l in enumerate(locate):

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

            picked_up.append( i )        # save the index of anything "picked"


# remove any picked-up circles from the list

for index in sorted( picked_up, reverse=True ):   # start with the highest index first

    print( "Removing circle from location[%d]" % ( index ) )  # DEBUG

    del( locations[ index ] )


查看完整回答
反對(duì) 回復(fù) 2023-08-22
  • 2 回答
  • 0 關(guān)注
  • 318 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)