Python - Ayuda con pygame, no se añadir ni sprites a mis personajes, ni hacer scroll ni hacer el fondo animad

 
Vista:
sin imagen de perfil

Ayuda con pygame, no se añadir ni sprites a mis personajes, ni hacer scroll ni hacer el fondo animad

Publicado por Héctor (3 intervenciones) el 31/12/2021 10:06:27
Hola a todos, he dejado aquí mi progreso, ya que, estoy estancado, se que puede haber errores en esto, y si los encontráis encantado de aprender de ellos y saber cuales son.

Un saludo.


import pygame
import random
from pygame.locals import *
import pickle
from os import path
from pygame import mixer
import time

pygame.mixer.pre_init(44100, -16, 2, 512)

pygame.init()
mixer.init()

clock = pygame.time.Clock()
fps = 60

ancho = 1000
alto = 1000
bg = pygame.image.load("img/Background/inicio.jpg")
segu = pygame.image.load("img/Iconos/juega.png")
salgan = pygame.image.load("img/Iconos/quit.png")
jugador = pygame.image.load(f"img/player/Idle/0.png")
pantalla = pygame.display.set_mode((ancho, alto))
icon = pygame.display.set_caption("You")
opciones = pygame.image.load("img/Iconos/pauson.png")
resumen = pygame.image.load("img/Iconos/restart.png")

pygame.mixer.music.load("snd/feel.wav")
pygame.mixer.music.set_volume(0.1)
coin_fx = pygame.mixer.Sound("snd/coins.wav")
coin_fx.set_volume(0.5)
flip = pygame.mixer.Sound("snd/flip.wav")

# define font
font = pygame.font.SysFont("freesansbold", 104)
font_score = pygame.font.SysFont("freesansbold", 30)
# variables
money = 0
pressed = False
mantener = False
scroll = 0
pausa = False
pablo = False
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
pantalla.blit(img, (x, y))
class Button():
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.clicked = False

def draw(self):
action = False
# get mouse position
pos = pygame.mouse.get_pos()

# check mouseover and clicked conditions
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
action = True
self.clicked = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False

# draw button
pantalla.blit(self.image, self.rect)
# pygame.draw.rect(pantalla, (255, 255, 255), self.rect, 2)


return action
class Pausa:
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

def draw(self):
pantalla.blit(self.image, self.rect)
class Player:
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

def update(self): # update se utiliza para la posicion del jugador y su movilidad
dx = 0
dy = 0
key = pygame.key.get_pressed()
if key[pygame.K_a]:
dx -= 5
self.direction = -1
if key[pygame.K_d]:
dx += 5
self.direction = 1
if key[pygame.K_w]:
dy -= 5
if key[pygame.K_s]:
dy += 5
if key[pygame.K_z]:
pygame.mixer.music.play(-1, 0.0, 5000)
draw_text("Tienes " + str(money) + " monedas", font_score, (0, 0, 0), 800, 10)
# update player coordinates
self.rect.x += dx
self.rect.y += dy

def draw(self):
pantalla.blit(self.image, self.rect)
class Player2:
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

def update(self): # update se utiliza para la posicion del jugador y su movilidad
dx = 0
dy = 0
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
dx -= 5
self.direction = -1
if key[pygame.K_RIGHT]:
dx += 5
self.direction = 1
if key[pygame.K_UP]:
dy -= 5
if key[pygame.K_DOWN]:
dy += 5
if key[pygame.K_z]:
pygame.mixer.music.play(-1, 0.0, 5000)
draw_text("Tienes " + str(money) + " monedas", font_score, (0, 0, 0), 800, 10)
# update rectangle position
self.rect.x += dx
self.rect.y += dy
def draw(self):
pantalla.blit(self.image, self.rect)
jugador1 = Player(500, 500, jugador)
jugador2 = Player2(0, 0, jugador)
seguro = Button(500, 500, segu)
pausaoff = Pausa(0,0, opciones)
salir = Button(20, 420, salgan)
continuar = Button(20, 20, segu)
returno = Button(20, 300, resumen)
run = True
while run:
clock.tick(fps)
pantalla.blit(bg, (0, 0))

if pablo == True:
if seguro.draw():
pass
mantener = True
pygame.mixer.music.play(-1, 0.0, 5000)

if mantener == True:
Player.draw(jugador1)
Player.update(jugador1)
Player2.draw(jugador2)
Player2.update(jugador2)
pablo = False
if pausa == True:
pausaoff.draw()
mantener = False
if salir.draw():
flip.play()
pass
if returno.draw():
flip.play()
mantener = True
pausa = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_v:
money += 1
if money >= 10:
if event.key == pygame.K_x:
money -= 10
if event.key == pygame.K_r:
pausa = True
flip.play()
if event.key == pygame.K_ESCAPE:
pausa = False
if event.key == pygame.K_b:
pablo = True
if event.key == pygame.K_ESCAPE:
pablo = False
if event.type == pygame.KEYUP:
pass

pygame.display.update()

pygame.quit()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 2.808
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Ayuda con pygame, no se añadir ni sprites a mis personajes, ni hacer scroll ni hacer el fondo animad

Publicado por tincopasan (1082 intervenciones) el 31/12/2021 17:39:09
¿tenés idea lo dificil que es leer ese código sin la indentación correcta? El editor tiene el tag </<Código para que se pueda leer con la indentación que estés usando.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar