在蛇游戏中碰撞后如何使精灵移动到其他位置

时间:2019-02-13 06:41:37

标签: python pygame

目前,我还是STILL,正在为我的python类开发一个最终项目,我希望这样做,以便每当蛇在“薄薄的薄荷糖”上跑过时,程序将先打印“ yeet yah”,然后打印薄薄的薄荷糖。将移至差异位置。

我有一个生成食物位置的函数,它输出一个列表,我将其索引到该列表中,以获取该特定食物的x和y坐标

import pygame
import random
import time
import sys

# Window size
WINDOW_WIDTH=400
WINDOW_HEIGHT=400

pygame.init()
WINDOW  = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), 
pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE )
SPRITES = pygame.sprite.Group()

pygame.display.set_caption("Picnic!")
HEAD_IMAGE = pygame.image.load('morrison.png').convert_alpha()
BODY_IMAGE = pygame.image.load('morrison.png').convert_alpha()
FOOD1_IMAGE= pygame.image.load("thinMint.png").convert_alpha()
FOOD2_IMAGE = pygame.image.load("cookie.png").convert_alpha()
FOOD3_IMAGE = pygame.image.load("triscuit.png").convert_alpha()
clock = pygame.time.Clock()

BLACK = (0,0,0)

STARTUP_MS = int(time.time() * 1000.0)  # Epoch time programme 
started
NOW_MS= 0  # miliseconds since we started


def generateFoodPosition():
    randFoodX1 = random.randrange(1, 40, 1) * 10
    randFoodY1 = random.randrange(1, 40, 1) * 10
    randFoodX2 = random.randrange(1, 40, 1) * 10
    randFoodY2 = random.randrange(1, 40, 1) * 10
    randFoodX3 = random.randrange(1, 40, 1) * 10
    randFoodY3 = random.randrange(1, 40, 1) * 10
    if randFoodX1 == randFoodX2 or randFoodY1 == randFoodY2 or 
randFoodY2==randFoodY3 or randFoodY1== randFoodY3 or randFoodX2 == 
randFoodX3 or randFoodX3 == randFoodX1:
        generateFoodPosition()
    else:
        return [randFoodX1, randFoodY1, randFoodX2, randFoodY2, 
randFoodX3, randFoodY3]

class ThinMintSprite(pygame.sprite.Sprite):
    def __init__(self, food1_image, position1):
        pygame.sprite.Sprite.__init__(self)
        self.image = food1_image
        self.rect = self.image.get_rect()
        self.rect.center = position1
    def getRect(self):
        return self.rect
    def checkForConsumption(self, snake):
        if self.rect.colliderect(snake):
            print("yeet yah")
            foodPosition[0] = random.randrange(1, 40, 1) * 10
            foodPosition[1] = random.randrange(1, 40, 1) * 10


class ThinMint():
    global SPRITES
    def __init__(self):
        self.food = ThinMintSprite(FOOD1_IMAGE, (foodPosition[0], 
foodPosition[1]))
    self.foodWidth = self.food.image.get_rect().width
    SPRITES.add(self.food)

class CookieSprite(pygame.sprite.Sprite):
    def __init__(self, food2_image, position):
        pygame.sprite.Sprite.__init__(self)
        self.image = food2_image
        self.rect = self.image.get_rect()
        self.rect.center = position
    def getRect(self):
        return self.rect
class Cookie():
    global SPRITES
    def __init__(self):
        self.food = CookieSprite(FOOD2_IMAGE, (foodPosition[2], 
foodPosition[3]))
        self.foodWidth = self.food.image.get_rect().width
        SPRITES.add( self.food )#also thisss piece of code
class TriscuitSprite(pygame.sprite.Sprite):
    def __init__(self, food3_image, position3):
        pygame.sprite.Sprite.__init__(self)
        self.image = food3_image
        self.rect = self.image.get_rect()
        self.rect.center = (position3)
    def getRect(self):
        return self.rect
class Triscuit():
    global SPRITES
    def __init__(self):
        self.food = TriscuitSprite(FOOD3_IMAGE, (foodPosition[4], 
foodPosition[5]))
        self.foodWidth = self.food.image.get_rect().width
        SPRITES.add( self.food )#and this piece


