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
144a3d9a
authored
Sep 26, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
7ce79097
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
6 deletions
q/main.py
q/main.py
View file @
144a3d9a
...
...
@@ -4,15 +4,18 @@ from pygame.locals import *
from
random
import
*
import
math
class
Ball
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
image
,
position
,
speed
,
bg_size
):
def
__init__
(
self
,
grayball_image
,
greenball_image
,
position
,
speed
,
bg_size
,
target
):
#初始化动画精灵
pygame
.
sprite
.
Sprite
.
__init__
(
self
)
self
.
image
=
pygame
.
image
.
load
(
image
)
.
convert_alpha
()
self
.
rect
=
self
.
image
.
get_rect
()
self
.
target
=
target
self
.
greenball_image
=
pygame
.
image
.
load
(
greenball_image
)
.
convert_alpha
()
self
.
grayball_image
=
pygame
.
image
.
load
(
grayball_image
)
.
convert_alpha
()
self
.
rect
=
self
.
grayball_image
.
get_rect
()
#将小球放到指定位置
self
.
rect
.
left
,
self
.
rect
.
top
=
position
self
.
speed
=
speed
self
.
width
,
self
.
height
=
bg_size
[
0
],
bg_size
[
1
]
self
.
control
=
False
#小球移动
def
move
(
self
):
self
.
rect
=
self
.
rect
.
move
(
self
.
speed
)
...
...
@@ -46,6 +49,7 @@ class Glass(pygame.sprite.Sprite):
def
main
():
pygame
.
init
()
#导入素材
pygame
.
mixer
.
music
.
load
(
'bg_music.ogg'
)
pygame
.
mixer
.
music
.
play
()
loser_sound
=
pygame
.
mixer
.
Sound
(
'loser.wav'
)
...
...
@@ -54,10 +58,11 @@ def main():
winner_sound
=
pygame
.
mixer
.
Sound
(
'winner.wav'
)
GAMEOVER
=
USEREVENT
pygame
.
mixer
.
music
.
set_endevent
(
GAMEOVER
)
ball_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\q\gray_ball.png'
bg_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\q\background.png'
glass_image
=
r'C:\Users\Administrator\Documents\level3-lesson24-diy3\glass.png'
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
)
...
...
@@ -71,7 +76,7 @@ def main():
for
i
in
range
(
ballnum
):
position
=
randint
(
0
,
width
-
100
),
randint
(
0
,
height
-
100
)
speed
=
[
randint
(
-
10
,
10
),
randint
(
-
10
,
10
)]
ball
=
Ball
(
ball_image
,
position
,
speed
,
bg_size
)
ball
=
Ball
(
greenball_image
,
grayball_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
)
...
...
@@ -103,7 +108,7 @@ def main():
screen
.
blit
(
glass
.
mouse_image
,
glass
.
mouse_rect
)
for
each
in
balls
:
each
.
move
()
screen
.
blit
(
each
.
image
,
each
.
rect
)
screen
.
blit
(
each
.
greenball_
image
,
each
.
rect
)
for
i
in
range
(
ballnum
):
item
=
balls
.
pop
(
i
)
if
collide_check
(
item
,
balls
):
...
...
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