首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何让我的玩家与物体碰撞?

我如何让我的玩家与物体碰撞?
EN

Stack Overflow用户
提问于 2021-06-22 23:39:05
回答 1查看 19关注 0票数 0

导入库和定义变量

代码语言:javascript
复制
import pygame, sys
import math


#variables globales
pygame.init()
size = width, height = 1000,700
screen = pygame.display.set_mode(size)
white = 255,255,255
reloj = pygame.time.Clock()
x = 100
y = 550
canjump = False
level = 3

这是我的player类

代码语言:javascript
复制
class Player:
    x = 100
    y = 550
    jump = 0
    change_x = 0
    change_y = 0
    ha_saltado = False
    top = 550
    plataformaTop = False
    def __init__(self):
        super().__init__()

        square_size = 50,50
        self.image = pygame.Surface((square_size))
        self.image.fill(white)

        #establecer referencia de la imagen
        self.rect = self.image.get_rect()
        self.impulso_salto = 10

使用x和y坐标绘制我的玩家

代码语言:javascript
复制
    def dibujar(self):
        self.x += self.change_x
        self.rect.x = self.x + self.change_x
        self.rect.y = self.y 
        screen.blit(self.image, (self.x,self.y))

    #movimiento horizontal 

更改玩家坐标

代码语言:javascript
复制
    def derecha (self):
        self.change_x = 5
    def izquierda (self):
        self.change_x = -5
    def stop (self):
        self.change_x = 0

重力函数

代码语言:javascript
复制
    #gravedad
    def grav(self):
        if self.ha_saltado:
            if self.impulso_salto>= -10:
                if self.impulso_salto < 0:
                    self.y += (self.impulso_salto ** 2)*0.5
                   
                else:
                    self.y -= (self.impulso_salto ** 2)*0.5
                self.impulso_salto -= 1
            else:
                self.ha_saltado = False
                canjump = False
                self.impulso_salto = 10
        
    

    def salto(self):
        Player.grav(self)

    def colisiones(self):
        self.y += 2
        if self.rect.colliderect(plataforma.rect):
            self.plataformaTop = True
        else: 
            self.plataformaTop = False
        self.y -= 2
        if self.plataformaTop:
            print(plataforma.rect.top, self.y)
            self.y -= 6

这是我的stage类,在这里我做了一个白色的矩形,我希望我的玩家可以与这个矩形碰撞。

代码语言:javascript
复制
class Escenario:
    def __init__(self):
        super().__init__()

在这里我创建了一个矩形

代码语言:javascript
复制
        self.image = pygame.Surface([300,100])
        self.image.fill(white)  
        self.rect = self.image.get_rect() 
    
    def dibujar(self, x, y):
        screen.blit(self.image, (x, y))
        self.rect.x = x
        self.rect.y = y

在这里,我创建事件并初始化对象

代码语言:javascript
复制
plataforma = Escenario()
player = Player()
texto = Texto()
pygame.mixer.music.load("C:/Users/Mati/Desktop/game/ost.mp3")
pygame.mixer.music.play()
while 1:
    screen.fill((0,0,0))
    player.colisiones()
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                player.derecha()
                
            if event.key == pygame.K_LEFT:
                player.izquierda()
                
            if event.key == pygame.K_x:
                player.stop()

            if event.key == pygame.K_UP:                
                canjump = True
                player.ha_saltado = True

重力控制

代码语言:javascript
复制
    if canjump == True and player.ha_saltado == True:
        player.salto()
    else:
        player.ha_saltado = False
        if player.y < 550:
            player.y += 10

关卡控制(当玩家x坐标大于屏幕宽度时,关卡增加)

代码语言:javascript
复制
    if player.x >= width:
        level += 1
        player.x = 100
    
    if level == 1:
        texto.write("welcome to monochromatic adventure", 43, 200)
    if level == 3:
        plataforma.dibujar(800,500)
        player.colisiones()

    if level == 2:
        texto.write("stage 1: Anguish", 43, 200)

更新游戏帧并绘制对象

代码语言:javascript
复制
    #dibujar escenario
    player.dibujar()

    pygame.draw.line(screen, white, [1000, 600], [0, 600], 2)

    reloj.tick(60)
    pygame.display.flip()
EN

回答 1

Stack Overflow用户

发布于 2021-06-22 23:43:17

dibujar方法中存在错误。必须是self.rect.x = self.x而不是self.rect.x = self.x + self.change_x

代码语言:javascript
复制
class Player:
    # [...]

    def dibujar(self):
        self.x += self.change_x
        self.rect.x = self.x
        self.rect.y = self.y 
        screen.blit(self.image, self.rect)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68086659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档