class MorrisonSprite(pygame.sprite.Sprite):
    def __init__( self, part_image, position ):
        pygame.sprite.Sprite.__init__(self)
        self.image = part_image
        self.rect = self.image.get_rect()
        self.rect.center = position
        self.move_step = self.image.get_rect().width # pixels per 
move step

    def wrapAroundScreen(self):
        # Stay on the screen, and wrap around
        if (self.rect.left >= WINDOW_WIDTH ):
            self.rect.right = 0
        elif (self.rect.right <= 0 ):
            self.rect.left = WINDOW_WIDTH
        if (self.rect.top >= WINDOW_HEIGHT ):
            self.rect.bottom = 0
        elif (self.rect.bottom <= 0):
            self.rect.top = WINDOW_HEIGHT

    def move( self, direction ):
        if ( direction == 'up' ):
            self.rect.y -= self.move_step
        elif ( direction == 'down' ):
            self.rect.y += self.move_step
        elif ( direction == 'left' ):
            self.rect.x -= self.move_step
        elif ( direction == 'right' ):
            self.rect.x += self.move_step
        else:
            print(" MOVE ERROR - "+direction)
        self.wrapAroundScreen()


    def moveTo( self, body_part ):
        self.rect.center = body_part.rect.center

    def getRect( self ):
        return self.rect

    def update(self):
        pass


