Pygame:随机方向?

时间:2013-12-23 20:24:27

标签: python random pygame

我正在制作一款以随机生成的洞穴为特色的游戏。首先创建一个64x16块的640x480字段。然后放置3个CaveMaker个实例,这些实例会破坏它们触摸的每个块,它们应该每隔几秒移动一次方向以产生随机的洞穴般的路径,但是对于我的代码,它们就像这个垃圾一样:< / p>

Eeeewwww

我相信这是因为我写的非常即兴的代码应该让它改变方向:

class CaveMaker(object):
    def __init__(self):
        self.active=1
        self.x=randrange(1,640)
        self.y=randrange(1,480)
        #Choose a X speed and Y speed
        self.direction=(choice([20,-20,1,-1,15,0,-15]),choice([20,-20,1,-1,15,0,-15]))
        self.rect=pygame.Rect(self.x,self.y,75,75)
        #Set amount of time to move this direction
        self.timeout=choice([30,50,70,90,110])
        self.destroytime=randrange(200,360)
    def Update(self):
        if self.active==1:
            #Move in a certain direction?
            self.x+=self.direction[0]
            self.y+=self.direction[1]
            self.timeout-=1
            if self.timeout==0:
                #Change direction?
                self.direction=   (choice([20,-20,1,-1,15,0,-15]),choice([20,-20,1,-1,15,0,-15]))
                self.timeout=choice([30,50,70,90,110])
            self.destroytime-=1
            if self.destroytime==0:
                self.active=0
            self.rect=pygame.Rect(self.x,self.y,60,60)

那么如何告诉对象移动随机方向?

这是我的完整代码:

