python – 软件设计与开发专业:Pygame Smudge Trails

前端之家收集整理的这篇文章主要介绍了python – 软件设计与开发专业:Pygame Smudge Trails前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先,我在网上搜索了这个网站的解决方案和我尝试过的那些都没有用,所以我决定发布我的个人问题和代码.该程序是使用Python 3.2.2和pygame的相应兼容版本创建的.我还意识到一个更有效的方法是使用精灵,精灵组和’脏矩形’更新,但我无法转换程序,所以我将继续没有这些功能的额外好处.

问题:“小行星”移动的污迹落后.
假设:背景在屏幕上显示,但小行星在背景上显示.

回复 – 顺便说一句,我是AUS的高中生:D

  1. import pygame
  2. import random
  3. import math
  4. pygame.init()
  5. height = 550
  6. width = 750
  7. screen = pygame.display.set_mode((width,height))
  8. background = pygame.image.load("Planet.jpg")
  9. Clock = pygame.time.Clock()
  10. class asteroid(pygame.sprite.Sprite):
  11. def __init__(self,x,y,size):
  12. pygame.sprite.Sprite.__init__(self)
  13. self.x = x
  14. self.y = y
  15. self.size = 15
  16. self.speed = 0.0
  17. self.angle = 0
  18. self.colour = (171,130,255)
  19. self.thickness = 0
  20. def display(self):
  21. pygame.draw.circle(background,self.colour,(int(self.x),int(self.y)),self.size,self.thickness)
  22. pygame.draw.circle(background,(255,255,255),1)
  23. def move(self):
  24. self.x += math.sin(self.angle) * self.speed
  25. self.y -= math.cos(self.angle) * self.speed
  26. def boundaries(self):
  27. if self.x > width - self.size:
  28. self.x = 0 + self.size
  29. elif self.x < self.size:
  30. self.x = width - self.size
  31. if self.y > height - self.size:
  32. self.y = 0 + self.size
  33. elif self.y score = (pygame.time.get_ticks()/1000)
  34. print (score)
  35. while True:
  36. pygame.display.update()
  37. screen.blit(background,0))
  38. MouseP = pygame.mouse.get_pos()
  39. frames = Clock.get_fps
  40. pygame.mouse.set_visible
  41. score = (pygame.time.get_ticks()/1000)
  42. print (score)
  43. print (MouseP)
  44. for target in my_particles:
  45. target.move()
  46. target.boundaries()
  47. target.display()
  48. pygame.display.update()
  49. for event in pygame.event.get():
  50. if event.type == pygame.QUIT:
  51. pygame.quit();
  52. if __name__=='__main__':
  53. main()
最佳答案
基本上,你是对的!圆圈直接绘制在背景上,每次绘制新圆圈时,旧圆圈都会保留.造成污迹/痕迹.

您可以在绘图方法中将背景更改为屏幕.这将解决它.

但是真的值得使用Sprite类.我对您的代码进行了一些更改,以便为您切换.通过这些更改,它运行没有路径:)

以下是变化和解释:

在顶部附近添加

  1. #Create a new `pygame.Surface`,and draw a circle on it,then set transparency:
  2. circle = pygame.Surface((30,30))
  3. circle = circle.convert()
  4. pygame.draw.circle(circle,(171,(int(15),int(15)),15,0)
  5. circle.set_colorkey(circle.get_at((0,0)),pygame.RLEACCEL)

将其添加到小行星__init__方法

  1. #Sets the asteroid image,and then the asteroids co-ords (these are in `rect`)
  2. self.image = circle
  3. self.rect = self.image.get_rect()

将其添加到def move(self)的末尾:

  1. self.rect[0] = self.x
  2. self.rect[1] = self.y

更改:

  1. my_particles = []

至:

  1. #This is a special pygame container class,it has a draw() method that tracks changed areas of the screen.
  2. my_particles = pygame.sprite.RenderUpdates()

更改:

  1. my_particles.append(target)

至:

  1. my_particles.add(target)

更改:

  1. while True:
  2. pygame.display.update()
  3. screen.blit(background,0))
  4. MouseP = pygame.mouse.get_pos()
  5. frames = Clock.get_fps
  6. pygame.mouse.set_visible
  7. score = (pygame.time.get_ticks()/1000)
  8. print (score)
  9. print (MouseP)
  10. for target in my_particles:
  11. target.move()
  12. target.boundaries()
  13. target.display()
  14. pygame.display.update()

至:

  1. #initial screen draw:
  2. screen.blit(background,0))
  3. pygame.display.update()
  4. while True:
  5. #remove prevIoUs drawn sprites and replaces with background:
  6. my_particles.clear(screen,background)
  7. MouseP = pygame.mouse.get_pos()
  8. frames = Clock.get_fps
  9. pygame.mouse.set_visible
  10. score = (pygame.time.get_ticks()/1000)
  11. print (score)
  12. print (MouseP)
  13. for target in my_particles:
  14. target.move()
  15. target.boundaries()
  16. #draws changed sprites to the screen:
  17. pygame.display.update(my_particles.draw(screen))

删除不再需要的显示方法.

这也比你早期的代码运行得快得多,因为绘制内容所花费的时间与绘图区域的大小成正比,之前它每次都绘制整个背景 – 现在它只绘制精灵并更改为背景!

希望这可以帮助 :)

猜你在找的Python相关文章