0

import libraries and define variables

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

this is my player class

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
        

draw my player with x and y coordinates

    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 

change player coordinates

    def derecha (self):
        self.change_x = 5
    def izquierda (self):
        self.change_x = -5
    def stop (self):
        self.change_x = 0

gravity function

    #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

this is my stage class, here i make a white rectangle and i want my player can collide with the rectangle

class Escenario:
    def __init__(self):
        super().__init__()

here i create the rectangle

        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

here I create the events and initialize the objects

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

gravity control

    if canjump == True and player.ha_saltado == True:
        player.salto()
    else:
        player.ha_saltado = False
        if player.y < 550:
            player.y += 10

level control(when the player x coordinate is greater than the screen width, the level increases)

    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)

update the game frames and draw the objects

    #dibujar escenario
    player.dibujar()

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

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

1 Answers1

0

There is a bug in the dibujar method. It must be self.rect.x = self.x instead of self.rect.x = self.x + self.change_x:

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)
Rabbid76
  • 177,135
  • 25
  • 101
  • 146