-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyGameEngine.py
227 lines (180 loc) · 6.22 KB
/
pyGameEngine.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#Created by Buddhima Zoysa on 27th of April 2020
#This is just a wrapper to pygame module (https://www.pygame.org) that provides funcionality like One Lone Coder's olcPixelGameEngine.h (which you can find here https://github.com/OneLoneCoder/olcPixelGameEngine)
#This may not be perfect I actually don't use python that often so there may be so many bugs and bad programming
#If someone find this useful go ahead download it experiment with it (And this only tested on windows)
#Good luck :)
#P.S. Sorry for my Bad English
#
#Dependancies : Just pygame module (to install pygame just run "pip install pygame")
from threading import Thread
import time
import math
import pygame
from pygame import gfxdraw
class pyGameEngine(Thread):
Keys = {
'A':pygame.K_a,
'B':pygame.K_b,
'C':pygame.K_c,
'D':pygame.K_d,
'E':pygame.K_e,
'F':pygame.K_f,
'G':pygame.K_g,
'H':pygame.K_h,
'I':pygame.K_i,
'J':pygame.K_j,
'K':pygame.K_k,
'L':pygame.K_l,
'M':pygame.K_m,
'N':pygame.K_n,
'O':pygame.K_o,
'P':pygame.K_p,
'Q':pygame.K_q,
'R':pygame.K_r,
'S':pygame.K_s,
'T':pygame.K_t,
'U':pygame.K_u,
'V':pygame.K_v,
'W':pygame.K_w,
'X':pygame.K_x,
'Y':pygame.K_y,
'Z':pygame.K_z,
'0':pygame.K_0,
'1':pygame.K_1,
'2':pygame.K_2,
'3':pygame.K_3,
'4':pygame.K_4,
'5':pygame.K_5,
'6':pygame.K_6,
'7':pygame.K_7,
'8':pygame.K_8,
'9':pygame.K_9,
'UP':pygame.K_UP,
'DOWN':pygame.K_DOWN,
'LEFT':pygame.K_LEFT,
'RIGHT':pygame.K_RIGHT
}
def __init__(self):
self.sAppName = 'Defualt'
self.sIconPath = ''
def Construct(self, nScreenWidth, nScreenHeight):
self.nScreenWidth = nScreenWidth
self.nScreenHeight = nScreenHeight
pygame.init()
self.screen = pygame.display.set_mode((self.nScreenWidth,self.nScreenHeight))
if self.sIconPath != '':
icon = pygame.image.load(self.sIconPath)
pygame.display.set_icon(icon)
self.bAtomRunning = True
def ScreenHeight(self):
return self.nScreenHeight
def ScreenWidth(self):
return self.nScreenWidth
def SetPixel(self, x, y, color = (255, 255, 255)):
x = int(x); y = int(y)
if x >= 0 and x < self.nScreenWidth and y >= 0 and y < self.nScreenHeight:
pygame.gfxdraw.pixel(self.screen,x,y,color)
def DrawLine(self, x1, y1, x2, y2, color = (255, 255, 255)):
if x1 > x2:
x1, x2 = x2, x1
y1, y2 = y2, y1
dx = x2 - x1
dy = y2 - y1
if dx != 0:
m = float(dy) / float(dx)
for xi in range(dx + 1):
x = xi + x1
y = m * xi + y1
self.SetPixel(x, y, color)
else:
for yi in range(dy + 1):
y = yi + y1
self.SetPixel(x1, y, color)
if dy != 0:
m = float(dx) / float(dy)
for yi in range(dy + 1):
y = yi + y1
x = m * yi + x1
self.SetPixel(x, y, color)
def DrawTriangle(self, x1, y1, x2, y2, x3, y3, color = (255,255,255)):
self.DrawLine(x1, y1, x2, y2, color)
self.DrawLine(x2, y2, x3, y3, color)
self.DrawLine(x3, y3, x1, y1, color)
def FillTriangle(self, x1, y1, x2, y2, x3, y3, color = (255,255,255)):
if y1 > y2:
x1, x2 = x2, x1
y1, y2 = y2, y1
if y1 > y3:
x1, x3 = x3, x1
y1, y3 = y3, y1
if y2 > y3:
x2, x3 = x3, x2
y2, y3 = y3, y2
dxA = x2 - x1
dyA = y2 - y1
dxB = x3 - x1
dyB = y3 - y1
dax_step = 0.0
if dyA > 0 :
dax_step = float(dxA) / math.fabs(float(dyA))
if dyB > 0 :
dbx_step = float(dxB) / math.fabs(float(dyB))
if dyA > 0:
for y in range(y1, y2):
ax = x1 + int(float(y - y1) * dax_step)
bx = x1 + int(float(y - y1) * dbx_step)
if ax > bx:
ax, bx = bx, ax
if (bx - ax) > 0:
tstep = 1.0 / float(bx - ax)
t = 0.0
for x in range(ax,bx):
self.SetPixel(x, y, color)
t += tstep
dxA = x3 - x2
dyA = y3 - y2
if dyA > 0 :
dax_step = float(dxA) / math.fabs(float(dyA))
if dyA > 0:
for y in range(y2, y3):
ax = x2 + int(float(y - y2) * dax_step)
bx = x1 + int(float(y - y1) * dbx_step)
if ax > bx:
ax, bx = bx, ax
if (bx - ax) > 0:
tstep = 1.0 / float(bx - ax)
t = 0.0
for x in range(ax,bx):
self.SetPixel(x, y, color)
t += tstep
def Fill(self, color = (0, 0, 0)):
self.screen.fill(color)
def OnUserCreate(self):
return True
def OnUserUpdate(self, fElapsedTime):
return True
def GameThread(self):
self.keyHolding = pygame.key.get_pressed()
if not self.OnUserCreate():
self.bAtomRunning = False
tp1 = float(time.time())
tp2 = float(time.time())
while self.bAtomRunning:
#Handling Timing
tp2 = float(time.time())
fElapsedTime = tp2 - tp1
tp1 = tp2
#Handling Inputs
#Handling keyboard inputs
self.keyHolding = pygame.key.get_pressed()
#Handling events
for event in pygame.event.get():
#Check if the quit event has occured
if event.type == pygame.QUIT:
self.bAtomRunning = False
if not self.OnUserUpdate(fElapsedTime):
self.bAtomRunning = False
pygame.display.update()
pygame.quit()
def Start(self):
self.GameThread()