2012-05-05 5 views
5

まず、私はオンラインとこのウェブサイトで解決策を探しています。私が試したものは動作していないので、私は個人的な質問とコードを投稿することにしました。このプログラムは、Python 3.2.2とPygameの対応する互換バージョンを使用して作成されました。私はまた、より効率的な方法は、スプライト、スプライトグループと 'ダーティrect'の更新を使用することがわかったが、私はプログラムを変換することができないので、私はそのような機能の追加の利点なしで続行します。ソフトウェアデザインと開発専攻:Pygame Smudge Trails

問題:「小惑星」が動いているスマッジゲートが残っています。仮説:背景は画面上にブリッジされますが、小惑星はバックグラウンドにブリットされます。

返信してください - ところで、私は、AUSからhighschoolerよ: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() 
+2

+1高校 –

答えて

4

は基本的に、あなたは正しいです!サークルは背景に直接描画され、新しいサークルが描画されるたびに古いサークルが残ります。汚れや跡が生じます。

drawメソッドでbackgroundscreenに変更するだけで済みます。 これで修正されます。

しかし、実際にはSpriteクラスを使用することをお勧めします。私はコードをいくつか変更して、それをあなたのために切り替えました。

を上部付近にこれを追加します:

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

#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) 

asteroid__init__方法にこれを追加それはトレイル:)ここ

なく実行されますこれらの変更により変更とexplainationsがあります

これを末尾に追加します。def move(self):

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

変更:

my_particles = [] 

へ:

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

変更:

my_particles.append(target) 

へ:

my_particles.add(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() 

へ:

#initial screen draw: 
screen.blit(background, (0,0)) 
pygame.display.update() 
while True: 
    #remove previous drawn sprites and replaces with background: 
    my_particles.clear(screen, background) 
    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() 
    #draws changed sprites to the screen: 
    pygame.display.update(my_particles.draw(screen)) 

それはもはや必要とされてdisplay方法を削除しません。

何かを描画するのにかかる時間が描画領域のサイズに比例し、以前は常に背景全体を描画していたため、以前のコードよりも高速に実行されます。背景に変更!

ホープ、このことができます:)

+0

にこのようなものの上に作業するためには、あなたは命の恩人です、どうもありがとうございます。このような素晴らしいコミュニティを期待したことはありません。また、あなたがコードに配置したスプライトグループはどこですか、私はそれが "my_particles.add(ターゲット)"か "my_particles = pygame.sprite.RenderUpdates()"かどうかはわかりません。私はそれを理解する必要があるので、私は衝突に移動することができます。 @fraxel – AnjewGS

+1

@AnjewGS - ありがとう。私があなたの質問を正しく理解していれば、あなたのリスト:my_particlesをpygameクラスに変更しました: 'pygame.sprite.RenderUpdates()'は事実上、すべての繰り返しを再描画する必要のあるスプライトのセットです。 'my_particles.add(target)'行はあなたの小惑星をこのセットに追加します(空になるので、 'add'は' append'のようなものですが、セットについてはあなたが調べるべきセットがわからないならそれらを..)。 – fraxel

+0

okありがとう、もう一度ありがとう。私はこの "pygame.sprite.groupcollide"の権利を使うことができますか? – AnjewGS