2012-05-05 60 views
5

首先,我在網上搜索與本網站的解決方案,我已經試過的人沒有工作,所以我決定後我的個人問題和代碼。這個程序是使用Python 3.2.2和相應的pygame兼容版本創建的。我也意識到一個更有效的方法是使用精靈,精靈組和'髒rect'更新,但我無法轉換程序,所以我會繼續沒有這些功能的附加好處。軟件設計和開發專業:Pygame的塗抹步道

問題:塗抹落後其中「小行星」被移動被留了下來。
假說:背景被位塊傳輸到屏幕上,不過小行星位塊傳輸到背景。

請回復 - 順便說一句我是從澳大利亞一個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方法中將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) 

一下添加到asteroid__init__方法:

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

結束
 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

對這個東西的工作非常感謝你,你是一個救星。這樣詳細的答案我從來沒有想到這麼好的社區。另外你在代碼中放置的Sprite組在哪裏,我不確定它是「my_particles.add(target)」還是「my_particles = pygame.sprite.RenderUpdates()」。我需要弄明白,所以我可以進入碰撞。 @fraxel – AnjewGS

+1

@AnjewGS - 謝謝:)。如果我正確理解你的問題,......我將你的列表變成了:'my_particles'變成了一個pygame類:'pygame.sprite.RenderUpdates()',這實際上是一組我們可能需要重繪每一次迭代的精靈。 'my_particles.add(target)'這一行添加了你的小行星到這個集合中(因爲它開始是空的,'add',有點像'append',但是對於集合 - 如果你不知道你應該檢查哪些集合他們出來..)。 – fraxel

+0

好酷,再次感謝。我可以使用這個「pygame.sprite.groupcollide」嗎? – AnjewGS