-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacMan_Eng.py
95 lines (76 loc) · 2.59 KB
/
pacMan_Eng.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# - * - coding: utf-8 - * -
# library loading
import pygame
# library initialization
pygame.init ()
# setting the screen size
sizeScr = (800, 600)
screen = pygame.display.set_mode (sizeScr)
# title for window
pygame.display.set_caption ('Pacman')
# timer creation
clock = pygame.time.Clock ()
# image loading for pacman
privedenie = pygame.image.load ('./ pac1.png')
# resize -> 100x100 pixels
privedenie = pygame.transform.scale (privedenie, (100,100))
# loading image for cast
pacMan = pygame.image.load ('./ pac2.png')
# resize -> 100x100 pixels
pacMan = pygame.transform.scale (pacMan, (100,100))
# coordinates of pacman
pacX = 0
pacY = 0
# cast coordinates
privedenieX = 400
privedenieY = 400
# flag to exit the program (necessary for the correct closure of the program)
runApp = true
# -------- program loop -----------
# execute while runApp is True
while runApp:
# --- reading the list of events
for event in pygame.event.get ():
# if there is a QUIT event in the list, then close the program
if event.type == pygame.QUIT:
runApp = False
# reading key pressed
keyP = pygame.key.get_pressed ()
# if left key is pressed
if keyP [pygame.K_LEFT]:
# reduce x coordinate
pacX = pacX - 3
if keyP [pygame.K_RIGHT]:
pacManPovernutii = pygame.transform.rotate (pacMan, 0)
# increase x coordinate
pacX = pacX + 3
if keyP [pygame.K_UP]:
pacManPovernutii = pygame.transform.rotate (pacMan, 90)
pacY = pacY - 3
if keyP [pygame.K_DOWN]:
pacManPovernutii = pygame.transform.rotate (pacMan, 270)
pacY = pacY + 3
# --- logic of game
# if pacman falls into cast coordinates +/- 50 pixels in X and then in Y
if (pacX> privedenieX - 50) & (pacX <privedenieX + 50) & (privedenieX == 400):
if (pacY> privedenieY - 50) & (pacY <privedenieY + 50):
# transfer the cast to coordinates (0, 0)
privedenieX = 0
privedenieY = 0
elif (pacX> privedenieX - 50) & (pacX <privedenieX + 50) & (privedenieX! = 400):
if (pacY> privedenieY - 50) & (pacY <privedenieY + 50):
# add 50 pixels to cast coordinates
privedenieX + = 50
privedenieY + = 50
# --- Screen redraw
# Here we draw the background
screen.fill ((200,200,200))
# Here we draw objects
screen.blit (privedenie, (privedenieX, privedenieY))
screen.blit (pacMan, (pacX, pacY))
# --- Update screen
pygame.display.flip ()
# --- Waiting for 60 timer ticks
clock.tick (60)
# exit from the program
pygame.quit ()