The idea is to make a game with horses that runs to win a race. Random movements will lead one to win. You should bet on the one that should win. Let’s start with no further ado. In the first part of these series of “tutorial” / live wip we will make the whole game but in a very raw version that we will refine in the next posts.
Yesterday we made an empty window. Today we will ful that window with images of horses and we will make them run for a race. Cute, isn’t it?
This is an image of the game.
The code is here
import pygame import sys import random pygame.init() width = 1000 height = 800 size = width, height screen = pygame.display.set_mode(size) clock = pygame.time.Clock() horses = pygame.sprite.Group() class Horse(pygame.sprite.Sprite): def __init__(self, horsename, x, y, color): super().__init__() # self.image = pygame.Surface((50,50)) self.horsename = horsename self.image = pygame.image.load(f"img\\{self.horsename}.png") self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y print(self.rect.w) self.color = color # self.image.fill(self.color) horses.add(self) self.time = 0 def update(self): global game, run if game: if self.rect.x < 900: self.rect.x += random.randrange(0, 5) #self.time else: game = 0 print(f"{self.horsename } wins!!!!") else: self.time += 1 if self.time > 200: run = 0 game = 1 horse1 = Horse("horse1", 10, 100, "white") horse2 = Horse("horse2", 10, 200, "blue") horse3 = Horse("horse3", 10, 300, "red") horse4 = Horse("horse4", 10, 400, "yellow") horse5 = Horse("horse5", 10, 500, "green") run = 1 while run: screen.fill(0) for event in pygame.event.get(): if event.type == pygame.QUIT: run = 0 horses.update() horses.draw(screen) pygame.display.flip() clock.tick(60) pygame.quit() sys.exit()
It’s not the most sophisticated game, to use an euphemism, but I think it’s a good way to start understanding how to make a game with pygame. I, in fact, do not think you (or me) can learn starting from a tutorial about making a 2d platform game like mario if your do not start understanding the very basic stuffs. There may be that there are people that can do it, to start from a complex kind of game, but it’s not my case, so if you think like me, I will do other games in the next days (or at least this is my actual purpose ☺). So, make your variations of this game. I will put these examples in the repository below.
https://github.com/formazione/pygame_tutorial.git
Open tkinter windows from Pygame
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame