-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelloWorld_pygame.py
219 lines (188 loc) · 6.48 KB
/
HelloWorld_pygame.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
# Módulos | imports
# ---------------------------------------------------------------------
# region
import sys
import pygame
from pygame.locals import *
from sympy import Point, pi, sin, cos
# endregion
# ---------------------------------------------------------------------
# Constantes | constants declarations
# ---------------------------------------------------------------------
# region
WIDTH = 640
HEIGHT = 480
FRAMES_PER_SECOND = 30
# endregion
# ---------------------------------------------------------------------
# Variables Globales | global variables
# ---------------------------------------------------------------------
# region
win_sfc = None # initialize global for testing drawing on game surface
render_i = None # gets incremented every draw loop
# endregion
# ---------------------------------------------------------------------
# Clases | local classes
# ---------------------------------------------------------------------
# region
# endregion
# ---------------------------------------------------------------------
# Funciones | local functions
# ---------------------------------------------------------------------
# region
# draw white circle with radius of 25
def drawCircle():
# set up position veriables
circ_pos = Point(100, 240)
circ_radius = 25
# draw circle
color = Color(255, 255, 255)
stroke = 0 # with 0 stroke, circle will be filled
pygame.draw.circle(win_sfc, color, circ_pos, circ_radius, stroke)
pass
# draw white circle outline with radius of 25, stroke of 3
def drawCircleOutline():
# set up position variables
circ_pos = Point(175, 240)
circ_radius = 25
# draw circle outline
color = Color(255, 255, 255)
stroke = 3
pygame.draw.circle(win_sfc, color, circ_pos, circ_radius, stroke)
pass
# draw a red 3/4-circle arc from pi/2 to 2pi with stroke of 5
def drawArc():
# set up position variables
arc_pos = Point(275, 240)
arc_radius_x = 50
arc_radius_y = 25
arc_rect = Rect(
arc_pos.x - arc_radius_x,
arc_pos.y - arc_radius_y,
arc_radius_x * 2,
arc_radius_y * 2,
)
arc_start = pi / 2
arc_end = 2*pi
# draw arc
stroke = 5
color = Color(255, 0, 0)
pygame.draw.arc(win_sfc, color, arc_rect, arc_start, arc_end, stroke)
pass
# draw a solid black rectangle with a blue-gray outline
def drawRectangle():
# set up position variables
rect_pos = Point(350, 215)
rect_width = 150
rect_height = 50
rect = Rect(rect_pos.x, rect_pos.y, rect_width, rect_height)
# draw solid rectangle
color = Color(0, 0, 0)
pygame.draw.rect(win_sfc, color, rect)
# draw rect outline
color = Color(63, 63, 127)
stroke = 5
pygame.draw.rect(win_sfc, color, rect, stroke)
pass
# endregion
# ---------------------------------------------------------------------
# Funcion principal | main function definition
# ---------------------------------------------------------------------
def main():
# Crear una ventana | create window
# -------------
# region
pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pruebas Pygame | Pygame Tutorial")
# get instance of window surface and store in global "win_sfc"
# to use later in drawing funcitons
global win_sfc
win_sfc = pygame.display.get_surface()
print(f'Got reference to surface "{win_sfc}"')
# endregion#
# -------------
# enter main draw / game loop
# -------------
global render_i
render_i = 0
while True:
# draw elements
# -------------
# region
# clear background with dark grey
win_sfc.fill((63, 63, 63))
# draw foreground elements on top of background
drawCircle()
drawCircleOutline()
drawArc()
drawRectangle()
# endregion
# -------------
# update display (must be done every render loop)
# ----------
pygame.display.update()
pygame.time.delay(1000//FRAMES_PER_SECOND) # wait fraction of a second betwen updates
# -------------
# increment render count
# ----------
render_i = render_i + 1
# -------------
# handle events
# ----------
for evento in pygame.event.get():
# keyboard input
# ----------
if evento.type == pygame.KEYDOWN or evento.type == pygame.KEYUP:
# handle modifier key presses
# ----------
if evento.mod == pygame.KMOD_NONE:
print('No modifier keys were in a pressed state when this '
'event occurred.')
else:
if evento.mod & pygame.KMOD_LSHIFT:
print('Left shift was in a pressed state when this event '
'occurred.')
if evento.mod & pygame.KMOD_RSHIFT:
print('Right shift was in a pressed state when this event '
'occurred.')
if evento.mod & pygame.KMOD_SHIFT:
print('Left shift or right shift or both were in a '
'pressed state when this event occurred.')
pass
pass
# ----------
# handle general key presses
# ----------
print(f'Key event captured for key named "{pygame.key.name(evento.key)}"')
# ----------
# exit application on "Esc" pressed
# ----------
if evento.key == pygame.K_ESCAPE:
print("\nyou pressed ESCAPE, so exiting...\n")
sys.exit(0)
pass
# ----------
pass
# -------------
# exit application on window quit event
# ----------
if evento.type == QUIT:
sys.exit(0)
pass
# -------------
pass
# -------------
pass
# -------------
return 0
pass
# ---------------------------------------------------------------------
# Ejecutar solo si se ejecuta como aplicación principal
# --
# Only execute if run as main application
# ---------------------------------------------------------------------
if __name__ == '__main__':
pygame.init()
main()
pass
# ---------------------------------------------------------------------