Pygame Platform tutorial part 1

The first video is the first of the series by Coding with Russ about one of the favourite topic about videogames: platform games. Russ has made a great kob here for our favourite module ‘pygame’.

First tutorial about this platform game by Coding with Russ

Lets summarize the first tutorial

import and initialize pygame
Create the surface “screen” where everything is displayed every frame
This will make a window to appear so that you can close it with the x button
Press ctrl + b in sublime text and you get your window (or open cmd and do python game.py if you called the script game,py)

Load the image for the player

the get_rect() makes you get the size of the image and rect.center gets the center
To see it you got to add the blit method with the img object and the position using rect, pygame.display.update() is to see it on the screen
There he is
Being tiny, let’s scale it

If we want to scale it, we need to multiply the width and height of the image for… 3, for example (scale = 3)

This class is for the player, load the image, scale it etc.
This changes will make the class work fine for the game loop
Now you can use the draw method in the while loop to show the player
here we go

See you for the next episode. The repository is here.

import pygame

pygame.init()


SCREEN_WIDTH = 800
SCREEN_HEIGHT = int(SCREEN_WIDTH * 0.8)

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Shooter')


class Soldier(pygame.sprite.Sprite):
	def __init__(self, x, y, scale):
		pygame.sprite.Sprite.__init__(self)
		img = pygame.image.load('img/player/Idle/0.png')
		self.image = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
		self.rect = self.image.get_rect()
		self.rect.center = (x, y)

	def draw(self):
		screen.blit(self.image, self.rect)



player = Soldier(200, 200, 3)
player2 = Soldier(400, 200, 3)



run = True
while run:

	
	player.draw()
	player2.draw()

	for event in pygame.event.get():
		#quit game
		if event.type == pygame.QUIT:
			run = False


	pygame.display.update()

pygame.quit()
0.png


Subscribe to the newsletter for updates
Tkinter templates

Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Claude's Games

Arkanoid
Platform 2d

1. Memory game

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Advertisement