Skip to main content
 首页 » 编程设计

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

2024年09月07日7tuyile006

import math 
import random 
import pygame 
from pygame.locals import * 
import sys 
 
 
 
def events(): 
    for event in pygame.event.get(): 
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): 
            pygame.quit() 
            sys.exit() 
 
 
pygame.init() 
W = 700 
H = 400 
updater = pygame.time.Clock() 
display = pygame.display.set_mode((700, 400)) 
pygame.display.set_caption("Skating_Game") 
x = y = 0 
 
surface = pygame.image.load("man2.png") 
pygame.display.set_icon(surface) 
 
class player: 
    def __init__(self, velocity, maxJumpRange): 
        self.velocity = velocity 
        self.maxJumpRange = maxJumpRange 
 
    def setLocation(self, 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 - 60 and self.y + self.velocity >= H - 60: 
                 self.y = H - 60 
                 self.falling = False 
             else: 
                self.y += self.velocity 
 
    def draw(self): 
        display = pygame.display.get_surface() 
        character = pygame.image.load("man3.png").convert_alpha() 
        display.blit(character, (self.x, self.y - 100)) 
 
        #pygame.draw.circle(display, (255, 255, 255), (self.x, self.y - 25), 25, 0) 
    def do(self): 
        self.keys() 
        self.move() 
        self.draw() 
 
P = player(3, 50) 
P.setLocation(350, 0) 
BLACK = (  0,   0,   0) 
 
g=0 
font = pygame.font.SysFont("Plump", 30) 
 
obstacle = pygame.image.load("obstacle.png").convert_alpha() 
 
 
background = pygame.image.load("Road.png").convert() 
x = 0 
while True: 
 
 
    events() 
 
    rel_x = x % background.get_rect().width 
    display.blit(background, (rel_x - background.get_rect().width,0)) 
    if rel_x < 700: 
        display.blit(background, (rel_x, 0)) 
    x -= 1 
    g += 0.01 
    pygame.draw.rect(display, (255,255,255,128), [rel_x, 275, 150, 50]) 
    display.blit(obstacle, (rel_x, 250)) 
    text = font.render("Score: "+str(int(g)), True, (255, 255, 255)) 
    display.blit(text, (0,0)) 
 
 
    P.do() 
    if P.rect.collidepoint(self.x,self.y): 
            pygame.quit() 
    pygame.display.update() 
    updater.tick(200) 

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

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

请您参考如下方法:

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

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)