class Morrison():
    def __init__( self, size=3 ):
        global SPRITES
        self.head = MorrisonSprite( HEAD_IMAGE, ( 
(WINDOW_WIDTH//2)-64, WINDOW_HEIGHT//2 ) )
        self.body_size = self.head.image.get_rect().width # pixels 
per move step
        self.direction = 'left';
        self.body = [ self.head ]
        SPRITES.add( self.head )
        self.grow( size )

    def changeDirection( self, new_direction ):
        self.direction = new_direction
        self.slither()

    def slither( self ):
        # Move each body part to the location of the previous part
        # So we iterate over the tail-parts in reverse
        for i in range( len( self.body )-1, 0, -1 ):
            self.body[i].moveTo( self.body[i-1] )
    # Move the head
        self.head.move( self.direction )

    def getGrowPosition( self ):
    # we grow against self.direction
    # so if we're moving up, the tail grows down
        x,y = self.body[ -1 ].getRect().center
        if ( self.direction == 'up' ):
            y += self.body_size
        elif ( self.direction == 'down' ):
            y -= self.body_size
        elif ( self.direction == 'left' ):
            x += self.body_size
        elif ( self.direction == 'right' ):
            x -= self.body_size
        return (x,y)

    def grow( self, by_size=1 ):

        for i in range( by_size ):
            # new body part needs to be added at the tail-position
            new_body_part = MorrisonSprite( BODY_IMAGE, 
self.getGrowPosition() )
            self.body.append( new_body_part )
            SPRITES.add( new_body_part )

foodPosition = generateFoodPosition()

def drawGameWindow(screen, sprites):
    global WINDOW_WIDTH, WINDOW_HEIGHT, SPRITES
    screen.fill(BLACK)
    SPRITES.draw(screen)
    pygame.display.update()
    pygame.display.flip()

### MAIN
Morrison = Morrison()
ThinMint = ThinMint()
Cookie=Cookie()
Triscuit = Triscuit()
#TriscuitSprite = TriscuitSprite()
#MorrisonSprite = MorrisonSprite()
##ThinMintSprite = ThinMintSprite()
#CookieSprite=CookieSprite()

last_move = STARTUP_MS
last_grow = STARTUP_MS

clock = pygame.time.Clock()
done = False
while not done:
    NOW_MS = int(time.time() * 1000.0)

    SPRITES.update()


    ThinMint.food.checkForConsumption(Morrison.head)

        # Move every 500ms
    if ( NOW_MS - STARTUP_MS > 3000 and NOW_MS - last_move >= 100 ):
    #print("Moving...")
        Morrison.slither()
        last_move = NOW_MS




# Grow every 2 seonds
    if ( NOW_MS - STARTUP_MS > 3000 and NOW_MS - last_grow >= 10000
):
        print("Growing...")
        Morrison.grow( 1 )
        last_grow = NOW_MS

    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.VIDEORESIZE ):
            # resize-window:
            WINDOW_WIDTH  = event.w
            WINDOW_HEIGHT = event.h
            WINDOW  = pygame.display.set_mode( ( WINDOW_WIDTH, 
WINDOW_HEIGHT ), pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE 
)
        elif (event.type == pygame.KEYDOWN):
            # Movement keys
            keys = pygame.key.get_pressed()
            if ( keys[pygame.K_UP] ):
                Morrison.changeDirection("up")
            elif ( keys[pygame.K_DOWN] ):
                Morrison.changeDirection("down")
            elif ( keys[pygame.K_LEFT] ):
                Morrison.changeDirection("left")
            elif ( keys[pygame.K_RIGHT] ):
                Morrison.changeDirection("right")

    drawGameWindow(WINDOW, SPRITES)
    clock.tick_busy_loop(60)

pygame.quit()

原本应该打印“ yeet yah”,并且有薄薄的薄荷味,但是只能打印“ yeet yah”

1 个答案:

答案 0 :(得分:0)

发生碰撞时,只需更改foodPosition[0]foodPosition[1]。但是更改此列表中的项目不会更改rect实例的ThinMintSprite属性。

总的来说,我发现您的代码令人困惑且不必要地复杂,因此我自由地将其简单化了一点:

import pygame
import random
import time
import sys
from collections import deque
# Window size
WINDOW_WIDTH=400
WINDOW_HEIGHT=400

pygame.init()
WINDOW  = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE )
SPRITES = pygame.sprite.Group()

pygame.display.set_caption("Picnic!")
HEAD_IMAGE = pygame.Surface((32, 32)) #pygame.image.load('morrison.png').convert_alpha()
HEAD_IMAGE.fill(pygame.Color('yellow'))
BODY_IMAGE = pygame.Surface((32, 32)) #pygame.image.load('morrison.png').convert_alpha()
BODY_IMAGE.fill(pygame.Color('orange'))
FOOD1_IMAGE= pygame.Surface((10, 10)) #pygame.image.load("thinMint.png").convert_alpha()
FOOD1_IMAGE.fill(pygame.Color('green'))
FOOD2_IMAGE = pygame.Surface((10, 10)) #pygame.image.load("cookie.png").convert_alpha()
FOOD2_IMAGE.fill(pygame.Color('red'))
FOOD3_IMAGE = pygame.Surface((10, 10)) #pygame.image.load("triscuit.png").convert_alpha()
FOOD3_IMAGE.fill(pygame.Color('blue'))

clock = pygame.time.Clock()

BLACK = (0,0,0)

last_positions = deque(maxlen=10)
def generateFoodPosition():
    pos = random.randrange(1, 40, 1) * 10, random.randrange(1, 40, 1) * 10
    if pos in last_positions:
        return generateFoodPosition()
    last_positions.append(pos)
    return pos

class Food(pygame.sprite.Sprite):
    def __init__(self, image, *grps):
        pygame.sprite.Sprite.__init__(self, *grps)
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.center = generateFoodPosition()

    def update(self, dt, snakehead, events):
        if self.rect.colliderect(snakehead):
            print("yeet yah")
            self.rect.center = generateFoodPosition()

class MorrisonSprite(pygame.sprite.Sprite):
    def __init__( self, part_image, position, head, *grps):
        pygame.sprite.Sprite.__init__(self, *grps)
        self.image = part_image
        self.rect = self.image.get_rect()
        self.rect.center = position
        self.move_step = self.image.get_rect().width # pixels per move step
        self.t = 0
        self.last_move = 0
        self.last_grow = 0
        self.head = head
        self.direction = 'left';
        self.body = [] 
        if self.head:
            self.body.append(self)
            self.grow(3)

    def wrapAroundScreen(self):
        # Stay on the screen, and wrap around
        if (self.rect.left >= WINDOW_WIDTH ):
            self.rect.right = 0
        elif (self.rect.right <= 0 ):
            self.rect.left = WINDOW_WIDTH
        if (self.rect.top >= WINDOW_HEIGHT ):
            self.rect.bottom = 0
        elif (self.rect.bottom <= 0):
            self.rect.top = WINDOW_HEIGHT

    def move( self ):
        if ( self.direction == 'up' ):
            self.rect.y -= self.move_step
        elif ( self.direction == 'down' ):
            self.rect.y += self.move_step
        elif ( self.direction == 'left' ):
            self.rect.x -= self.move_step
        elif ( self.direction == 'right' ):
            self.rect.x += self.move_step
        else:
            print(" MOVE ERROR - "+direction)
        self.wrapAroundScreen()

    def update(self, dt, snakehead, events):
        if not self.head:
            return

        keys = pygame.key.get_pressed()
        if ( keys[pygame.K_UP] ):
            self.direction = "up"
        elif ( keys[pygame.K_DOWN] ):
            self.direction =  "down"
        elif ( keys[pygame.K_LEFT] ):
            self.direction = "left"
        elif ( keys[pygame.K_RIGHT] ):
            self.direction = "right"

        self.t += dt
        if any(e for e in events if e.type == pygame.KEYDOWN) or ( self.t > 3000 and self.t - self.last_move >= 100 ):
            self.slither()
            self.last_move = self.t

        # Grow every 2 seonds
        if (self.t  > 3000 and self.t - self.last_grow >= 10000):
            print("Growing...")
            self.grow( 1 )
            self.last_grow = self.t

    def slither( self ):
        # Move each body part to the location of the previous part
        # So we iterate over the tail-parts in reverse
        for i in range( len( self.body )-1, 0, -1 ):
            self.body[i].rect.center = self.body[i-1].rect.center
        # Move the head
        self.move()

    def getGrowPosition( self ):
        # we grow against self.direction
        # so if we're moving up, the tail grows down
        x,y = self.body[ -1 ].rect.center
        if ( self.direction == 'up' ):
            y += self.move_step
        elif ( self.direction == 'down' ):
            y -= self.move_step
        elif ( self.direction == 'left' ):
            x += self.move_step
        elif ( self.direction == 'right' ):
            x -= self.move_step
        return (x,y)

    def grow( self, by_size=1 ):
        for i in range( by_size ):
            # new body part needs to be added at the tail-position
            new_body_part = MorrisonSprite( BODY_IMAGE, self.getGrowPosition(), False, *self.groups())
            self.body.append( new_body_part )

### MAIN
Morrison = MorrisonSprite(HEAD_IMAGE, ((WINDOW_WIDTH//2)-64, WINDOW_HEIGHT//2 ), True, SPRITES)

clock = pygame.time.Clock()
done = False
Food(FOOD1_IMAGE, SPRITES)
Food(FOOD2_IMAGE, SPRITES)
Food(FOOD3_IMAGE, SPRITES)
dt = 0
while not done:

    events = pygame.event.get()
    for event in events:
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.VIDEORESIZE ):
            # resize-window:
            WINDOW_WIDTH  = event.w
            WINDOW_HEIGHT = event.h
            WINDOW  = pygame.display.set_mode(( WINDOW_WIDTH, WINDOW_HEIGHT ), pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE)

    SPRITES.update(dt, Morrison.rect, events)
    WINDOW.fill(BLACK)
    SPRITES.draw(WINDOW)
    pygame.display.flip()
    dt = clock.tick_busy_loop(60)

pygame.quit()

一些注意事项:

  • 我将图像更改为纯色表面,因为我没有您的图像,并且我想运行我的代码,显然:-)
  • 我不知道你为什么这么多课。就像您有一个类ThinMintSprite,然后创建另一个类ThinMint,然后创建ThinMint的实例并用该实例覆盖该类。
  • 您为食物创建的3个(或6个)类基本上都是相同的。只需创建一个类,然后将其他图像传递给它即可。
  • 无需为所有食品创建全局职位列表。让每个食品类实例自己创建自己的位置。在上面的示例中,我使用了双端队列(deque)(它只是一个具有最大项目数的列表),以防止食物在同一位置产生。
  • 您的generateFoodPosition函数还是坏了,因为当if语句为True时,它不会从递归调用中返回值
  • 我将所有逻辑移到了它们所属的恕我直言的sprite类中(不在主循环中)
  • 您必须确定是要使用事件作为控件还是要检查pygame.key.get_pressed()。你不应该把它们混在一起。您可以在前3秒内移动。是故意的吗?如果没有,只需删除any(...) or部分。
  • 此外,蛇类游戏通常是基于网格的,这使得碰撞检查等变得更加容易。