如何从Sprite中的单独类中获取信息

问题描述 投票:0回答:1

我试图在Pygame的类似突破游戏中为每种不同的颜色获得不同的分数。我知道我需要做的是,我已经设置了self.points,并为每种不同的颜色分配了一个点值。唯一的问题是,我不知道如何将信息从Blocks类传递到gameloop。我想我需要将Sprite中的所有代码转换为不同文件中的Object类。我在下面标记了要点。任何帮助,将不胜感激。

import math
import pygame

pygame.init()

pygame.mixer.music.load('Breakout/BGmusic.mp3')
pygame.mixer.music.set_volume(0.0) ######################## CHANGE BACK TO 0.1
pygame.mixer.music.play(-1) 

black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 153, 255)
yellow = (252, 226, 5)
green = (63, 128, 70)
orange = (218, 165, 32)
red = (178, 34, 34)

block_width = 28
block_height = 5.5

returnBall = pygame.mixer.Sound('Breakout/ReturnBall.wav')
offWall = pygame.mixer.Sound('Breakout/OffWall.wav')
brickBreak = pygame.mixer.Sound('Breakout/BrickBreak.wav')



'''

hsFile = 'highscore.txt'

with open('highscore.txt', 'w') as f:
    try:
        self.highscore = int(f.read())
    except:
        self.highscore = 0

if self.score > self.highscore:
    self.highscore = self.score
    font = pygame.font.Font('Breakout/font.ttf', 32) ############## HIGHSCORE FONT
    text = font.render('New Highscore', True, white)
    textpos = text.get_rect(centerx=background.get_width()/2)
    textpos.top = 300
    screen.blit(text, textpos)
    with open('highscore.txt', 'w') as f:
        f.write(str(self.score))
else:
    font = pygame.font.Font('Breakout/font.ttf', 32) ############## HIGHSCORE FONT
    text = font.render(str(self.highscore), True, white)
    screen.blit(text, (380, 10))

'''





'''
def gameintro():
    intro = True

    while intro:
        game.display.fill(black)

        text = font.render('Breakout/font.ttf', True, white)
        textpos = text.get_rect(centerx=background.get_width()/2)
        textpos.top = 300
        screen.blit(text, textpos)

        font = pygame.font.Font('Breakout/font.ttf', 32)
        text = font.render('Press Spacebar To Play', True, white)
        screen.blit(text, (0, 0))

        if event.type == pygame.KEYDOWN:
            keys = pygame.key.get_pressed()
            if keys[pygame.SPACEBAR]:
                gameloop()
'''


class Block(pygame.sprite.Sprite):

    def __init__(self, color, x, y, points):

        super().__init__()

        self.points = points

        self.image = pygame.Surface([block_width, block_height])

        self.image.fill(color)

        self.rect = self.image.get_rect()

        self.rect.x = x
        self.rect.y = y


class Ball(pygame.sprite.Sprite):

    speed = 12.0

    x = 210
    y = 550

    direction = 200

    width = 8
    height = 8

    def __init__(self):

        super().__init__()

        self.image = pygame.Surface([self.width, self.height])

        self.image.fill(white)

        self.rect = self.image.get_rect()

        self.screenheight = pygame.display.get_surface().get_height()
        self.screenwidth = pygame.display.get_surface().get_width()

    def bounce(self, diff):

        self.direction = (180 - self.direction) % 360
        self.direction -= diff

    def update(self):
        direction_radians = math.radians(self.direction)

        self.x += self.speed * math.sin(direction_radians)
        self.y -= self.speed * math.cos(direction_radians)

        self.rect.x = self.x
        self.rect.y = self.y

        if self.y <= 0:
            offWall.play() 
            self.bounce(0)
            self.y = 1

        if self.x <= 0:
            offWall.play() 
            self.direction = (360 - self.direction) % 360
            self.x = 1

        if self.x > self.screenwidth - self.width:
            offWall.play() 
            self.direction = (360 - self.direction) % 360
            self.x = self.screenwidth - self.width - 1

        if self.y > 600:
            return True
        else:
            return False


