简体   繁体   中英

Why don't my pygame sprites move about independently? And how do I make them move about independently?

With only 1 orchid sprite, it bounces around the screen more or less as I intend it to. When I add more, the orchid sprites move around together as a single mass and bounce off a boundary even though they don't come near to touching it. Here is the code:

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

Perhaps its the fact that you type "orchid_####" in your for loop that updates each orchid. If in your orchid class the init method defines a self.#### as it should, its called with "Object.####" and not an underscore. If i am wrong, check the "flowers.draw" function, making sure it draws all orchids using their own respective coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM