Skip to content

Commit 5d10de7

Browse files
author
youssif-sully
committed
first
1 parent 3fca4ae commit 5d10de7

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

Player.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from init import *
2+
3+
4+
class Player(pygame.sprite.Sprite):
5+
def __init__(self):
6+
pygame.sprite.Sprite.__init__(self)
7+
self.image = pygame.Surface((50, 50))
8+
self.image.fill(RED)
9+
self.rect = self.image.get_rect()
10+
self.rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
11+
12+
def update(self):
13+
self.rect.x += 10
14+
if self.rect.left > SCREEN_WIDTH:
15+
self.rect.right = 0

colors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# RGB colors
2+
# https://rgbcolorcode.com/
3+
4+
RED = (255, 0, 0)
5+
GREEN = (0, 255, 0)
6+
BLUE = (0, 0, 255)
7+
8+
9+
BLACK = (0, 0, 0)
10+
WHITE = (255, 255, 255)
11+

config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SCREEN_WIDTH = 360
2+
SCREEN_HEIGHT = 480
3+
FPS = 30

init.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pygame
2+
from colors import *
3+
from config import *
4+
from Player import *
5+
6+
# initialize pygame
7+
pygame.init()
8+
9+
# initialize the sound
10+
pygame.mixer.init()
11+
12+
# initialize the screen and set the sizes (from config.py)
13+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
14+
15+
# set the caption (above the screen)
16+
pygame.display.set_caption('First game')
17+
18+
# set the clock
19+
clock = pygame.time.Clock()

main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from init import *
2+
3+
# -------------------- START ----- SPRITE ---------------------------------
4+
5+
# all sprites in one group (sprites_group)
6+
sprites_group = pygame.sprite.Group()
7+
8+
# ==================== START ===== ADDING PLAYER SPRITE ===================
9+
10+
player = Player()
11+
sprites_group.add(player)
12+
13+
# ==================== END === ADDING PLAYER SPRITE =======================
14+
15+
# -------------------- END --- SPRITE -------------------------------------
16+
17+
is_running = True
18+
while is_running:
19+
20+
# -------------------- Setting the FPS ---------------------------------
21+
22+
clock.tick(FPS)
23+
24+
# -------------------- Setting the FPS ---------------------------------
25+
26+
# -------------------- START --- EVENTS --------------------------------
27+
28+
# group all the events and check them
29+
for event in pygame.event.get():
30+
31+
# event (closing the window by pressing the x button)
32+
if event.type == pygame.QUIT:
33+
is_running = False
34+
35+
# -------------------- END ----- EVENTS --------------------------------
36+
37+
# -------------------- START --- UPDATE --------------------------------
38+
39+
# update sprites_group (all sprites)
40+
sprites_group.update()
41+
42+
# -------------------- END ----- UPDATE --------------------------------
43+
44+
# -------------------- START --- RENDER --------------------------------
45+
46+
# init is the module and screen is a variable within
47+
screen.fill(BLACK)
48+
# draw sprites_group (all sprites)
49+
sprites_group.draw(screen)
50+
51+
# double buffering: draw all then display
52+
# always AFTER the drawing
53+
pygame.display.flip()
54+
55+
# -------------------- END ----- RENDER --------------------------------
56+
57+
pygame.quit()

0 commit comments

Comments
 (0)