当主要玩家与图像(障碍物)发生碰撞时,如何让游戏结束?

时间:2018-01-10 01:17:20

标签: python pygame

adb shell am instrument -w -e size small -e size medium com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner

因此,如果玩家与障碍物图像发生碰撞,游戏应该停止。我该怎么做呢?我为玩家制作了一个课程,障碍只是一个不断移动的图像。

我想也许我可以跟踪玩家和障碍物的x和y坐标,当他们的半径重叠时,游戏可以停止。

2 个答案:

答案 0 :(得分:1)

Pygame矩形包含一个collidepoint and colliderect方法,允许您检查某些内容是否与矩形相交。因此,您可以在障碍物下方绘制矩形,并检查玩家的坐标是否与矩形相交。像这样:

if self.rect.collidepoint(self.x,self.y):
    pygame.quit()

答案 1 :(得分:1)

这是您的程序的工作(简化)版本,带有一些注释。您必须为障碍物和玩家创建rects,然后在colliderect方法的帮助下检查rects是否发生碰撞。

import sys
import pygame
from pygame.locals import *


pygame.init()
W = 700
H = 400
updater = pygame.time.Clock()
display = pygame.display.set_mode((700, 400))

PLAYER_IMAGE = pygame.Surface((30, 50))
PLAYER_IMAGE.fill(pygame.Color('dodgerblue1'))

class Player:

    def __init__(self, x, y, velocity, maxJumpRange):
        self.velocity = velocity
        self.maxJumpRange = maxJumpRange
        self.image = PLAYER_IMAGE  # Give the player an image.
        # Create a rect with the size of the PLAYER_IMAGE and
        # pass the x, y coords as the topleft argument.
        self.rect = self.image.get_rect(topleft=(x, y))
        self.x = x
        self.y = y
        self.xVelocity = 0
        self.jumping = False
        self.jumpCounter = 0
        self.falling = True

    def keys(self):
        k = pygame.key.get_pressed()

        if k[K_LEFT]:
            self.xVelocity = -self.velocity
        elif k[K_RIGHT]:
            self.xVelocity = self.velocity
        else:
            self.xVelocity = 0

        if k[K_SPACE] and not self.jumping and not self.falling:
            self.jumping = True
            self.jumpCounter = 0

    def move(self):
        self.x += self.xVelocity

        if self.jumping:
            self.y -= self.velocity
            self.jumpCounter += 1
            if self.jumpCounter  == self.maxJumpRange:
                self.jumping = False
                self.falling = True
        elif self.falling:
             if self.y >= H - 160:  # Simplified a little.
                 self.y = H - 160
                 self.falling = False
             else:
                self.y += self.velocity
        # Update the position of the rect, because it's
        # used for the collision detection.
        self.rect.topleft = self.x, self.y

    def draw(self, display):
        # Just draw the image here.
        display.blit(self.image, (self.x, self.y))

    def do(self):
        self.keys()
        self.move()


player = Player(350, 0, 3, 50)

obstacle = pygame.Surface((150, 50))
obstacle.fill(pygame.Color('sienna1'))
# Create a rect with the size of the obstacle image.
obstacle_rect = obstacle.get_rect()

g = 0
x = 0
FPS = 60  # Cap the frame rate at 60 or 30 fps. 300 is crazy.
while True:
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()

    # --- Update the game ---
    player.do()
    rel_x = x % display.get_width()
    x -= 7
    g += 0.01
    obstacle_rect.topleft = rel_x, 250  # Update the position of the rect.

    # --- Draw everything ---
    display.fill((30, 30, 30))
    display.blit(obstacle, (rel_x, 250))
    if g > 30:
        display.blit(obstacle, (rel_x+350, 250))

    # Check if the obstacle rect and the player's rect collide.
    if obstacle_rect.colliderect(player.rect):
        print("Game over!")  # And call pygame.quit and sys.exit if you want.

    # Draw the image/surface of the player onto the screen.
    player.draw(display)
    # Draw the actual rects of the objects (for debugging).
    pygame.draw.rect(display, (200, 200, 0), player.rect, 2)
    pygame.draw.rect(display, (200, 200, 0), obstacle_rect, 2)

    pygame.display.update()
    updater.tick(FPS)
相关问题