Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson24-diy3
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
82ae3232
authored
Sep 26, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
144a3d9a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
3 deletions
q/main.py
winner.png
q/main.py
View file @
82ae3232
...
...
@@ -27,7 +27,11 @@ class Ball(pygame.sprite.Sprite):
self
.
rect
.
top
=
self
.
height
elif
self
.
rect
.
top
>
self
.
height
:
self
.
rect
.
bottom
=
0
def
check
(
self
,
motion
):
if
self
.
target
<
motion
<
self
.
target
+
5
:
return
True
else
:
return
False
def
collide_check
(
item
,
target
):
col_balls
=
[]
for
each
in
target
:
...
...
@@ -63,20 +67,24 @@ def main():
mouse_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\hand.png'
greenball_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\green_ball.png'
grayball_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\q\gray_ball.png'
running
=
True
bg_size
=
width
,
height
=
1048
,
681
screen
=
pygame
.
display
.
set_mode
(
bg_size
)
pygame
.
display
.
set_caption
(
'Play the ball demo'
)
background
=
pygame
.
image
.
load
(
bg_image
)
.
convert_alpha
()
msgs
=
[]
go_home
=
[]
hole
=
[(
100
,
130
,
180
,
210
),(
210
,
240
,
380
,
400
),(
490
,
515
,
310
,
340
),(
680
,
720
,
180
,
215
),(
890
,
920
,
400
,
435
)]
balls
=
[]
group
=
pygame
.
sprite
.
Group
()
ballnum
=
5
motion
=
0
#小球位置
for
i
in
range
(
ballnum
):
position
=
randint
(
0
,
width
-
100
),
randint
(
0
,
height
-
100
)
speed
=
[
randint
(
-
10
,
10
),
randint
(
-
10
,
10
)]
ball
=
Ball
(
gr
eenball_image
,
gray
ball_image
,
position
,
speed
,
bg_size
,
5
*
(
i
+
1
))
ball
=
Ball
(
gr
ayball_image
,
green
ball_image
,
position
,
speed
,
bg_size
,
5
*
(
i
+
1
))
while
pygame
.
sprite
.
spritecollide
(
ball
,
group
,
False
,
pygame
.
sprite
.
collide_circle
):
ball
.
rect
.
left
,
ball
.
rect
.
top
=
randint
(
0
,
width
-
100
),
randint
(
0
,
height
-
100
)
balls
.
append
(
ball
)
...
...
@@ -84,6 +92,8 @@ def main():
glass
=
Glass
(
glass_image
,
mouse_image
,
bg_size
)
clock
=
pygame
.
time
.
Clock
()
MYTIMER
=
USEREVENT
+
1
pygame
.
time
.
set_timer
(
MYTIMER
,
1000
)
while
running
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
QUIT
:
...
...
@@ -93,6 +103,52 @@ def main():
pygame
.
time
.
delay
(
2000
)
laugh_sound
.
play
()
running
=
False
elif
event
.
type
==
MOUSEMOTION
:
motion
+=
1
elif
event
.
type
==
MYTIMER
:
if
motion
:
for
ball
in
balls
:
if
ball
.
check
(
motion
):
ball
.
speed
=
[
0
,
0
]
ball
.
control
=
True
motion
=
0
elif
event
.
type
==
KEYDOWN
:
if
event
.
key
==
K_UP
:
for
each
in
balls
:
if
each
.
control
:
each
.
speed
[
1
]
-=
1
if
event
.
key
==
K_DOWN
:
for
each
in
balls
:
if
each
.
control
:
each
.
speed
[
1
]
+=
1
if
event
.
key
==
K_LEFT
:
for
each
in
balls
:
if
each
.
control
:
each
.
speed
[
0
]
-=
1
if
event
.
key
==
K_RIGHT
:
for
each
in
balls
:
if
each
.
control
:
each
.
speed
[
0
]
+=
1
if
event
.
key
==
K_SPACE
:
for
bal
in
balls
:
if
bal
.
control
:
for
i
in
hole
:
if
i
[
0
]
<=
bal
.
rect
.
left
<=
i
[
1
]
and
i
[
2
]
<=
bal
.
rect
.
top
<=
i
[
3
]:
hole_sound
.
play
()
bal
.
speed
=
[
0
,
0
]
go_home
.
append
(
bal
)
balls
.
remove
(
bal
)
ballnum
-=
1
hole
.
remove
(
i
)
if
len
(
hole
)
==
4
:
pygame
.
mixer
.
music
.
stop
()
winner_sound
.
play
()
pygame
.
time
.
delay
(
3000
)
winner_image
=
pygame
.
image
.
load
(
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\winner.png'
)
.
convert_alpha
msg_pos
=
(
100
,
100
)
msgs
.
append
((
winner_image
,
msg_pos
))
laugh_sound
.
play
()
screen
.
blit
(
background
,(
0
,
0
))
screen
.
blit
(
glass
.
glass_image
,
glass
.
glass_rect
)
#固定鼠标活动范围
...
...
@@ -108,12 +164,22 @@ def main():
screen
.
blit
(
glass
.
mouse_image
,
glass
.
mouse_rect
)
for
each
in
balls
:
each
.
move
()
if
each
.
control
:
screen
.
blit
(
each
.
greenball_image
,
each
.
rect
)
else
:
screen
.
blit
(
each
.
grayball_image
,
each
.
rect
)
for
each
in
go_home
:
screen
.
blit
(
each
.
greenball_image
,
each
.
rect
)
for
i
in
range
(
ballnum
):
item
=
balls
.
pop
(
i
)
if
collide_check
(
item
,
balls
):
if
item
.
control
==
False
:
item
.
speed
[
0
]
=-
item
.
speed
[
0
]
item
.
speed
[
1
]
=-
item
.
speed
[
1
]
else
:
item
.
speed
[
0
]
=
randint
(
-
10
,
10
)
item
.
speed
[
1
]
=
randint
(
-
10
,
10
)
item
.
control
=
False
balls
.
insert
(
i
,
item
)
# for each in group:
# group.remove(each)
...
...
@@ -121,6 +187,8 @@ def main():
# each.speed[0]=-each.speed[0]
# each.speed[1]=-each.speed[1]
# group.add(each)
for
winner_image
in
msgs
:
screen
.
blit
(
winner_image
[
0
],
winner_image
[
1
])
pygame
.
display
.
flip
()
clock
.
tick
(
30
)
main
()
winner.png
0 → 100644
View file @
82ae3232
8.07 KB
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment