how to make a game with pygame code example
Example 1: how to setup pygame
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
FPS = 30
screen_width = 600
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
Example 2: pygame example
1
2
3
4 import pygame
5 pygame.init()
6
7
8 screen = pygame.display.set_mode([500, 500])
9
10
11 running = True
12 while running:
13
14
15 for event in pygame.event.get():
16 if event.type == pygame.QUIT:
17 running = False
18
19
20 screen.fill((255, 255, 255))
21
22
23 pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25
26 pygame.display.flip()
27
28
29 pygame.quit()
Example 3: simple game with python
import pygame
from pygame.locals import *
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
player = pygame.image.load("resources/images/dude.png")
while 1:
screen.fill(0)
screen.blit(player, (100,100))
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit(0)
Example 4: pygame tutorial
import pygame
pygame.init()
Example 5: pygame tutorial
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
Example 6: shooting game with pygame python tutorial
import pygame
pygame.windoe