|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
对飞机的rect位置参数进行加减时,self.speed如果设置为0.3或者0.5就不能加只能减,self.speed设置为1时就是正常的,我主要是想解答这个疑惑(所以现在我都是用的1在使用)
当self.speed为0.3时:如果把right和bottom中的self.speed改成1,可以向对的方向移动;如果把right和bottom中的+改成-,可以向左和上移动;如果把left和top的-改成+,不能移动;如果在self.speed前加-号,结果和上面相同。
复制的代码,缩进可能有问题,但是代码运行是没有问题的。
main函数中:
import pygame
import sys
import random
import target
import me
pygame.init()
bg_size = (width, height) = (800, 600)
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('Enjoy It')
# 定义颜色
Black = (0,0,0)
Green = (0,255,0)
Red = (255,0,0)
White = (255,255,255)
# 加载背景
b1 = pygame.image.load('back_images/b1.png')
b2 = pygame.image.load('back_images/b2.png')
b3 = pygame.image.load('back_images/b3.png')
b4 = pygame.image.load('back_images/b4.png')
b5 = pygame.image.load('back_images/b5.png')
b6 = pygame.image.load('back_images/b6.png')
b7 = pygame.image.load('back_images/b7.png')
b8 = pygame.image.load('back_images/b8.png')
b9 = pygame.image.load('back_images/b9.png')
b10 = pygame.image.load('back_images/b10.png')
b11 = pygame.image.load('back_images/b11.png')
# 载入背景音乐
m1 = pygame.mixer.music.load('music/m1.mp3')
def main():
background = [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11]
# 播放背景音乐
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(1)
# 控制背景图片
background_control = False
background_appear = random.choice(background)
# 实例化target
animal = target.Target(bg_size)
# 实例化帧数
clock = pygame.time.Clock()
# 实例化myself
myself = me.Me(bg_size)
# 自定义事件1,控制target left速度
time_speed_left = [500,1000,1500,2000,2500,3000,3500,4000]
SPEED_LEFT = pygame.USEREVENT
pygame.time.set_timer(SPEED_LEFT, random.choice(time_speed_left))
# 自定义事件2,控制target top速度
time_speed_top = [500,1000,1500,2000,2500,3000,3500,4000]
SPEED_TOP = pygame.USEREVENT + 1
pygame.time.set_timer(SPEED_TOP, random.choice(time_speed_top))
# 自定义事件3,控制target left方向
time_direct_left = [500,1000,2000,3000,4000]
DIRECT_LEFT = pygame.USEREVENT + 2
pygame.time.set_timer(DIRECT_LEFT,random.choice(time_direct_left))
# 自定义事件4,控制target top方向
time_direct_top = [500,1000,2000,3000,4000]
DIRECT_TOP = pygame.USEREVENT + 3
pygame.time.set_timer(DIRECT_TOP,random.choice(time_direct_top))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == SPEED_LEFT:
animal.reset_speed_left()
pygame.time.set_timer(SPEED_LEFT, random.choice(time_speed_left))
elif event.type == SPEED_TOP:
animal.reset_speed_top()
pygame.time.set_timer(SPEED_TOP, random.choice(time_speed_top))
elif event.type == DIRECT_LEFT:
animal.reset_direct_left()
pygame.time.set_timer(DIRECT_LEFT, random.choice(time_direct_left))
elif event.type == DIRECT_TOP:
animal.reset_direct_top()
pygame.time.set_timer(DIRECT_TOP, random.choice(time_direct_top))
# 控制myself移动
if animal.active:
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w]:
myself.move_top()
if key_pressed[pygame.K_a]:
myself.move_left()
if key_pressed[pygame.K_s]:
myself.move_bottom()
if key_pressed[pygame.K_d]:
myself.move_right()
# 绘制背景
if background_control:
background_appear = random.randint(background)
background_control = False
screen.blit(background_appear,(0,0))
# 绘制target
animal.move()
screen.blit(animal.image, animal.rect)
# 绘制myself
screen.blit(myself.image, myself.rect)
clock.tick(200)
pygame.display.flip()
if __name__ == '__main__':
main()
me模块中:
import pygame
import random
class Me(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.t1_top = pygame.image.load('me_images/me.png').convert_alpha()
self.t1_left = pygame.transform.rotate(self.t1_top, 90)
self.t1_bottom = pygame.transform.rotate(self.t1_top, 180)
self.t1_right = pygame.transform.rotate(self.t1_top, 270)
self.image = self.t1_top
self.width = bg_size[0]
self.height = bg_size[1]
self.rect = self.image.get_rect()
self.rect.left = random.randint(0, self.width - self.rect.width)
self.rect.top = random.randint(0, self.height - self.rect.height)
self.speed = 1
self.accelerate = 0.1
def move_left(self):
self.image = self.t1_left
if self.rect.left > 0:
self.rect.left -= self.speed
def move_right(self):
self.image = self.t1_right
if self.rect.left < self.width - self.rect.width:
self.rect.left += self.speed
def move_top(self):
self.image = self.t1_top
if self.rect.top > 0:
self.rect.top -= self.speed
def move_bottom(self):
self.image = self.t1_bottom
if self.rect.top < self.height - self.rect.height:
self.rect.top += self.speed |
|