Python-Day07- graphical user interface and game development

Learning Python-100Day- punch
the Author: Seven_0507
a Date: 2019-05-22
. 1
2
. 3

Article directories
Python graphical user interface and game development
1. tkinter module
2. Pygame game development
Python graphical user interface and game development
1. tkinter module
"" "
Use tkinter create the GUI
- top-level window
- Controls
- Layout
- event callback
" ""
Tkinter Import
Import tkinter.messagebox


main DEF ():
# Class Properties In Flag
In Flag = True

# Modify the text on the label
DEF change_label_text ():
nonlocal In Flag # nonlocal keyword for the outer layer (non-global) variables in function or other scopes
In Flag = Not In Flag
IF In Flag:
Color, MSG = ( 'Red', 'the Hello, World!')
the else:
Color, MSG = ( 'Blue', 'Goodbye, World!')
label.config (text = MSG, FG = Color)

# To confirm exit
DEF confirm_to_quit ():
IF tkinter.messagebox.askokcancel ( 'Tips', 'Are you sure you want to quit?'):
Top.quit ()

# Create a top-level window
Top tkinter.Tk = ()
# set window size
top.geometry ( '240x160')
# Set the window title
top.title ( 'mini-games')
# Create a label object and added to the top-level window
label = tkinter.Label (Top, text = 'the Hello, World!', = font 'Arial -32', FG = 'Red')
label.pack (= the expand. 1)
# Create a button mounted container
Panel = tkinter.Frame (Top)
# create button to add the object to which the container specified in bind event callbacks by command parameters
button1 = tkinter.Button (panel, text = ' modify', command = change_label_text)
button1.pack (Side = 'left')
Button2 = tkinter.Button (panel, text = 'exit', the Command = confirm_to_quit)
button2.pack (Side = 'right')
panel.pack (Side = 'bottom')
# turn on the main event loop
tkinter.mainloop ()


if __name__ == '__main__':
main()
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
"" "
Use tkinter create the GUI
- making on the window animation
" ""
Import tkinter
Import Time


# Animation playback function
DEF play_animation ():
canvas.move (Oval, 2, 2)
canvas.update ()
top.after (50, play_animation)


x = 10
y = 10
top = tkinter.Tk()
top.geometry('600x600')
top.title('动画效果')
top.resizable(False, False)
top.wm_attributes('-topmost', 1)
canvas = tkinter.Canvas(top, width=600, height=600, bd=0, highlightthickness=0)
canvas.create_rectangle(0, 0, 600, 600, fill='gray')
oval = canvas.create_oval(10, 10, 60, 60, fill='red')
canvas.pack()
top.update()
play_animation()
tkinter.mainloop()

# Consider how to hit the ball on the edge of the screen bounce
# Consider how the object-oriented programming ideas of the above code package
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
"" "
with the graphics module turtle
this is a very interesting module that simulates a turtle manner on the window for drawing crawling
" ""
Import turtle

turtle.pensize (. 3)
turtle.penup ()
turtle.goto (-180, 150)
turtle.pencolor ( 'Red')
turtle.fillcolor ( 'Yellow')
turtle.pendown ()
turtle.begin_fill ()
for _ in Range (36):
turtle.forward (200 is)
turtle.right (170.)
turtle.end_fill ()
turtle.mainloop ()
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
2. the Pygame game development
"" "
big eat the small ball game ball step
- making the game window
- drawing in the window
- the image is loaded
- for animations
- collision detection
." ""
from enum import Enum, unique
from math import sqrt
from random import randint

import pygame

# Collision detection
@Unique
class Color (the Enum):
"" "Color" ""

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (242, 242, 242)

@staticmethod
DEF random_color ():
"" "obtained random color" ""
R & lt the randint = (0, 255)
G = the randint (0, 255)
B = the randint (0, 255)
return (R & lt, G, B)


class Ball(object):
"""球"""

def __init__(self, x, y, radius, sx, sy, color=Color.RED):
"""初始化方法"""
self.x = x
self.y = y
self.radius = radius
self.sx = sx
self.sy = sy
self.color = color
self.alive = True

def move(self, screen):
"""移动"""
self.x += self.sx
self.y += self.sy
if self.x - self.radius <= 0 or self.x + self.radius >= screen.get_width():
self.sx = -self.sx
if self.y - self.radius <= 0 or self.y + self.radius >= screen.get_height():
self.sy = -self.sy

def eat(self, other):
"""吃其他球"""
if self.alive and other.alive and self != other:
dx, dy = self.x - other.x, self.y - other.y
distance = sqrt(dx ** 2 + dy ** 2)
if distance < self.radius + other.radius \
and self.radius > other.radius:
other.alive = False
self.radius = self.radius + int(other.radius * 0.146)

def draw(self, screen):
"""在窗口上绘制球"""
pygame.draw.circle(screen, self.color,
(self.x, self.y), self.radius, 0)

# Event Processing
def main ():
the container used to hold all #define balls
Balls = []
# introduced in the pygame initialization module
pygame.init ()
# window initialization for display and window size setting
screen = pygame.display .set_mode ((800, 600))
Print (screen.get_width ())
Print (screen.get_height ())
# set current window title
pygame.display.set_caption ( 'eat a big ball small ball')
# define a variable to represent ball position on the screen of
the X-, the y-= 50, 50
running = True
#-on event processing takes place in an event loop
the while running:
# get events from the message queue and event processing
for event in pygame.event.get ( ):
IF Event.type == pygame.quit:
running = False
IF Event.type == == pygame.MOUSEBUTTONDOWN and event.button. 1:
X, Y = event.pos
RADIUS = the randint (10, 100)
sx, sy = randint(-10, 10), randint(-10, 10)
color = Color.random_color()
ball = Ball(x, y, radius, sx, sy, color)
balls.append(ball)
screen.fill((255, 255, 255))
for ball in balls:
if ball.alive:
ball.draw(screen)
else:
balls.remove(ball)
pygame.display.flip()
# 每隔50毫秒就改变小球的位置再刷新窗口
pygame.time.delay(50)
for ball in balls:
ball.move(screen)
for other in balls:
ball.eat(other)


if __name__ == '__main__':
main()
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
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
800
600
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10983045.html