Python Snakes游戏:1类2种颜色

时间:2017-01-20 10:41:47

标签: python

我目前正在制作一个小蛇游戏,并且在尝试编写苹果时遇到困难。我使用了一个名为 <div class="row"> <div class="col-md-6"> <img src="http://i.imgur.com/Ym35iJI.jpg" alt="frontend" class="img-thumbnail" style="height: 500px;background-size: cover;"/> </div> <div class="col-md-6"> <img src="http://i.imgur.com/ot6RubY.jpg" alt="webd" class="img-thumbnail" style="height: 500px;background-size: cover;"/> </div> 的类,它在其构造函数(Segment)中定义了段颜色。

但是,我也希望将__init__类用于苹果。我将它用于Segment,现在我希望它用于snake_segments。我这样做了,但颜色仍然是白色,我不知道如何让它变成绿色,同时仍然使蛇变白。

apple_segments

1 个答案:

答案 0 :(得分:2)

根据Peter Wood的说法,您可以修改Segment以接受颜色作为其参数之一:

class Segment(pygame.sprite.Sprite):
    def __init__(self, x, y, color):
        super().__init__()

        self.image = pygame.Surface([segment_width, segment_height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

你也可以为蛇和苹果片段创建这个的子类:

class SnakeSegment(Segment):
    def __init__(self, x, y):
        super().__init__(x, y, WHITE)


class AppleSegment(Segment):
    def __init__(self, x, y):
        super().__init__(x, y, GREEN)

在代码的其余部分中,您确保构成蛇的所有段的实例都是SnakeSegments,而代表苹果的段的实例是AppleSegment的实例:

snake_segments = []
for i in range(3):
    x = 400 - (segment_width + segment_margin) * 1
    y = 200
    segment = SnakeSegment(x, y)
    snake_segments.append(segment)
    allspriteslist.add(segment)

apple_segments = []
for i in range(1):
    x = 200 - (segment_width + segment_margin) * 1
    y = 100
    segment = AppleSegment(x, y)
    apple_segments.append(segment)
    allspriteslist.add(segment)

    ...

 segment = SnakeSegment(x, y)

snake_segments.insert(0, segment)
allspriteslist.add(segment)

这是我开始游戏时所看到的:

作为参考,这是我正在运行的修改代码的整个部分:

import pygame
import random
import time

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)

segment_width = 16
segment_height = 16
segment_margin = 4

x_change = segment_width + segment_margin
y_change = 0


class Segment(pygame.sprite.Sprite):
    def __init__(self, x, y, color):
        super().__init__()

        self.image = pygame.Surface([segment_width, segment_height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def draw(self, screen):
        screen_rect = screen.get_rect()
        screen.blit(self.image, self.rect)

        if self.rect.left < screen_rect.left:
            temp_rect = self.rect.copy()
            temp_rect.x += screen_rect.width
            screen.blit(self.image, temp_rect)
        elif self.rect.right > screen_rect.right:
            temp_rect = self.rect.copy()
            temp_rect.x -= screen_rect.width
            screen.blit(self.image, temp_rect)

        if self.rect.top < screen_rect.top:
            temp_rect = self.rect.copy()
            temp_rect.y += screen_rect.height
            screen.blit(self.image, temp_rect)
        elif self.rect.bottom > screen_rect.bottom:
            temp_rect = self.rect.copy()
            temp_rect.y -= screen_rect.height
            screen.blit(self.image, temp_rect)


class SnakeSegment(Segment):
    def __init__(self, x, y):
        super().__init__(x, y, WHITE)


class AppleSegment(Segment):
    def __init__(self, x, y):
        super().__init__(x, y, GREEN)


pygame.init()

screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()

pygame.display.set_caption('Snakes')

allspriteslist = pygame.sprite.Group()

snake_segments = []
for i in range(3):
    x = 400 - (segment_width + segment_margin) * 1
    y = 200
    segment = SnakeSegment(x, y)
    snake_segments.append(segment)
    allspriteslist.add(segment)

apple_segments = []
for i in range(1):
    x = 200 - (segment_width + segment_margin) * 1
    y = 100
    segment = AppleSegment(x, y)
    apple_segments.append(segment)
    allspriteslist.add(segment)

clock = pygame.time.Clock()
done = False

while not done:

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

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x_change = (segment_width + segment_margin) * -1
            y_change = 0
        if event.key == pygame.K_RIGHT:
            x_change = (segment_width + segment_margin)
            y_change = 0
        if event.key == pygame.K_UP:
            x_change = 0
            y_change = (segment_height + segment_margin) * -1
        if event.key == pygame.K_DOWN:
            x_change = 0
            y_change = (segment_height + segment_margin)

    old_segment = snake_segments.pop()
    allspriteslist.remove(old_segment)

    x = snake_segments[0].rect.x + x_change
    y = snake_segments[0].rect.y + y_change

    if x < screen_rect.left:
        x += screen_rect.width
    elif x + segment_width + segment_margin > screen_rect.right:
        x -= screen_rect.width

    if y < screen_rect.top:
        y += screen_rect.height
    elif y + segment_height + segment_margin > screen_rect.bottom:
        y -= screen_rect.height

    segment = SnakeSegment(x, y)

    snake_segments.insert(0, segment)
    allspriteslist.add(segment)

    screen.fill(BLACK)

    # allspriteslist.draw(screen)

    for x in allspriteslist:
        x.draw(screen)

    pygame.display.flip()

    clock.tick(5)

    pygame.quit()