雪碧移动游戏python

时间:2018-01-03 22:43:52

标签: python-2.7 inheritance pygame

我正在为python(tutorial)做一个http://programarcadegames.com/,我被困在实验室14:Sprite Moving。

链接页面有步骤,我坚持第5步。具体来说,我认为我的 GoodBlock 类有问题。我认为我从我的Block类中继承了错误,我也按照说明进行了操作。我正在使用Python 2.7,我很确定我使用了超级功能,但我不确定。当我运行我的代码时,我想通过我的箭头键显示绿色,红色和蓝色精灵。使用 GoodBlock 类,假设覆盖更新方法(我认为?)并且还添加好(绿色)精灵的随机移动。

然而,当我用GoodBlock()替换Block()类的所有实例时,我只会显示我的播放器精灵,并且它似乎正在应用来自GoodBlock()的update()函数

这是我的所有代码。这里是prorgam在此步骤之前工作的一个例子。这是精灵和声音文件的link

#********************************************
# Latest working version of collect block game with sprite graphics
#Seperated into multiple files. block_library is my class
#
#lab14v1.py
#********************************************

import pygame
import random
#from block_library import *
import block_library
import goodblock_library



# Define some colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
RED      = ( 255,   0,   0)





# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])

pygame.display.set_caption("Collect Blocks")


#drawing text on the screen, selecting font to use
font = pygame.font.SysFont('Calibri', 25, True, False)




# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
block_list = pygame.sprite.Group()

# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()

bad_block_list = pygame.sprite.Group()



for i in range(50):
    # This represents a block
    block = block_library.Block("goodBlk.png")

    #block = goodblock_library.GoodBlock("goodBlk.png")

    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)


    badBlock = block_library.Block("badBlk.png")
    #badBlock = goodblock_library.GoodBlock("badBlk.png")
    badBlock.rect.x = random.randrange(screen_width)
    badBlock.rect.y = random.randrange(screen_height)

    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)

    bad_block_list.add(badBlock) #adds badBlock to list of blocks for collision
    all_sprites_list.add(badBlock) #adds badBlocks to list of all blocks



# Create a RED player block
player = block_library.Block("playerBlk.png" )
#player = goodblock_library.GoodBlock("playerBlk.png")
all_sprites_list.add(player)

#Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

score = 0


#for sound:

good_sound = pygame.mixer.Sound("good_blockSnd.wav")

bad_sound = pygame.mixer.Sound("bad_blockSnd.wav")

#col_sound = pygame.mixer.Sound("bumpSnd.wav")




# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop

            # --- Game logic should go here
            # Set the speed based on the key pressed
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, -3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, 3)

        # Reset speed when key goes up (if not included it goes off in one direction)
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, 3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, -3)    




    # Clear the screen
    screen.fill(WHITE)





    # See if the player block has collided with anything.
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True) 





    blocks_hit_list2 = pygame.sprite.spritecollide(player, bad_block_list, True)

    # Check the list of collisions.
    for block in blocks_hit_list:
        score += 1
        #print( score )
        good_sound.play()




    for block in blocks_hit_list2:
        score = score - 1
        bad_sound.play()
        #print(score) #this will print to console,
        # Note: This line creates an image of the letters,
        # but does not put it on the screen yet.




    #to display score on screen:    
    # Note: This line creates an image of the letters,
    # but does not put it on the screen yet. 
    text = font.render("Score:" + str(score), True,BLACK)
    # Put the image of the text on the screen at 250x250
    screen.blit(text, [250, 250])


    # Draw all the spites using the Group class method draw, to loop through each sprite in list (all_sprite_list) and call draw
    all_sprites_list.draw(screen)

    #update sprites speed
    all_sprites_list.update()    





    # Limit to 60 frames per second
    clock.tick(60)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

pygame.quit()

。 。

    # -*- coding: utf-8 -*-
"""
Block class for sprites. block_library.py

"""
import pygame
import pygame.mixer
import random


