1

このコードを見てください(彼女は少し長いかもしれません):</ p>

#Begin
import pygame
from pygame.locals import *

global x, y, picPath
x, y, picPath = 100, 100, 'C:\\Pic\\'

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(picPath + 'boll.png')
        self.image.set_colorkey(0xffffff, 0)
        self.rect = self.image.get_rect()
    def update(self, pos):
        screen.blit(self.image, pos)

class Stone(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(picPath + 'stone.jpg')
        self.rect = self.image.get_rect()
        self.num = 0
        self.up = 5
    def update(self):
        if(self.up != 370)and(self.num == 0):
            self.up += 5
        elif(self.up == 0)and(self.num == 1):
            self.up += 5
            self.num = 0
        else:
            self.up -= 5
            self.num = 1
        screen.blit(self.image, (305, self.up))

class GameStart:
    def __init__(self):
        self.life = 405
        self.bg = pygame.image.load(picPath + 'bg.jpg')
        self.lf = pygame.image.load(picPath + 'life.jpg')
        player1 = pygame.sprite.Group()
        player2 = pygame.sprite.Group()
        self.cB = Ball()
        self.cS = Stone()
        player1.add(self.cB)
        player2.add(self.cS)
        self.collide = pygame.sprite.groupcollide(player1, player2, 1, 0)
    def update(self):    
        if self.collide:
            print 'Yoshi!' 
        screen.blit(self.bg, (0, 0))
        self.cB.update((x, y))
        self.cS.update()
        screen.blit(self.lf, (150, self.life))

    def start(self):
        # This is Start!
        print 'Hello'

pygame.init()
screen = pygame.display.set_mode([700,460])
clock = pygame.time.Clock()

game = GameStart()
game.start()

while True:
    clock.tick(50)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()

    pygame.display.update()
    game.update()

    kname = pygame.key.get_pressed()
    if kname[pygame.K_LEFT]:
        if x > 0: x = x - 5
    elif kname[pygame.K_RIGHT]:
        if x < 700: x = x + 5
    elif kname[pygame.K_UP]:
        if y > 0: y = y - 5
    elif kname[pygame.K_DOWN]:
        if y < 460: y = y + 5
#End

なぜ彼女は「ヨッシー」を印刷し続けるのですか?ボールとストーンの衝突を一度だけ印刷したいと思います。(私の英語は下手です)

4

1 に答える 1

0

groupcollideは一度だけ実行されます。入居groupcollideしてみるupdate

class GameStart:
    def __init__(self):
        self.life = 405
        self.bg = pygame.image.load(picPath + 'bg.jpg')
        self.lf = pygame.image.load(picPath + 'life.jpg')
        self.player1 = pygame.sprite.Group()
        self.player2 = pygame.sprite.Group()
        self.cB = Ball()
        self.cS = Stone()
        self.player1.add(self.cB)
        self.player2.add(self.cS)
    def update(self):    
        if pygame.sprite.groupcollide(self.player1, self.player2, 1, 0):
            print 'Yoshi!' 
        screen.blit(self.bg, (0, 0))
        self.cB.update((x, y))
        self.cS.update()
        screen.blit(self.lf, (150, self.life))

また、ゲーム ロジックは、表示が既に描画された後に更新されます。何らかの理由でその動作が必要でない限りpygame.display.update、メイン ループで最後に呼び出すことができます。

game.update()

kname = pygame.key.get_pressed()
if kname[pygame.K_LEFT]:
    if x > 0: x = x - 5
elif kname[pygame.K_RIGHT]:
    if x < 700: x = x + 5
elif kname[pygame.K_UP]:
    if y > 0: y = y - 5
elif kname[pygame.K_DOWN]:
    if y < 460: y = y + 5

pygame.display.update()
于 2013-01-17T05:28:59.113 に答える