亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在激活碰撞時讓我的“游戲結束”文本保持亮起?

如何在激活碰撞時讓我的“游戲結束”文本保持亮起?

慕村225694 2022-12-06 17:26:17
我制作了一個帶有多個下落立方體的小游戲,玩家(正方形)必須避開它。我設法使碰撞起作用,但問題是每次圓形和正方形碰撞時,每次碰撞時都會顯示文本。但我希望當圓形和方形第一次發生碰撞時,文本會繼續顯示。有什么辦法嗎?import pygamefrom pygame.locals import *import osimport randomimport mathimport sysimport timewhite = (255,255,255)blue = (0,0,255)gravity = 10size =10height = 500width =600varHeigth = heightballNum = 5eBall = []apGame = pygame.display.set_mode((width, height))pygame.display.set_caption("AP Project")clock = pygame.time.Clock()class Player(object):  def __init__(self):    red = (255, 0, 0)    move_x = 300    move_y = 400    self.rect = pygame.draw.rect(apGame,red, (move_x, move_y, 10, 10))    self.dist = 10  def handle_keys(self):    for e in pygame.event.get():      if e.type == pygame.QUIT:        pygame.quit();        exit()    key = pygame.key.get_pressed()    if key[pygame.K_LEFT]:      self.draw_rect(-1, 0)    elif key[pygame.K_RIGHT]:      self.draw_rect(1, 0)    elif key[pygame.K_ESCAPE]:      pygame.quit();      exit()    else:      self.draw_rect(0, 0)  def draw_rect(self, x, y):    red = (255, 0, 0)    black = (0, 0, 0)    '''apGame.fill(black)'''    self.rect = self.rect.move(x * self.dist, y * self.dist);    pygame.draw.rect(apGame, red , self.rect)    pygame.display.update()  def draw(self,surface):    red = (255, 0, 0)    move_x = 300    move_y = 400    pygame.draw.rect(apGame, red, (move_x, move_y, 10, 10))#The game over text here def show_go_screen():  pygame.font.init()  myfont = pygame.font.SysFont("Comic Sans MS", 30)  label = myfont.render("Game Over", 1, red)  apGame.blit(label, (300,100))def instuct():  pygame.font.init()  myfont = pygame.font.SysFont("Comic Sans MS", 15)  label = myfont.render("Avoid The Circles", 1, red)  apGame.blit(label, (250,450))
查看完整描述

1 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

每當您希望功能只運行一次時,您應該在該函數外部添加一個檢查以確保該函數內的代碼只運行一次。這是使用您的代碼的示例。


collidedOnce = False


if player.rect.colliderect(ball_rect):

    if(collidedOnce == False):

        collidedOnce = True #This makes the code only run once since it's setting this variable to be true within the condition

        #game_over = True

        show_go_screen()



        eBall[i][1] += 5


        if eBall[i][1] > height:


            y = random.randrange(-50, -10)

            eBall[i][1] = y


            x = random.randrange(0, width)

            eBall[i][0] = x


        instuct()

        player.handle_keys()

        pygame.display.flip()

        clock.tick(30)


希望這可以幫助


查看完整回答
反對 回復 2022-12-06
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號