|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lzb1001 于 2022-8-19 01:03 编辑
import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
def __init__(self, grayball_image, greenball_image, position, speed, bg_size, target):
pygame.sprite.Sprite.__init__(self)
self.grayball_image = pygame.image.load(grayball_image).convert_alpha()
self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
self.rect = self.grayball_image.get_rect()
self.rect.left, self.rect.top = position
self.side = [choice([-1, 1]), choice([-1, 1])]
self.speed = speed
self.collide = False
self.target = target
self.control = False
self.width, self.height = bg_size[0], bg_size[1]
self.radius = self.rect.width / 2
def move(self):
self.rect = self.rect.move((self.side[0] * self.speed[0], \
self.side[1] * self.speed[1]))
if self.rect.right < 0:
self.rect.left = self.width
elif self.rect.left > self.width:
self.rect.right = 0
elif self.rect.bottom < 0:
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
class Glass(pygame.sprite.Sprite):
def __init__(self, glass_image, mouse_image, bg_size):
pygame.sprite.Sprite.__init__(self)
self.glass_image = pygame.image.load(glass_image).convert_alpha()
self.glass_rect = self.glass_image.get_rect()
self.glass_rect.left, self.glass_rect.top = \
(bg_size[0] - self.glass_rect.width) // 2, \
(bg_size[1] - self.glass_rect.height)
self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
self.mouse_rect = self.mouse_image.get_rect()
self.mouse_rect.left, self.mouse_rect.top = \
self.glass_rect.left, self.glass_rect.top
pygame.mouse.set_pos([self.glass_rect.left, self.glass_rect.top])
pygame.mouse.set_visible(False)
def main():
pygame.init()
grayball_image = 'gray_ball.png'
greenball_image = 'green_ball.png'
glass_image = 'glass.png'
mouse_image = 'hand.png'
bg_image = 'background.png'
running = True
pygame.mixer.music.load('bg_music.ogg')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play()
loser_sound = pygame.mixer.Sound('loser.wav')
loser_sound.set_volume(0.2)
laugh_sound = pygame.mixer.Sound('laugh.wav')
laugh_sound.set_volume(0.2)
winner_sound = pygame.mixer.Sound('winner.wav')
winner_sound.set_volume(0.2)
hole_sound = pygame.mixer.Sound('hole.wav')
hole_sound.set_volume(0.2)
GAMEOVER = USEREVENT
pygame.mixer.music.set_endevent(GAMEOVER)
bg_size = width, height = 1024, 681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('Play the ball - FishC 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(1, 10), randint(1, 10)]
ball = Ball(grayball_image, greenball_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)
group.add(ball)
glass = Glass(glass_image, mouse_image, bg_size)
motion = 0
MYTIMER = USEREVENT + 1
pygame.time.set_timer(MYTIMER, 1000)
pygame.key.set_repeat(100, 100)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == GAMEOVER:
loser_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running = False
elif event.type == MYTIMER:
if motion:
for each in group:
if each.check(motion):
each.speed = [0, 0]
each.control = True
motion = 0
elif event.type == MOUSEMOTION:
motion += 1
elif event.type == KEYDOWN:
if event.key == K_w:
for each in group:
if each.control:
each.speed[1] -= 1
if event.key == K_s:
for each in group:
if each.control:
each.speed[1] += 1
if event.key == K_a:
for each in group:
if each.control:
each.speed[0] -= 1
if event.key == K_d:
for each in group:
if each.control:
each.speed[0] += 1
screen.blit(background, (0, 0))
screen.blit(glass.glass_image, glass.glass_rect)
glass.mouse_rect.left, glass.mouse_rect.top = pygame.mouse.get_pos()
if glass.mouse_rect.left < glass.glass_rect.left:
glass.mouse_rect.left = glass.glass_rect.left
if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
if glass.mouse_rect.top < glass.glass_rect.top:
glass.mouse_rect.top = glass.glass_rect.top
if glass.mouse_rect.top > glass.glass_rect.bottom - glass.mouse_rect.height:
glass.mouse_rect.top = glass.glass_rect.bottom - glass.mouse_rect.height
screen.blit(glass.mouse_image, glass.mouse_rect)
for each in balls:
each.move()
if each.collide:
each.speed = [randint(1, 10), randint(1, 10)]
each.collide = False
if each.control:
screen.blit(each.greenball_image, each.rect)
else:
screen.blit(each.grayball_image, each.rect)
for each in group:
group.remove(each)
if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):
each.side[0] = -each.side[0]
each.side[1] = -each.side[1]
each.collide = True
each.control = False
group.add(each)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------
【我的问题】无法理解当一个灰色小球变成绿色小球静止不动后,我按下键盘上的a,绿色小球应该从右向左移动,可为何实际它却是偏偏向右移动?
我的疑问或不解其实可以分解为以下四个问题:
1、while语句中代码的执行顺序不太理解,感觉越看越乱
2、下面我自己理解的执行顺序和代码逻辑是否正确?
当灰色小球的目标达到鼠标移动次数的范围内时,小球静止不动:
elif event.type == MYTIMER:
if motion:
for each in group:
if each.check(motion):
each.speed = [0, 0]
each.control = True
motion = 0
然后跳过下面的代码:
elif event.type == MOUSEMOTION:
motion += 1
elif event.type == KEYDOWN:
if event.key == K_w:
for each in group:
if each.control:
each.speed[1] -= 1
if event.key == K_s:
for each in group:
if each.control:
each.speed[1] += 1
if event.key == K_a:
for each in group:
if each.control:
each.speed[0] -= 1
if event.key == K_d:
for each in group:
if each.control:
each.speed[0] += 1
继续执行后面的代码,即:
screen.blit(background, (0, 0))
screen.blit(glass.glass_image, glass.glass_rect)
glass.mouse_rect.left, glass.mouse_rect.top = pygame.mouse.get_pos()
if glass.mouse_rect.left < glass.glass_rect.left:
glass.mouse_rect.left = glass.glass_rect.left
if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
if glass.mouse_rect.top < glass.glass_rect.top:
glass.mouse_rect.top = glass.glass_rect.top
if glass.mouse_rect.top > glass.glass_rect.bottom - glass.mouse_rect.height:
glass.mouse_rect.top = glass.glass_rect.bottom - glass.mouse_rect.height
screen.blit(glass.mouse_image, glass.mouse_rect)
for each in balls:
each.move()
if each.collide:
each.speed = [randint(1, 10), randint(1, 10)]
each.collide = False
if each.control:
screen.blit(each.greenball_image, each.rect) # 绘制绿色的小球
else:
screen.blit(each.grayball_image, each.rect)
for each in group:
group.remove(each)
if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):
each.side[0] = -each.side[0]
each.side[1] = -each.side[1]
each.collide = True
each.control = False
group.add(each)
pygame.display.flip()
clock.tick(30)
然后再返回while语句开头重新执行
……
直到用户按下键盘上的a,对应代码如下:
if event.key == K_a:
for each in group:
if each.control:
each.speed[0] -= 1
小球会移动是因为如下代码:
for each in balls:
each.move()
……
而其中的move()方法调用自定义的move()函数,其代码如下:
def move(self):
self.rect = self.rect.move((self.side[0] * self.speed[0], \
self.side[1] * self.speed[1]))
……
3、move()函数中的side值从哪里传入或得到?是当我按下键盘上的a键时传入或得到的吗?
4、最大的疑问在于为何小球摩擦中为何绿色小球不按照键盘按键的方向而是反方向移动?
如果(绿色的小球受玩家控制时)速度减到变为负数,方向为负数(小球从灰色变成绿色说明它已静止不动了,又何来方向之说?绿色的小球的方向不是用户通过键盘按键指明的吗?难道这里的方向就是指用户通过键盘按键指给小球的方向吗?),负负得正,就会得到一个反方向移动的小球。
******************************
感谢大神不吝赐教,为新手解疑释惑。
赠人玫瑰,手有余香,好人一生平安! |
|