Python implements Super Mario

The examples in this article share the specific code for writing Super Mario in Python for your reference. The specific content is as follows

Poke me with the complete code and material

Main code

import pygame as pg
from source.main import main

if __name__=='__main__':main()
 pg.quit()

main

__ author__ ='marble_xu'import pygame as pg
from.import setup, tools
from.import constants as c
from.states import main_menu, load_screen, level

def main():
 game = tools.Control()
 state_dict ={c.MAIN_MENU: main_menu.Menu(),
   c.LOAD_SCREEN: load_screen.LoadScreen(),
   c.LEVEL: level.Level(),
   c.GAME_OVER: load_screen.GameOver(),
   c.TIME_OUT: load_screen.TimeOut()}
 game.setup_states(state_dict, c.MAIN_MENU)
 game.main()

setup

__ author__ ='marble_xu'import os
import pygame as pg
from.import constants as c
from.import tools

pg.init()
pg.event.set_allowed([pg.KEYDOWN, pg.KEYUP, pg.QUIT])
pg.display.set_caption(c.ORIGINAL_CAPTION)
SCREEN = pg.display.set_mode(c.SCREEN_SIZE)
SCREEN_RECT = SCREEN.get_rect()

GFX = tools.load_all_gfx(os.path.join("resources","graphics"))

tools

__ author__ ='marble_xu'import os
import pygame as pg
from abc import ABC, abstractmethod

keybinding ={'action':pg.K_s,'jump':pg.K_a,'left':pg.K_LEFT,'right':pg.K_RIGHT,'down':pg.K_DOWN
} classState():
 def __init__(self):
 self.start_time =0.0
 self.current_time =0.0
 self.done = False
 self.next = None
 self.persist ={}
  
 @ abstractmethod
 def startup(self, current_time, persist):'''abstract method'''

 def cleanup(self):
 self.done = False
 return self.persist
  
 @ abstractmethod
 def update(sefl, surface, keys, current_time):'''abstract method'''classControl():
 def __init__(self):
 self.screen = pg.display.get_surface()
 self.done = False
 self.clock = pg.time.Clock()
 self.fps =60
 self.current_time =0.0
 self.keys = pg.key.get_pressed()
 self.state_dict ={}
 self.state_name = None
 self.state = None
  
 def setup_states(self, state_dict, start_state):
 self.state_dict = state_dict
 self.state_name = start_state
 self.state = self.state_dict[self.state_name]
  
 def update(self):
 self.current_time = pg.time.get_ticks()if self.state.done:
  self.flip_state()
 self.state.update(self.screen, self.keys, self.current_time)
  
 def flip_state(self):
 previous, self.state_name = self.state_name, self.state.next
 persist = self.state.cleanup()
 self.state = self.state_dict[self.state_name]
 self.state.startup(self.current_time, persist)

 def event_loop(self):for event in pg.event.get():if event.type == pg.QUIT:
  self.done = True
  elif event.type == pg.KEYDOWN:
  self.keys = pg.key.get_pressed()
  elif event.type == pg.KEYUP:
  self.keys = pg.key.get_pressed()
  
 def main(self):while not self.done:
  self.event_loop()
  self.update()
  pg.display.update()
  self.clock.tick(self.fps)

def get_image(sheet, x, y, width, height, colorkey, scale):
 image = pg.Surface([width, height])
 rect = image.get_rect()

 image.blit(sheet,(0,0),(x, y, width, height))
 image.set_colorkey(colorkey)
 image = pg.transform.scale(image,(int(rect.width*scale),int(rect.height*scale)))return image

def load_all_gfx(directory, colorkey=(255,0,255), accept=('.png','.jpg','.bmp','.gif')):
 graphics ={}for pic in os.listdir(directory):
 name, ext = os.path.splitext(pic)if ext.lower()in accept:
  img = pg.image.load(os.path.join(directory, pic))if img.get_alpha():
  img = img.convert_alpha()else:
  img = img.convert()
  img.set_colorkey(colorkey)
  graphics[name]= img
 return graphics

Operation results

Okay, I forgot to click star in GitHub.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements Super Mario
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements parking management system
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
Python implements the brick-and-mortar game
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down
Python implements QQ mailbox to send mail
Python implements alternate execution of two threads
Python implements the sum of fractional sequences
python implements the gradient method python the fastest descent method
Python implements singly linked lists and dictionaries
python3 simply implements the combined design pattern