如何在Pygame中设置我的图像可以通过的边界?如何防止图像落后于另一个?

时间:2016-08-04 02:03:33

标签: python pygame

这是我的pygame的代码

import pygame
import os

img_path=os.path.join('C:/Desktop/Python Stuff','Angry Birds.jpg')
class pic(object):
    def __init__(self):
    """ The constructor of the class """
    self.image = pygame.image.load(img_path)
    # the bird's position
    self.x = 0
    self.y = 0


def handle_keys(self):
    """ Handles Keys """
    key = pygame.key.get_pressed()
    dist = 5
    if key[pygame.K_DOWN]: # down key
        self.y += dist # move down
    elif key[pygame.K_UP]: # up key
        self.y -= dist # move up
    if key[pygame.K_RIGHT]: # right key
        self.x += dist # move right
    elif key[pygame.K_LEFT]: # left key
        self.x -= dist # move left

def draw(self, surface):
    """ Draw on surface """
    # blit yourself at your current position
    surface.blit(self.image, (self.x, self.y))

这是屏幕尺寸。这是我应该限制图像边界的地方吗?

pygame.init()
screen=pygame.display.set_mode([1500,850])
Pic=pic()

pygame.display.set_caption('Angry Birds')

这是我希望有

边界的图像
pic=pygame.image.load('Angry Birds.jpg')

keep_going=True
while keep_going:
    event=pygame.event.poll()
   *emphasized text* if event.type == pygame.QUIT:
    pygame.quit()
    running = False
Pic.handle_keys()
screen.blit(pic, (-200, 0))
Pic.draw(screen)

这张图片就是“愤怒的小鸟”形象背后的形象。如何阻止它落后于此图像?

tux=pygame.image.load('Rock Lee.gif')



screen.blit(tux,(500,600))
screen.blit(tux,(500,400))
screen.blit(tux,(500,0))
screen.blit(tux,(900,200))
screen.blit(tux,(900,400))
screen.blit(tux,(900,600))
screen.blit(tux,(1300,0))
screen.blit(tux,(1300,200))
screen.blit(tux,(1300,600))
pygame.display.get_surface([1500,850]).get_size([1500,850])
pygame.display.update()

2 个答案:

答案 0 :(得分:0)

边境碰撞

实现边界碰撞的一种简单方法是检查当前位置是否在屏幕外,如果是,则将其移回。通过从屏幕创建一个Rect对象来完成最简单的操作,您可以通过 pic 的类更新方法传递它(类应该以大写字母开头)。因此,如果您传递屏幕对象,请先创建更新方法。

此外,由于x和y位置参考图像的左上角,因此在检查右侧和底部的边界碰撞时需要考虑这一点。最好是创建属性 width height ,而不是我在下面做的。

def update(self, screen):
    """Method that check border collision for object 'pic'."""
    border = screen.get_rect()
    width = self.image.get_width()
    height = self.image.get_height()

    if self.x < border.left:
        # You collided with the left side of the border.
        # Move your character back to the screen
        self.x = border.left

    elif self.x > border.right - width:
        # You collided with the right side of the border.
        # Move your character back to the screen
        self.x = border.right - width

    if self.y < border.top:
        # You collided with the top of the border.
        # Move your character back to the screen
        self.y = border.top

    elif self.y > border.bottom - height:
        # You collided with the bottom of the border.
        # Move your character back to the screen
        self.y = border.bottom - height

您所要做的就是每次在循环中调用此方法,如下所示:

Pic.handle_keys()  # Variables should be all lowercase! Not like it's currently.
Pic.update(screen)  # Variables should be all lowercase! Not like it's currently.
Pic.draw(screen)  # Variables should be all lowercase! Not like it's currently.

将图像保持在前面

当blitting到屏幕时,它会将每个图像绘制在彼此之上。在你的情况下,你会让你的角色然后是岩石,这意味着岩石总是在你的角色之上。改变这一点很简单,首先将岩石劈开,然后将角色放在最后,你的角色最终会在你的岩石前面。

答案 1 :(得分:0)

A)屏幕上保持矩形

最简单的方法是在Rect.clamp_ip(rect)

上使用Sprite
screen_size = Rect(1500,850)

# right after when you move the bird
bird.rect.clamp_ip(screen_size)

B)直接碰撞时的矩形

# Where .xvel and .yvel are bird's movement per frame
new_rect = bird.rect.move(bird.vxel, bird.yvel)
if not new_rect.collide_rect(other_bird.rect)
    bird.rect = new_rect
else
    print("Ouch!")