I create the game "Mario." algorithm). It is only necessary that this all happen gradually, not immediately. Code:

import sys,time,pygame,random pygame.init() playSurface = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Марио") fpsController = pygame.time.Clock() white = pygame.Color(255,255,255) x1 = 100 y1 = 300 mario = pygame.image.load("Mario.png") mario = pygame.transform.scale(mario,(50,60)) move_2 = False move_3 = False move_4 = False def music(): pygame.mixer.init() pygame.mixer.music.load('music.mp3') pygame.mixer.music.play(loops=-1) while True: playSurface.blit (mario, [x1, y1]) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit () sys.exit () elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit () sys.exit() if event.key == ord("m"): music() if event.key == ord ("n"): pygame.mixer.music.stop () if event.key == pygame.K_RIGHT: move_2 = True if event.key == pygame.K_SPACE: move_3 = True if event.key == pygame.K_LEFT: move_4 = True if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT: move_2 = False if event.key == pygame.K_UP: move_3 = False if event.key == pygame.K_LEFT: move_4 = False # if event.type == pygame.MOUSEBUTTONDOWN: # x, y = event.pos if x1 >= 500: y1 += 5 if move_2 == True: x1 += 5 if x1 >= 730: x1 = 750 if move_3 == True: if move_4 == True: x1 -= 5 if x1 < 15: x1 = 15 pygame.display.flip() playSurface.fill(white) fpsController.tick(15) 
  • 3
    Particularly pleased with the part // Тут вам надо написать код , there will be no one for you who will not write anything, this is for you to freelance forums. - Igor Igoryanych

2 answers 2

Function in class Player

class Player (): def init (self): # the rest self.jumpforce = 0 self.gravityforce = 0 self.maxgravityforce = 50

  def gravity(self): #Событие начала прыжка игрока self.jumpforce=10 self.gravityforce=0 def update(self,...): if self.gravityforce>self.maxgravityforce: self.gravityforce=self.maxgravityforce if self.jumpforce>0: if self.gravityforce<self.maxgravityforce: self.gravityforce+=1 self.y-=self.jumpforce self.y+=self.gravityforce 

This is an example. Simply, there is a force of jump and gravity, a little bit, the force of gravity grows and “overlaps” the force of the jump, which makes the fall smooth.

    This principle (add to it what you need):

     import sys import time import random import datetime import threading import pygame pygame.init() pygame.display.set_caption("Марио") SCOPE = {} SCOPE['th'] = None SCOPE['x1'] = 100 SCOPE['y1'] = 300 WHITE = pygame.Color(255,255,255) FPS_CONTROLLER = pygame.time.Clock() PLAY_WINDOW = pygame.display.set_mode((800, 600)) MARIO = pygame.transform.scale(pygame.image.load("mario.png"), (50, 60)) move_2 = False move_3 = False move_4 = False def move_mario(play_vindow, mario, scope): start_time = datetime.datetime.now() while (datetime.datetime.now() - start_time) < datetime.timedelta(minutes=3): scope['y1'] += 5 play_vindow.blit(mario, (scope['x1'], scope['y1'])) time.sleep(5) time.sleep(60 * 3) scope['y1'] = 0 play_vindow.blit(mario, (scope['x1'], scope['y1'])) scope['th'] = None def init_music(): pygame.mixer.init() pygame.mixer.music.load('music.mp3') pygame.mixer.music.play(loops=-1) while True: PLAY_WINDOW.blit (MARIO, [SCOPE['x1'], SCOPE['y1']]) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() elif event.key == ord("m"): init_music() elif event.key == ord("n"): pygame.mixer.music.stop() move_2 = (event.key == pygame.K_RIGHT) move_3 = (event.key == pygame.K_SPACE) move_4 = (event.key == pygame.K_LEFT) elif event.type == pygame.KEYUP: move_2 = (event.key != pygame.K_RIGHT) move_3 = (event.key != pygame.K_UP) move_4 = (event.key != pygame.K_LEFT) if SCOPE['x1'] >= 500: SCOPE['y1'] += 5 if move_2: SCOPE['x1'] += 5 if SCOPE['x1'] >= 730: SCOPE['x1'] = 750 elif move_3 and (SCOPE['th'] is None): SCOPE['th']= threading.Thread(target=move_mario, args=(PLAY_WINDOW, MARIO, SCOPE)) SCOPE['th'].start() elif move_4: SCOPE['x1'] -= 5 if SCOPE['x1'] < 15: SCOPE['x1'] = 15 pygame.display.flip() PLAY_WINDOW.fill(WHITE) FPS_CONTROLLER.tick(15) 
    • When I run this code in the game, I get 2 Mario pictures, one of which is on top and the second one is below, and they repeat the movements one by one. And one more picture of Mario is trying to appear - user269883
    • There is also an error: start_new_thread (self._bootstrap, ()) RuntimeError: can't start new thread - user269883
    • full traceback errors throw - Andrio Skur
    • Maybe I will change the question, what would be easier for me and you? And you delete or change this answer - user269883