# Define some colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
RED      = ( 255,   0,   0)


#initizlize mixer for sound
pygame.mixer.init(44100, -16, 2, 2048)

#define sound for wall coll
col_sound = pygame.mixer.Sound("bumpSnd.wav")


# This class represents the ball        
# It derives from the "Sprite" class in Pygame
class Block(pygame.sprite.Sprite):

    # READ BEFORE USING:
    # This constructor lets you use any graphic:
    # my_sprite = Block("any_graphic.png")
    # But if you DON'T want any graphic, use the following instead:

    def __init__(self, filename):
        # Call the parent class (Sprite) constructor
        #super().__init__() 
        super(Block, self).__init__()

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.image.load(filename).convert()

        # Set background color to be transparent. Adjust to WHITE if your
        # background is WHITE.
        self.image.set_colorkey(BLACK)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values 
        # of rect.x and rect.y
        self.rect = self.image.get_rect()

        self.change_x = 0
        self.change_y = 0


        #class method to have speed/position for block
    def changespeed(self, x, y):
        self.change_x = self.change_x + x
        self.change_y = self.change_y + y

    #update rectangle object with position/speed of block
    def update(self):
        self.rect.x = self.change_x + self.rect.x
        self.rect.y = self.change_y + self.rect.y 

        #boundry check, plays sound when colides at screen.
        if self.rect.x > 670:
            self.rect.x = 670
            col_sound.play()
        if self.rect.x < 0:
            self.rect.x = 0
            col_sound.play()
        if self.rect.y < 0:
            self.rect.y = 0
            col_sound.play()
        if self.rect.y > 370:
            self.rect.y = 370
            col_sound.play()

。 。

    #GoodBlock class , goodblock_library.py
import block_library

import random

# Define some colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
RED      = ( 255,   0,   0)



class GoodBlock(block_library.Block):
    def __init__(self, filename):
        super(GoodBlock, self).__init__(filename)
        # Call the parent/super class constructor first 
        # READ BEFORE USING:
    # This constructor lets you use any graphic:
    # my_sprite = Block("any_graphic.png")
    # But if you DON'T want any graphic, use the following instead:

    def changespeed(self, x, y):
        super(GoodBlock,self).changespeed(x,y)


    #update rectangle object with position/speed of block
    def update(self):
        #self.rect.x = self.change_x + self.rect.x
        #self.rect.y = self.change_y + self.rect.y 
        super(GoodBlock, self).update()

        self.rect.x = random.randrange(-3, 3)
        self.rect.y = random.randrange(-3, 3)

1 个答案:

答案 0 :(得分:1)

您可以在GoodBlock方法中将每个帧的update s的位置设置为-3到3之间的新随机值(不包括)。

self.rect.x = random.randrange(-3, 3)
self.rect.y = random.randrange(-3, 3)

这意味着他们被放置在立即收集他们的玩家身上。

如果你想让块摇晃,你可以增加x和y坐标:

self.rect.x += random.randrange(-3, 4)
self.rect.y += random.randrange(-3, 4)

为了让它们移动得更好,我会实现一个计时器(在这个例子中只是一个帧计数器)并在达到时间限制后改变精灵的速度:

class GoodBlock(Block):
    def __init__(self, image):
        super(GoodBlock, self).__init__(image)
        self.velocity_x = random.randrange(-1, 2)
        self.velocity_y = random.randrange(-1, 2)
        self.counter = 0

    def update(self):
        super(GoodBlock, self).update()
        self.counter += 1
        if self.counter >= 60:  # Every 60 frames.
            self.counter = 0
            # Change the velocity.
            self.velocity_x = random.randrange(-1, 2)
            self.velocity_y = random.randrange(-1, 2)

        self.rect.x += self.velocity_x
        self.rect.y += self.velocity_y

实际上,我会使用vectors来获得更好的动作。 pygame.Rect坐标只能用整数递增。