为什么我的pygame精灵不会独立移动?我如何让他们独立行动?

时间:2015-03-24 21:31:58

标签: python pygame

只有1个兰花精灵,它会或多或少地在屏幕周围反弹。当我添加更多时,兰花精灵作为单个质量一起移动并从边界反弹,即使它们没有接近它。这是代码:

#Import and Init
import pygame
import random
from pygame.locals import *
pygame.init()

#Set Up the Window
width = 1240
height = 700
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pollination!")

background = pygame.Surface(screen.get_size())
background = pygame.image.load("Forrest.jpg")
background = background.convert()

#Load and Convert the wasp
wasp = pygame.sprite.Sprite()
wasp.image = pygame.image.load("wasp1.gif")
wasp.rect = wasp.image.get_rect()
wasp_group = pygame.sprite.GroupSingle(wasp)

flowers = pygame.sprite.OrderedUpdates()

for i in range(5):
    orchid = pygame.sprite.Sprite()
    orchid.image = pygame.image.load("orchid1.gif")
    orchid.rect = orchid.image.get_rect()
    orchid.rect.right = random.randint(0, 1240)
    orchid.rect.top = random.randint(0, 700)
    flowers.add(orchid)

buzz = pygame.mixer.Sound("buzz.wav")
#Clock and Loop Variables
framerate = pygame.time.Clock()
GameGo = True

orchid_xinc = 2
orchid_yinc = 2

wasp_move = 5
#The Main Loop
while GameGo:

    #Tick the Clock
    framerate.tick(60)

    #Keyborad Keypress Events, Movement

    if pygame.key.get_pressed()[K_UP]:
        wasp.rect.top -= wasp_move
        if wasp.rect.top < -30:
            wasp.rect.top = -30
    if pygame.key.get_pressed()[K_DOWN]:
        wasp.rect.top += wasp_move
        if wasp.rect.top > 625:
            wasp.rect.top = 625
    if pygame.key.get_pressed()[K_LEFT]:
        wasp.rect.right -= wasp_move
        if wasp.rect.right < 110:
            wasp.rect.right = 110
    if pygame.key.get_pressed()[K_RIGHT]:
        wasp.rect.right += wasp_move
        if wasp.rect.right > 1255:
            wasp.rect.right = 1255

    for orchid in flowers:
        if orchid.rect.top < -30:
            orchid_yinc *= -1
        if orchid.rect.top > 625:
            orchid_yinc *= -1
        if orchid.rect.right < 110:
            orchid_xinc *= -1
        if orchid.rect.right > 1255:
            orchid_xinc *= -1

        orchid.rect.right += orchid_xinc
        orchid.rect.top -= orchid_yinc

    for orchid in pygame.sprite.groupcollide(wasp_group, flowers, False, True):
        buzz.play()
##  #Blit our Images
    screen.blit(background, (0, 0))
    flowers.draw(screen)
    wasp_group.draw(screen)
    pygame.display.update()


  #Handle a Close Event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            GameGo = False

pygame.quit()

1 个答案:

答案 0 :(得分:0)

也许是你输入&#34; orchid _ ####&#34;在你的for循环中更新每个兰花。如果在你的兰花类中, init 方法定义了一个自我。####应该如此,它用&#34;对象调用。####&#34;而不是一个下划线。如果我错了,请查看&#34; flowers.draw&#34;功能,确保它使用各自的坐标绘制所有兰花。