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

Python 软件设计与开发专业:Pygame Smudge Trails,python,pygame,blit,trail,Python,Pygame,Blit,Trail,首先,我在网上和这个网站上搜索解决方案,我尝试过的解决方案不起作用,所以我决定发布我的个人问题和代码。这个程序是使用Python3.2.2和相应的pygame兼容版本创建的。我还意识到一个更有效的方法是使用精灵、精灵组和“脏rect”更新,但我无法转换程序,因此我将继续使用这些函数的附加好处 问题:留下了“小行星”移动的污迹痕迹。 假设:背景在屏幕上闪烁,而小行星则在背景上闪烁 请回复-顺便说一句,我是来自澳大利亚的高中生:D import pygame import random import

首先,我在网上和这个网站上搜索解决方案,我尝试过的解决方案不起作用,所以我决定发布我的个人问题和代码。这个程序是使用Python3.2.2和相应的pygame兼容版本创建的。我还意识到一个更有效的方法是使用精灵、精灵组和“脏rect”更新,但我无法转换程序,因此我将继续使用这些函数的附加好处

问题:留下了“小行星”移动的污迹痕迹。
假设:背景在屏幕上闪烁,而小行星则在背景上闪烁

请回复-顺便说一句,我是来自澳大利亚的高中生:D

import pygame
import random
import math
pygame.init()

height = 550
width = 750
screen = pygame.display.set_mode((width, height))
background = pygame.image.load("Planet.jpg")
Clock = pygame.time.Clock()


class asteroid(pygame.sprite.Sprite):
    def __init__(self, x, y, size):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.size = 15
        self.speed = 0.0
        self.angle = 0
        self.colour = (171, 130, 255)
        self.thickness = 0

def display(self):
     pygame.draw.circle(background, self.colour, (int(self.x),int(self.y)), self.size, self.thickness)

     pygame.draw.circle(background, (255, 255, 255), (int(self.x),int(self.y)), self.size, 1)

def move(self):
    self.x += math.sin(self.angle) * self.speed
    self.y -= math.cos(self.angle) * self.speed

def boundaries(self):
    if self.x > width - self.size:
        self.x = 0 + self.size
    elif self.x < self.size:
        self.x = width - self.size
    if self.y > height - self.size:
        self.y = 0 + self.size
    elif self.y <self.size:
        self.y = height - self.size




num_target = 5
my_particles = []
num_particles = len(my_particles)
while num_particles < 5:
    for n in range(num_target):
        size = 20
        x = random.randint(size, height - size)
        y = random.randint(size, width - size)
        target = asteroid(x, y, size)
        target.speed = random.uniform(1.0, 1.0)
        target.angle = random.uniform(0, math.pi*2)
        my_particles.append(target)
        num_particles = num_particles + 1


def main():
    pygame.display.set_caption("Anyu's Game")
    screen.blit(background, (0,0))
    pygame.display.update()
    score = (pygame.time.get_ticks()/1000)
    print (score)


while True:
    pygame.display.update()
    screen.blit(background, (0,0))
    MouseP = pygame.mouse.get_pos()
    frames = Clock.get_fps
    pygame.mouse.set_visible
    score = (pygame.time.get_ticks()/1000)
    print (score)
    print (MouseP)
    for target in my_particles:
        target.move()
        target.boundaries()
        target.display()
        pygame.display.update()


    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit();

if __name__=='__main__':
    main()
导入pygame
随机输入
输入数学
pygame.init()
高度=550
宽度=750
screen=pygame.display.set_模式((宽度、高度))
background=pygame.image.load(“Planet.jpg”)
Clock=pygame.time.Clock()
小行星类(pygame.sprite.sprite):
定义初始值(self,x,y,size):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.x=x
self.y=y
自我尺寸=15
自身速度=0.0
自转角=0
self.color=(171130255)
自身厚度=0
def显示(自):
pygame.draw.circle(背景,self.color,(int(self.x),int(self.y)),self.size,self.thickness)
pygame.draw.circle(背景,(255,255,255),(int(self.x),int(self.y)),self.size,1)
def移动(自我):
self.x+=数学sin(self.angle)*self.speed
self.y-=数学cos(self.angle)*self.speed
def边界(自我):
如果self.x>宽度-self.size:
self.x=0+self.size
elif self.x高度-self.size:
self.y=0+self.size

elif self.y基本上,你是对的!圆圈直接绘制在背景上,每次绘制新圆圈时,旧圆圈都会保留下来。导致污迹/痕迹

