简体   繁体   English

如何为我的游戏正确设置坐标

[英]How to set up co-ordinates correctly for my game

How to make the red block appear between grey and green block. 如何使红色块出现在灰色和绿色块之间。

在此处输入图片说明

In the above picture i changed my rectangle x position of donkey to 160 then only i can able to see the red block if not means i'll hide under grey block. 在上面的图片中, 我将驴的矩形x位置更改为160,然后只有我才能看到红色块,否则我将隐藏在灰色块下。

this is my code 这是我的代码

class Road(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((200, 500))
        self.image.fill(grey)
        self.rect = self.image.get_rect()
        self.rect.centerx = width/2
        #self.rect.left = width-200
        #print self.rect.left
        self.speedy = 5
    def update(self):
        self.rect.y += self.speedy
        if self.rect.y > 0:
            self.rect.y = 0

class Donkey(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 100))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.rect.x = 200
        print 'x:',self.rect.x
        self.rect.y = -54
        print 'y:',self.rect.y
        self.speedy = random.randint(8,10)
    def update(self):
        self.choice = (200, 300)
        self.rect.y += self.speedy
        if self.rect.top > height:
            self.rect.x = random.choice(self.choice)
            print 'x:', self.rect.x
            self.rect.y = random.randrange(-100, -40)
            print 'y', self.rect.y
            self.speedy = random.randint(8,10)

class Race_car(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 100))
        self.image.fill(green)
        self.rect = self.image.get_rect()
        self.rect.x = 200
        self.rect.bottom = height
        self.speedy = -0.1
    def update(self):
        #self.speedx = 0
        self.rect.y += self.speedy
        keypressed = pygame.key.get_pressed()
        if keypressed[pygame.K_RIGHT]:
            self.rect.x = 300
        elif keypressed[pygame.K_LEFT]:
            self.rect.x = 200


#Group, object for game_hearth
game_hearth = pygame.sprite.Group()
road = Road()
game_hearth.add(road)

#Group, object for donkey
donkey = Donkey()
donkeys = pygame.sprite.Group()
game_hearth.add(donkey)
donkeys.add(donkey)

#Group, object for racecar
racecar = Race_car()
racecars = pygame.sprite.Group()
game_hearth.add(racecar)
racecars.add(racecar)

when i run my code after commenting the Race_car class the red blocks working fine, see 当我在注释Race_car类后运行代码时,红色块正常工作,请参见

在此处输入图片说明

so the thing is i want to display the both red and green block above the grey road. 所以问题是我想在灰色道路上方显示红色和绿色的块。 so how to do it without commenting any class in my program. 因此如何在不注释程序中任何类的情况下进行操作。 i know its some co-ordinate mistake but i don't know where to change it. 我知道它的一些协调错误,但我不知道在哪里更改。

From the Pygame docs : Pygame文档

The Group does not keep sprites in any order, so the draw order is arbitrary. 组不以任何顺序保留精灵,因此绘制顺序是任意的。

You should probably switch to pygame.sprite.OrderedUpdates : 您可能应该切换到pygame.sprite.OrderedUpdates

This class derives from pygame.sprite.RenderUpdates(). 此类派生自pygame.sprite.RenderUpdates()。 It maintains the order in which the Sprites were added to the Group for rendering. 它保持将精灵添加到组以进行渲染的顺序。 This makes adding and removing Sprites from the Group a little slower than regular Groups. 这使得从组中添加和删除Sprite的速度比常规组慢一些。

#Group, object for game_hearth
game_hearth = pygame.sprite.OrderedUpdates()

road = Road()
game_hearth.add(road)

#Group, object for donkey
donkey = Donkey()
donkeys = pygame.sprite.OrderedUpdates()
game_hearth.add(donkey)
donkeys.add(donkey)

#Group, object for racecar
racecar = Race_car()
racecars = pygame.sprite.OrderedUpdates()
game_hearth.add(racecar)
racecars.add(racecar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Konvajs:如何从画布对象中获取与画布坐标相对应的弓箭手坐标? - Konvajs : how to get bowser co-ordinates corresponding to canvas co-ordinates from canvas object? 如何找到在二维平面上连接一组坐标的最小生成树? - How to find minimum spanning tree connecting a set of co-ordinates in the 2d plane? 如何使用圆公式在python中为圆生成一组坐标? - How to generate a set of co-ordinates for a circle in python using the circle formula? 通过坐标标记图像 - Marking image by their co-ordinates 如何使用Tweepy和Python将坐标发送到Twitter状态更新 - How to send co-ordinates with Tweepy and Python to Twitter status update Python Web抓取-如何找到map元素的坐标? - Python Web scraping - How to locate the Co-ordinates of the map element? 如何在 Opencv Python 中查找特定地标点的坐标 - How to find co-ordinates of a specific Landmark point in Opencv Python 如何使用笛卡尔坐标在 matplotlib 中绘制球体? - How to plot a sphere in matplotlib using cartesian co-ordinates? 如何转换wx.GetMousePosition坐标以适合DC - How to convert wx.GetMousePosition co-ordinates to fit in a DC 如何将坐标从图像映射到2D表面 - How to map co-ordinates from the image to a 2D surface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM