我不能blit同一个对象多次

编程入门 行业动态 更新时间:2024-10-26 12:31:47
本文介绍了我不能blit同一个对象多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在制作一个吉他英雄喜欢游戏在python,我试图 blit 一个笔记多次,但我似乎无法得到它的工作注意被称为红色)!

I am currently making a Guitar Hero like game in python and I am trying to blit a note multiple times, but I can't seem to get it to work (the note is called Red)!

#Sprite Class class Sprite(pygame.sprite.Sprite): # What has to be passed when you create a sprite. # image_file: The filename of the image. # lacation: The initial location of where to draw the image. def __init__(self, image_file, location): pygame.sprite.Sprite.__init__(self) # Call Sprite initializer self.image = pygame.image.load(image_file) self.rect = self.image.get_rect() self.rect.left, self.rect.top = location self.xSpeed = 15 # The default xSpeed self.ySpeed = 15 # The default ySpeed self.name = "Not Assigned" self.speed = 10 self.direction = 0 def setSpeed(self, speed): self.speed = speed self.calcXYSpeeds() def setDirection(self, direction): self.direction = direction self.calcXYSpeeds() def calcXYSpeeds(self): angleRadians = math.radians(self.direction) # Convert the direction to radians. self.xSpeed= self.speed*math.cos(angleRadians) self.ySpeed = self.speed*math.sin(angleRadians) def move(self): self.rect = self.rect.move(self.xSpeed,self.ySpeed) def setDirectionTowards(self, (x,y)): self.direction = math.degrees(math.atan2((self.rect.x - x), (self.rect.y - y))) self.direction = 270 - self.direction self.calcXYSpeeds() #Object Variables Red = Sprite("Red.png", (100,25)) note1 = Sprite("note1.jpg", (50,650)) #add sprite to group notes = pygame.sprite.Group() notes.add(Red) # Create an clock to keep track time clock = pygame.time.Clock() #Scoring Variables font=pygame.font.Font(None,50) score=0 score_text=font.render(str(score),1,(225,225,225)) #other Variables Red1=0 ySpeed = 10 running = 1 while (running==1): # Sets the frame rate to 30 frames/second. clock.tick(30) # Gets any events like keyboard or mouse. event = pygame.event.poll() key=pygame.key.get_pressed() #object random spawn time = pygame.time.get_ticks() if (time==30*(random.randint(0,1000))): screen.blit(Red.image, Red.rect) pygame.display.update() #Object Movement Red.rect.y = Red.rect.y + ySpeed #Keys to complete notes if key[pygame.K_a]and Red1==0 and pygame.sprite.collide_rect(Red, note1) == True: font=pygame.font.Font(None,50) score = score+1 score_text=font.render(str(score),1,(225,225,225)) Red1=1 #where i currently working to fix the problem notes.Group.clear(screen, background) notes.Group.update() notes.draw(screen) # Sets the exit flag if the X was clicked on the run window. if event.type == pygame.QUIT: running = 0 # Color the whole screen with a solid color. screen.fill((0, 255, 255)) #prints objects screen.blit(note1.image, note1.rect) screen.blit(Red.image, Red.rect) screen.blit(score_text,(500,50)) # Update the window. pygame.display.update()

推荐答案

在每个周期清洁屏幕是一种正确的方法,但你只需要清洁一次,否则你是

Clean the screen in every cycle is a correct approach, but you need to clean only once otherwise you are erasing everything you blitted before.

此外,对于在循环之间持续的音符,您需要将它们保存在一个集合中,例如音符,这样就可以在每个循环中嵌入每个音符

Also, for notes to persist between cycles you need to keep them in a collection, like notes, that way you blit every note in every cycle.

请记住您的主循环中的顺序:

Keep in mind this order in your main loop:

  • 控制FPS (clock.tick())
  • 检查事件
  • 更新状态(例如:添加/删除注释集合等)
  • 清除屏幕
  • 绘制当前状态(注释,静态元素等)
  • 更新显示
  • Control FPS (clock.tick())
  • Check events
  • Update state (ie: add/remove notes collection, etc)
  • Clear screen
  • Draw current state (notes, static elements, etc)
  • Update display
  • 另一个重要的事情,不要在不同的位置使用相同的Sprite()和相同的Rectangle。如果要在新位置绘制相同图像的另一个副本,请创建一个矩形的副本,然后创建一个新的Sprite()对象。

    Another important thing, don't use the same Sprite() nor the same Rectangle in different locations. If you want to draw another copy of the same image in a new location make a copy of the rectangle, then create a new Sprite() object.

    更多推荐

    我不能blit同一个对象多次

    本文发布于:2023-11-22 08:31:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1616699.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:对象   blit

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!