import pygame
from pygame.locals import *
from collections import namedtuple
from random import randrange,choice
pygame.init()
screen=pygame.display.set_mode((640,480))
Move = namedtuple('Move', ['up', 'left', 'right'])
max_gravity=50
class Block(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.rect=pygame.Rect(self.x,self.y,16,16)
class CaveMaker(object):
    def __init__(self):
        self.active=1
        self.x=randrange(1,640)
        self.y=randrange(1,480)
        #Choose a X speed and Y speed
        self.direction=(choice([20,-20,1,-1,15,0,-15]),choice([20,-20,1,-1,15,0,-15]))
        self.rect=pygame.Rect(self.x,self.y,75,75)
        #Set amount of time to move this direction
        self.timeout=choice([30,50,70,90,110])
        self.destroytime=randrange(200,360)
    def Update(self):
        if self.active==1:
            #Move in a certain direction?
            self.x+=self.direction[0]
            self.y+=self.direction[1]
            self.timeout-=1
            if self.timeout==0:
                #Change direction?
                self.direction=(choice([20,-20,1,-1,15,0,-15]),choice([20,-20,1,-1,15,0,-15]))
                self.timeout=choice([30,50,70,90,110])
            self.destroytime-=1
            if self.destroytime==0:
                self.active=0
            self.rect=pygame.Rect(self.x,self.y,60,60)
class Player(object):
    def __init__(self, x, y):
        self.rect = pygame.Rect(x,y,16,16)
        self.on_ground = True
        self.xvel = 0
        self.yvel = 0
        self.jump_speed = 5
        self.move_speed = 2

    def update(self, move, blocks):

        if move.up and self.on_ground:
            self.yvel -= self.jump_speed

        if move.left:
                self.xvel = -self.move_speed
        if move.right:
                self.xvel = self.move_speed

        if not self.on_ground:
            self.yvel += 0.3
            if self.yvel > max_gravity: self.yvel = max_gravity

        if not (move.left or move.right):
            self.xvel = 0

        self.rect.left += self.xvel
        self.collide(self.xvel, 0, blocks)

        self.rect.top += self.yvel
        self.on_ground = False;
        self.collide(0, self.yvel, blocks)

    def collide(self, xvel, yvel, blocks):
        for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:

            if xvel > 0:
                    self.rect.right = block.rect.left
            if xvel < 0:
                    self.rect.left = block.rect.right

            if yvel > 0:
                self.rect.bottom = block.rect.top
                self.on_ground = True
                self.yvel = 0
            if yvel < 0: self.rect.top = block.rect.bottom;self.yvel=0
blocks=[]
cavemakes=[]
gen=1
genx=0
geny=0
px=0
py=0
cavemakes.append(CaveMaker());cavemakes.append(CaveMaker());cavemakes.append(CaveMaker());cavemakes.append(CaveMaker());
while True:
    key=pygame.key.get_pressed()
    if gen==1:
        blocks.append(Block(genx,geny))
        geny+=16
        if geny>480:
            geny=0
            genx+=16
            if genx>640:
                gen=2
    elif gen==2:
        screen.fill((5,80,200))
        for b in blocks:
            pygame.draw.rect(screen, (90,90,90), b.rect, 0)
        for c in cavemakes:
            if c.active==0:
                gen='done'
                while any(b.rect.collidepoint(px, py) for b in blocks):
                    px=randrange(1,630)
                    py=randrange(1,470)
                player=Player(px,py)
            c.Update()
            for b in blocks:
                if b.rect.colliderect(c.rect):
                    blocks.remove(b) 
        pygame.display.flip()
    if gen=='done':
        screen.fill((5,80,200))
        for b in blocks:
            pygame.draw.rect(screen, (90,90,90), b.rect, 0)
        for e in pygame.event.get():
            if e.type==KEYUP:
                if e.key==K_r:
                    blocks=[]
                    cavemakes=[]
                    gen=1
                    genx=0
                    geny=0
                    px=0
                    py=0
                    cavemakes.append(CaveMaker());cavemakes.append(CaveMaker());cavemakes.append(CaveMaker());cavemakes.append(CaveMaker());
            if e.type==QUIT:
                exit()
        move = Move(key[K_w], key[K_a], key[K_d])
        player.update(move,blocks)
        pygame.draw.rect(screen, (5,200,5), player.rect, 3)
        pygame.display.flip()

2 个答案:

答案 0 :(得分:1)

一些伪/ C#代码:

// lets say we have 4 directions
// we generate random number for it    

int randomDirection = Random.Next(0, 4);

// 0:MoveUp()
// 1:MoveDown()
// 2:MoveLeft()
// 3:MoveRight()

// then we check what random direction number we got 
// and execute specific method for it

switch(randomDirection)
{
    case 0:
    player.MoveUp();
    break;

    case 1:
    player.MoveDown();
    break;

    case 3:
    player.MoveDown();
    break;

    case 4:
    player.MoveDown();
    break;
}

我刚刚发现Python中没有case / switch语句。请检查此question以了解如何替换它。

答案 1 :(得分:1)

我不太明白你想要什么。你想要一系列相连的隧道吗?你想要一组随机的未连接清除圆圈吗?您想要清除通过隧道连接的圆圈吗?

但这是一些一般性的建议。

首先,您的代码中包含太多硬编码的数字。您应该创建一个名为Board或类似的类,将所有数字保存为常量变量,然后使用这些数字:

class Board(object):
    def __init__(self, width, height, block_size):
        assert width % block_size == 0
        assert height % block_size == 0
        self.width = width
        self.height = height
        self.block_size = block_size
        self.w = width // self.block_size
        self.h = height // self.block_size

现在,一旦你拥有了上述内容,就可以做到这样的事情:

x = random.randint(0, board.w - 1)
y = random.randint(0, board.h - 1)
xpos = x * board.block_size
ypos = y * board.block_size

我的建议是使用上面的代码来选择洞穴所在的位置,并在添加每个洞穴时绘制一个隧道来连接它。不是绘制一堆随机线,而是选择随机洞穴,然后用线连接洞穴。让您的代码使用连接洞穴的直线,然后如果您不喜欢直线,请重新编写代码以绘制具有一些随机性的线条。