【问题标题】:How can i make sprites spawn on random locations using pygame?如何使用 pygame 在随机位置生成精灵?
【发布时间】:2015-05-23 12:48:56
【问题描述】:

我一直在尝试对一个类似躲闪者的游戏做出改变,其中一些方块让你成长,而另一些方块让你缩小。我还计划添加一个红色的,其中一个应该会让你失去一条生命(你将有 3 条生命开始),以及其他各种具有自己属性的。然而,我在让掉落的方块随机生成方面遇到了麻烦,这在很多游戏中都是必需的。

我的计划基本上是我希望每次都在随机位置重新生成块,并且在某个时候我希望增加掉落块的数量以进一步增加难度。

这是我目前的进度。非常感谢任何输入:

import pygame
import random

pygame.init()

win_width = 800
win_height = 600
window = pygame.display.set_mode((win_width,win_height))

pygame.display.set_caption("Roger Dodger")

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
orange = (255,127,0)
yellow = (255,255,0)
blue = (0,0,255)


clock = pygame.time.Clock()

class Collisions:
    def __init__(self, x1,y1,w1,h1,x2,y2,w2,h2):
        self.x1 = x1
        self.y1 = y1
        self.w1 = w1
        self.h1 = h1
        self.x2 = x2
        self.y2 = y2
        self.w2 = w2
        self.h2 = h2

    def checkCol(self):
        if (self.x2 + self.w2 >= self.x1 >= self.x2 and self.y2 + self.h2 >= self.y1 >= self.y2):

            return True

        elif (self.x2 + self.w2 >= self.x1 + self.w1 >= self.x2 and self.y2 + self.h2 >= self.y1 >= self.y2):

            return True

        elif (self.x2 + self.w2 >= self.x1 >= self.x2 and self.y2 + self.h2 >= self.y1 + self.h1 >= self.y2):

            return True

        elif (self.x2 + self.w2 >= self.x1 + self.w1 >= self.x2 and self.y2 + self.h2 >= self.y1 + self.h1 >= self.y2):

            return True

        else:

            return False

    def yel_col(self):
        if Collisions.checkCol(self):
            return True

    def ora_col(self):
        if Collisions.checkCol(self):
            return True


class Sprite:

    def __init__(self,x,y,width,height, color):

        self.x = x

        self.y = y

        self.width = width

        self.height = height

        self.color = color

    def render(self,):
              pygame.draw.rect(window,self.color,(self.x,self.y,self.width,self.height))



Sprite1=Sprite(win_width/2 ,win_height-60,30,30, blue)


moveX = 0

sprite2x = random.randrange(30, win_width, 30)
sprite3x = random.randrange(30, win_width, 30)

falling_pos = 0


gameLoop=True
while gameLoop:

    for event in pygame.event.get():

        if (event.type==pygame.QUIT):

            gameLoop=False

        if (event.type==pygame.KEYDOWN):

            if (event.key==pygame.K_LEFT):

                moveX = -3

            if (event.key==pygame.K_RIGHT):

                moveX = 3




    window.fill(white)

    ground = pygame.draw.rect(window, black, ((0, win_height-30), (win_width, 30)))

    Sprite1.x+=moveX

    falling_pos += 3

    Sprite2=Sprite(sprite2x,falling_pos,30,30, orange)
    Sprite3=Sprite(sprite3x, falling_pos, 30, 30, yellow)

    collisions1=Collisions(Sprite1.x,Sprite1.y,Sprite1.width,Sprite1.height,Sprite2.x,Sprite2.y,Sprite2.width,Sprite2.height)
    collisions2=Collisions(Sprite1.x,Sprite1.y,Sprite1.width,Sprite1.height,Sprite3.x,Sprite3.y,Sprite3.width,Sprite3.height)


    Sprite1.render()
    Sprite2.render()
    Sprite3.render()

    if collisions2.checkCol() and collisions2.yel_col():
       if Sprite1.width and Sprite1.height > 30:
           Sprite1.width -= 5
           Sprite1.height -= 5
           Sprite1.y += 5

    if collisions1.checkCol() and collisions1.ora_col():
       if Sprite1.width and Sprite1.height < 300:
           Sprite1.width += 5
           Sprite1.height += 5
           Sprite1.y -= 5

    if Sprite1.x < 0:
        Sprite1.x = win_width

    elif Sprite1.x > win_width:
        Sprite1.x = 0



    if falling_pos > win_height:
        falling_pos = 0

    pygame.display.flip()

    clock.tick(120)

pygame.quit()

【问题讨论】:

  • 你到底有什么问题?

标签: python pygame sprite game-physics


【解决方案1】:

要让它们在随机位置生成,请使用random.randint(a, b) 指定起始位置。

【讨论】:

  • 谢谢。但是,如果您查看我的代码,我已经使用 random.randrange(a, b, c) 来分配 X 轴上的起始位置。我遇到的问题是他们之后没有获得新的随机位置,矩形每次都从相同的随机分配位置落下。
  • 您只能在 while 循环开始之前分配一次。当您将 y 值重置回顶部时,获取另一个随机 x 位置并使用它。
  • 在你指出之前我才意识到,谢谢分配!
猜你喜欢
  • 2019-03-23
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多