小球摩擦中为何绿色小球不按照键盘按键的方向而是反方向移动?
本帖最后由 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])]
self.speed = speed
self.collide = False
self.target = target
self.control = False
self.width, self.height = bg_size, bg_size
self.radius = self.rect.width / 2
def move(self):
self.rect = self.rect.move((self.side * self.speed, \
self.side * self.speed))
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 - self.glass_rect.width) // 2, \
(bg_size - 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()
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 =
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 =
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
if event.key == K_s:
for each in group:
if each.control:
each.speed += 1
if event.key == K_a:
for each in group:
if each.control:
each.speed -= 1
if event.key == K_d:
for each in group:
if each.control:
each.speed += 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 =
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 = -each.side
each.side = -each.side
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 =
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
if event.key == K_s:
for each in group:
if each.control:
each.speed += 1
if event.key == K_a:
for each in group:
if each.control:
each.speed -= 1
if event.key == K_d:
for each in group:
if each.control:
each.speed += 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 =
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 = -each.side
each.side = -each.side
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 -= 1
小球会移动是因为如下代码:
for each in balls:
each.move()
……
而其中的move()方法调用自定义的move()函数,其代码如下:
def move(self):
self.rect = self.rect.move((self.side * self.speed, \
self.side * self.speed))
……
3、move()函数中的side值从哪里传入或得到?是当我按下键盘上的a键时传入或得到的吗?
4、最大的疑问在于为何小球摩擦中为何绿色小球不按照键盘按键的方向而是反方向移动?
如果(绿色的小球受玩家控制时)速度减到变为负数,方向为负数(小球从灰色变成绿色说明它已静止不动了,又何来方向之说?绿色的小球的方向不是用户通过键盘按键指明的吗?难道这里的方向就是指用户通过键盘按键指给小球的方向吗?),负负得正,就会得到一个反方向移动的小球。
******************************
感谢大神不吝赐教,为新手解疑释惑。
赠人玫瑰,手有余香,好人一生平安! 本帖最后由 lzb1001 于 2022-8-19 22:58 编辑
在下面的代码中,导致小球的速度减为负数的代码有两处(已标记为红色):
elif event.type == KEYDOWN:
if event.key == K_w:
for each in group:
if each.control:
each.speed -= 1
if event.key == K_s:
for each in group:
if each.control:
each.speed += 1
if event.key == K_a:
for each in group:
if each.control:
each.speed -= 1
if event.key == K_d:
for each in group:
if each.control:
each.speed += 1
这两处都是放在小球处于静止状态、变为绿色小球受玩家键盘按键控制的代码里面,也就是说只有运行了这两处代码,小球的速度才有可能减为负数,
但经过测试,我发现有些时候游戏刚开始没多久(玩家还未控制小球,也就是没有运行这两处代码),而当小球变成绿色的时候,它就已不按控制反方向移动了
不知道这是怎么回事呢? 你的Ball.move函数中判断小球是否受控的if self.control哪去了?
1. 循环里就是遍历事件序列,还有碰撞检测,小球移动和受控检测
2. 应该是对的
3. move方法中的side是实例变量,即self.side,它在小球初始化的时候被声明,可由小球类里的其他方法访问,也可用实例名.side访问。
4. 我没怎么看懂你的话,我想的是小球受控后描述运动的方式是速率和方向结合,即有正负的速度,这时候就不能用self.side这个变量描述运动的方向了。但是你的代码里,无论小球是否受控,都是用self.speed和self.side两个变量来计算的
这样在小球受控后,由于方向可能是负数,而向左移动表示为each.speed -= 1,使速度为负。负负得正,小球就不按照键盘指示,而是往右运动。第二个帖子也是这个原因。 鱼cpython学习者 发表于 2022-8-20 11:28
你的Ball.move函数中判断小球是否受控的if self.control哪去了?
1. 循环里就是遍历事件序列,还有碰撞检 ...
谢谢大神回复,加你好友了,不知可否通过下? lzb1001 发表于 2022-8-20 11:31
谢谢大神回复,加你好友了,不知可否通过下?
好的,通过了 鱼cpython学习者 发表于 2022-8-20 11:41
好的,通过了
看到了大神,已给你发信息,请查收 鱼cpython学习者 发表于 2022-8-20 11:41
好的,通过了
elif event.type == KEYDOWN:
if event.key == K_w:
for each in group:
if each.control:
each.speed -= 1
if event.key == K_s:
for each in group:
if each.control:
each.speed += 1
if event.key == K_a:
for each in group:
if each.control:
each.speed -= 1
if event.key == K_d:
for each in group:
if each.control:
each.speed += 1
这段代码在小球受控的语句段落里,只有运行到其中each.speed -= 1或each.speed -= 1,小球的速度才可能减为负数
问题是有时游戏一开始,灰色小球还没变成绿色小球,不能手动控制,所以不能用按键控制小球,也就是说没有运行上面的代码
但当灰色的小球变成绿色的小球时,我用按键控制它,它就已经不听控制(反方向移动)了,
所以我的疑问是:并没有手动控制它,说明没有运行到上面的代码,那小球的速度为何会变成负数呢?
不知道我这样表述大神看得懂理解吗? lzb1001 发表于 2022-8-20 12:02
elif event.type == KEYDOWN:
if event.key == K_w:
你看Ball.move方法,你的代码中并没有判断小球是否受控的语句,而是一直都
self.rect = self.rect.move((self.side * self.speed, self.side * self.speed))
那么这样影响小球运动的就有self.side和self.speed两个变量。当小球受控,速度归零,但方向没有变成
那么这样,如果小球在受控前方向为[-1, -1],你按下w键,想让小球向上运动。speed -= 1变成-1,而你move方法一直都使用self.side这个变量,那么不就变成-1 * -1 = 1,小球不就往反方向运动了吗
页:
[1]