您可以在
draw
方法中将
background
更改为
screen
这会解决问题。

但是真正值得按预期使用
Sprite
类。我对您的代码做了一些更改,以便为您切换代码。通过这些更改,它运行时没有轨迹:)

以下是变化和解释:

在顶部附近添加以下内容:

#Create a new `pygame.Surface`, and draw a circle on it, then set transparency:
circle = pygame.Surface((30,30))
circle = circle.convert()
pygame.draw.circle(circle, (171, 130, 255), (int(15),int(15)), 15, 0)
circle.set_colorkey(circle.get_at((0, 0)), pygame.RLEACCEL)
将此添加到小行星
初始化
方法中:

#Sets the asteroid image, and then the asteroids co-ords (these are in `rect`)
        self.image = circle
        self.rect = self.image.get_rect()
将此添加到
def move(self)的末尾:

更改:

my_particles = []
my_particles.append(target)
while True:
    pygame.display.update()
    screen.blit(background, (0,0))
    MouseP = pygame.mouse.get_pos()
    frames = Clock.get_fps
    pygame.mouse.set_visible
    score = (pygame.time.get_ticks()/1000)
    print (score)
    print (MouseP)
    for target in my_particles:
        target.move()
        target.boundaries()
        target.display()
        pygame.display.update()
致:

更改:

my_particles = []
my_particles.append(target)
while True:
    pygame.display.update()
    screen.blit(background, (0,0))
    MouseP = pygame.mouse.get_pos()
    frames = Clock.get_fps
    pygame.mouse.set_visible
    score = (pygame.time.get_ticks()/1000)
    print (score)
    print (MouseP)
    for target in my_particles:
        target.move()
        target.boundaries()
        target.display()
        pygame.display.update()
致:

更改:

my_particles = []
my_particles.append(target)
while True:
    pygame.display.update()
    screen.blit(background, (0,0))
    MouseP = pygame.mouse.get_pos()
    frames = Clock.get_fps
    pygame.mouse.set_visible
    score = (pygame.time.get_ticks()/1000)
    print (score)
    print (MouseP)
    for target in my_particles:
        target.move()
        target.boundaries()
        target.display()
        pygame.display.update()
致:

删除
显示
方法,因为不再需要它

这也将比您以前的代码运行得快得多,因为绘制某物所花费的时间与绘图区域的大小成比例,而且以前它每次都绘制整个背景-现在它只绘制精灵和对背景的更改


希望这有帮助:)

这已经有了答案,但这可以替代其他方法。 确保在将图像快速显示到屏幕上时,在快速显示所有内容后翻转显示器。 我会考虑制作一个()函数 像这样:

def draw(self):

    # Blit images
    self.screen.blit(image)

    # Flip display
    pygame.display.flip()
这将在每一帧翻转显示,然后绘制下一帧而不带轨迹

另外,请记住执行
image=pygame.image.load(image.convert)或.convert_alpha()
否则在添加更多图像后,游戏会变慢。
另外,如果你将pygame导入为pg,你不必每次都输入pygame,你只需输入PgGame,就可以了。谢谢你,你是一个救星。如此详细的答案,我从未想过会有这么好的社区。另外,您在代码中放置的精灵组在哪里,我不确定它是“my_particles.add(target)”还是“my_particles=pygame.sprite.RenderUpdates()”。我需要弄清楚,这样我就可以进行碰撞了@fraxel@AnjewGS-谢谢:)。如果我正确理解你的问题,。。我将您的列表:
my_particles
更改为pygame类:
pygame.sprite.RenderUpdates()
,这实际上是一组精灵,我们可能需要在每次迭代中重新绘制。行
my_particles.add(target)
将您的小行星添加到此集合中(因为它一开始是空的,
add
,有点像
append
,但是对于集合-如果您不知道集合是什么,您应该检查它们)。好的,再次感谢。我可以用这个“pygame.sprite.groupcollide”对吗?@AnjewGS-最好的办法就是试试
groupcollide
用于查看两个组中的哪些成员发生冲突。这里我们只有一个组(目前),因此您可以对每个小行星使用
spritecollide
。。也就是说,把它放在你的
循环的最末端,但是你必须首先做:
我的粒子。移除(目标)
,然后,
pygame.sprite.spritecollide(目标,我的粒子,True)
然后
我的粒子。添加(目标)
。因为否则它会报告每个小行星和它自己之间的碰撞。。。试着打印这些东西的输出,并了解发生了什么。。