import pygame import math import random pygame.init() W, H = 640, 480 screen = pygame.display.set_mode((W, H)) # keys用来记录按键情况:WASD依次对应 keys = [False, False, False, False] # playerpos表示玩家位置 playerpos = [100, 100] acc = [0, 0] # 记录射出的箭数和命中的箭数 arrows = [] # 跟踪箭头变量,存放箭头旋转角度值(弧度),箭头起始位置坐标 # 坏人相关变量或列表 badtimer = 100 # 定义一个定时器,使得游戏里过一段时间后就产生一只新的獾(坏人) badtimer1 = 0 badguys = [[640, 100]] # 坏人产生位置列表,第一个坏人位置为(640,100) healthvalue = 194 # 城堡健康值等于194 # 加载图片 player = pygame.image.load("resources/images/dude.png") # 兔子 grass = pygame.image.load("resources/images/grass.png") # 草 castle = pygame.image.load("resources/images/castle.png") # 城堡 arrow = pygame.image.load("resources/images/bullet.png") # 箭 badguyimg1 = pygame.image.load("resources/images/badguy.png") # 加载坏人 badguyimg = badguyimg1 # 设置坏人图像副本,可以更流畅地为坏人设置动画 healthbar=pygame.image.load("resources/images/healthbar.png") #生命值条 health=pygame.image.load("resources/images/health.png") #剩余生命值图片 while True: screen.fill(0) # 屏幕填充黑色 for x in range(W // grass.get_width() + 1): for y in range(H // grass.get_height() + 1): screen.blit(grass, (x * 100, y * 100)) screen.blit(castle, (0, 30)) screen.blit(castle, (0, 135)) screen.blit(castle, (0, 240)) screen.blit(castle, (0, 345)) # 首先获取鼠标和玩家的位置,然后,获取atan2函数得出的角度和弧度。 # 当兔子被旋转时,它的位置将会改变 # 所以你需要计算兔子新的位置,然后将其在屏幕上显示出来 position = pygame.mouse.get_pos() # 获取鼠标坐标 angle = math.atan2(position[1] - (playerpos[1] + 32), position[0] - (playerpos[0] + 26)) playerrot = pygame.transform.rotate(player, 360 - angle * 57.29) # 玩家旋转图片 playerpos1 = (playerpos[0] - playerrot .get_rect().width // 2, playerpos[1] - playerrot.get_rect().height // 2) screen.blit(playerrot, playerpos1) # 在屏幕上画出箭头来 # vely和velx的值是根据三角定理算出来的 # 10是箭头的速度,if 是检查箭头是否超出了屏幕范围,如果超出解除这个箭头 # 第二个for 循环把箭头根据相应的旋转画出来 for bullet in arrows: # 对每一支箭在箭头列表里 index = 0 velx = math.cos(bullet[0]) * 10 vely = math.sin(bullet[0]) * 10 bullet[1] += velx bullet[2] += vely if bullet[1] < -64 or bullet[1] > 640 or bullet[2] < -64 or bullet[2] > 480: arrows.pop(index) index += 1 for projectile in arrows: arrow1 = pygame.transform.rotate(arrow, 360 - projectile[0] * 57.29) screen.blit(arrow1, (projectile[1], projectile[2])) # 更新并显示坏人 # 检查badtimer是否为0,如果为0,创建一个獾然后重新设置badtimer # 第一个循环更新獾的x坐标,检查獾是否超出屏幕范围,如果超出,将獾删掉 # 第二个循环是来画出所有的獾 if badtimer == 0: badguys.append([640, random.randint(50, 430)]) badtimer = 100 - (badtimer1 * 2) if badtimer1 >= 20: badtimer1 =20 else: badtimer1 += 5 index_badguy = 0 for badguy in badguys: # 对坏人列表里的每个坏人显示 if badguy[0] < -64: badguys.pop(index_badguy) badguy[0] -= 1 # 獾可以炸掉城堡 # 如果獾的x坐标离左边小于64,就删除獾,并随机减少5-20之间的健康值 badrect = pygame.Rect(badguyimg.get_rect()) badrect.top = badguy[1] badrect.left = badguy[0] if badrect.left < 64: healthvalue -= random.randint(5, 20) badguys.pop(index_badguy) # 循环所有的坏蛋和箭头来检查是否有碰撞 # 如果碰撞上,删除獾删除箭头,并且命中数变量里+1 # 使用了pygame的内建功能来检查两个矩形是否交叉 #''' index_arrow = 0 for bullet in arrows: bulletrect = pygame.Rect(arrow.get_rect()) bulletrect.left = bullet[1] bulletrect.top = bullet[2] if badrect.colliderect(bulletrect): acc[0] += 1 badguys.pop(index_badguy) arrows.pop(index_arrow) index_arrow += 1 #''' index_badguy += 1 for badguy in badguys: screen.blit(badguyimg, badguy) #添加一个计时 font=pygame.font.Font('Arial.ttf',24) survivedtext=font.render(str((120-pygame.time.get_ticks()//1000)//60)+":"+ str((120-pygame.time.get_ticks()//1000)%60).zfill(2),True,(255,0,0)) textRect=survivedtext.get_rect() textRect.topright=[635,5] screen.blit(survivedtext,textRect) #画出城堡健康值 #先画出一个全红色的生命条 screen.blit(healthbar,(5,5)) for health1 in range(healthvalue): screen.blit(health,(health1+8,8)) badtimer -= 1 # 每更新一帧,计时-1 pygame.display.flip() # 更新屏幕 # 事件检测 for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: position = pygame.mouse.get_pos() acc[1] += 1 arrows.append([math.atan2(position[1] - (playerpos1[1] + 32), position[0] - (playerpos1[0] + 26)), playerpos1[0] + 32, playerpos1[1] + 32]) if event.type == pygame.QUIT: pygame.quit() exit(0) # 根据按下的键来更新按键记录数组 if event.type == pygame.KEYDOWN: if event.key == pygame.K_w: keys[0] = True elif event.key == pygame.K_a: keys[1] = True elif event.key == pygame.K_s: keys[2] = True elif event.key == pygame.K_d: keys[3] = True if event.type == pygame.KEYUP: if event.key == pygame.K_w: keys[0] = False elif event.key == pygame.K_a: keys[1] = False elif event.key == pygame.K_s: keys[2] = False elif event.key == pygame.K_d: keys[3] = False # 移动玩家 if keys[0]: playerpos[1] -= 1 elif keys[2]: playerpos[1] += 1 if keys[1]: playerpos[0] -= 1 elif keys[3]: playerpos[0] += 1