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
5109a5f3
authored
Sep 12, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
32785524
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
5 deletions
d.py/main.py
d.py/main.py
View file @
5109a5f3
...
...
@@ -2,6 +2,7 @@ import pygame
import
sys
from
pygame.locals
import
*
from
random
import
*
import
math
class
Ball
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
,
position
,
speed
,
bg_size
):
pygame
.
sprite
.
Sprite
.
__init__
(
self
)
...
...
@@ -20,6 +21,15 @@ class Ball(pygame.sprite.Sprite):
self
.
rect
.
top
=
self
.
height
elif
self
.
rect
.
top
>
self
.
height
:
self
.
rect
.
bottom
=
0
def
collide_check
(
item
,
target
):
col_balls
=
[]
for
each
in
target
:
distance
=
math
.
sqrt
(
\
math
.
pow
((
item
.
rect
.
center
[
0
]
-
each
.
rect
.
center
[
0
]),
2
)
+
\
math
.
pow
((
item
.
rect
.
center
[
1
]
-
each
.
rect
.
center
[
1
]),
2
))
if
distance
<=
(
item
.
rect
.
width
+
each
.
rect
.
width
)
/
2
:
col_balls
.
append
(
each
)
return
col_balls
def
main
():
pygame
.
init
()
ball_image
=
"gray_ball.png"
...
...
@@ -30,11 +40,16 @@ def main():
pygame
.
display
.
set_caption
(
"Play the ball - Demo"
)
background
=
pygame
.
image
.
load
(
bg_image
)
.
convert_alpha
()
balls
=
[]
for
i
in
range
(
5
):
group
=
pygame
.
sprite
.
Group
()
BALL_NUM
=
5
for
i
in
range
(
BALL_NUM
):
position
=
randint
(
0
,
width
-
100
),
randint
(
0
,
height
-
100
)
speed
=
[
randint
(
-
10
,
10
),
randint
(
-
10
,
10
)]
ball
=
Ball
(
ball_image
,
position
,
speed
,
bg_size
)
balls
.
append
(
ball
)
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
)
group
.
add
(
ball
)
clock
=
pygame
.
time
.
Clock
()
while
running
:
for
event
in
pygame
.
event
.
get
():
...
...
@@ -44,7 +59,13 @@ def main():
for
each
in
balls
:
each
.
move
()
screen
.
blit
(
each
.
image
,
each
.
rect
)
for
each
in
group
:
group
.
remove
(
each
)
if
pygame
.
sprite
.
spritecollide
(
each
,
group
,
False
,
pygame
.
sprite
.
collide_circle
):
each
.
speed
[
0
]
=
-
each
.
speed
[
0
]
each
.
speed
[
1
]
=
-
each
.
speed
[
1
]
group
.
add
(
each
)
pygame
.
display
.
flip
()
clock
.
tick
(
30
)
if
__name__
==
"__main__"
:
main
()
\ No newline at end of file
main
()
\ No newline at end of file
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