class Player(pygame.sprite.Sprite):

    def __init__(self):

        super().__init__()

        self.width = 50
        self.height = 6
        self.image = pygame.Surface([self.width, self.height])
        self.image.fill((blue))

        self.rect = self.image.get_rect()
        self.screenheight = pygame.display.get_surface().get_height()
        self.screenwidth = pygame.display.get_surface().get_width()

        self.rect.x = 190
        self.rect.y = self.screenheight-self.height

    def update(self):
        speed = 10
        if event.type == pygame.KEYDOWN:
            keys = pygame.key.get_pressed()
            if keys[pygame.K_RIGHT]:
                self.rect.x += speed
            if keys[pygame.K_LEFT]:
                self.rect.x -= speed
        if self.rect.x > self.screenwidth - self.width:
            self.rect.x = (self.screenwidth - self.width)
        if self.rect.x < 0:
            self.rect.x = 0






pygame.init()

screen = pygame.display.set_mode([446, 565])

pygame.display.set_caption('PyGame Breakout')

font = pygame.font.Font(None, 36)

background = pygame.Surface(screen.get_size())

blocks = pygame.sprite.Group()
balls = pygame.sprite.Group()
allsprites = pygame.sprite.Group()

player = Player()
allsprites.add(player)

ball = Ball()
allsprites.add(ball)
balls.add(ball)

top = 80

blockcount = 14

for row in range(8):
    for column in range(0, blockcount):
        block1 = Block(red, column * (block_width + 4) + 1, 80, 7)
        block2 = Block(red, column * (block_width + 4) + 1, 88, 7)
        block3 = Block(orange, column * (block_width + 4) + 1, 96, 5)
        block4 = Block(orange, column * (block_width + 4) + 1, 104, 5)
        block5 = Block(green, column * (block_width + 4) + 1, 112, 3)
        block6 = Block(green, column * (block_width + 4) + 1, 120, 3)
        block7 = Block(yellow, column * (block_width + 4) + 1, 128, 1)
        block8 = Block(yellow, column * (block_width + 4) + 1, 136, 1)
        blocks.add(block1, block2, block3, block4, block5, block6, block7, block8)
        allsprites.add(block1, block2, block3, block4, block5, block6, block7, block8)

    top += block_height + 1

score = 0 

clock = pygame.time.Clock()

game_over = False

exit_program = False

while not exit_program:

    clock.tick(30)

    screen.fill(black)

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

    if not game_over:
        font = pygame.font.Font('Breakout/font.ttf', 32)
        text = font.render(str(score), True, white)
        screen.blit(text, (10, 10))


        #font = pygame.font.Font('Breakout/font.ttf', 32) ############## HIGHSCORE FONT
        #text = font.render(str(self.highscore), True, white)
        #screen.blit(text, (380, 10))

        player.update()
        game_over = ball.update()

    if game_over:
        text = font.render("Game Over", True, white)
        textpos = text.get_rect(centerx=background.get_width()/2)
        textpos.top = 300
        screen.blit(text, textpos)
        pygame.mixer.music.set_volume(0)

    if pygame.sprite.spritecollide(player, balls, False):
        returnBall.play() 
        diff = (player.rect.x + player.width/2) - (ball.rect.x+ball.width/2)
        ball.rect.y = screen.get_height() - player.rect.height - ball.rect.height - 1
        ball.bounce(diff)

    deadblocks = pygame.sprite.spritecollide(ball, blocks, True)

    if len(deadblocks) > 0:
        brickBreak.play()
        score += 1 #################### NEEDS TO BE SELF.POINTS FROM BLOCK
        ball.bounce(0)

        if len(blocks) == 0:
            game_over = True
            pygame.mixer.music.set_volume(0)

    allsprites.draw(screen)

    pygame.display.flip()

pygame.quit()
python class pygame sprite
1个回答
0
投票

您需要带有fordeadblocksblock.points循环

#if len(deadblocks) > 0:
if deadblocks:
    brickBreak.play()

    for block in deadblocks:
        score += block.points 

    ball.bounce(0)
© www.soinside.com 2019 - 2024. All rights reserved.