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
531b542f
authored
Sep 12, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
2e795643
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
3 deletions
ccc/main.py
ccc/main.py
View file @
531b542f
...
@@ -2,6 +2,9 @@ import pygame
...
@@ -2,6 +2,9 @@ import pygame
import
sys
import
sys
from
pygame.locals
import
*
from
pygame.locals
import
*
from
random
import
*
from
random
import
*
import
math
BALL_NUM
=
5
class
Ball
(
pygame
.
sprite
.
Sprite
):
class
Ball
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
,
position
,
speed
,
bg_size
):
def
__init__
(
self
,
image
,
position
,
speed
,
bg_size
):
...
@@ -12,8 +15,7 @@ class Ball(pygame.sprite.Sprite):
...
@@ -12,8 +15,7 @@ class Ball(pygame.sprite.Sprite):
self
.
speed
=
speed
self
.
speed
=
speed
self
.
width
,
self
.
height
=
bg_size
[
0
],
bg_size
[
1
]
self
.
width
,
self
.
height
=
bg_size
[
0
],
bg_size
[
1
]
def
move
(
self
):
def
move
(
self
):
selff
.
rect
=
self
.
rect
.
move
(
self
.
speed
)
self
.
rect
=
self
.
rect
.
move
(
self
.
speed
)
if
self
.
rect
.
right
<
0
:
if
self
.
rect
.
right
<
0
:
self
.
rect
.
left
=
self
.
width
self
.
rect
.
left
=
self
.
width
elif
self
.
rect
.
left
>
self
.
width
:
elif
self
.
rect
.
left
>
self
.
width
:
...
@@ -22,5 +24,57 @@ class Ball(pygame.sprite.Sprite):
...
@@ -22,5 +24,57 @@ class Ball(pygame.sprite.Sprite):
self
.
rect
.
top
=
self
.
height
self
.
rect
.
top
=
self
.
height
elif
self
.
rect
.
top
>
self
.
height
:
elif
self
.
rect
.
top
>
self
.
height
:
self
.
rect
.
bottom
=
0
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
():
def
main
():
pygame
.
pygame
.
init
()
ball_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\ccc\gray_ball.png'
bg_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\ccc\background.png'
running
=
True
bg_size
=
width
,
height
=
1024
,
681
screen
=
pygame
.
display
.
set_mode
(
bg_size
)
pygame
.
display
.
set_caption
(
'Play the ball Demo'
)
background
=
pygame
.
image
.
load
(
bg_image
)
.
convert_alpha
()
balls
=
[]
group
=
pygame
.
sprite
.
Group
()
for
i
in
range
(
5
):
position
=
randint
(
0
,
width
-
100
),
randint
(
0
,
height
-
100
)
speed
=
[
randint
(
-
10
,
10
),
randint
(
-
10
,
10
)]
ball
=
Ball
(
ball_image
,
position
,
speed
,
bg_size
)
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
():
if
event
.
type
==
QUIT
:
sys
.
exit
()
screen
.
blit
(
background
,(
0
,
0
))
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
)
# balls.insert(i,item)
pygame
.
display
.
flip
()
clock
.
tick
(
30
)
main
()
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