初学者勿喷
躲避小游戏 添加了一个背景音乐
import sys
import pygame
import time
pygame.init()
#会有那么一天
pygame.mixer.music.load('背景音乐.flac')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(loops=-1)
#颜色
red_color = (220,20,60)
orange_color = (255,165,0)
yellow_color = (255,255,0)
green_color = (0,128,0)
blue_color = (0,0,255)
indigo_color = (75,0,130)
purple_color = (128,0,128)
white_color = (255,255,255)
black_color = (0,0,0)
screen_image = pygame.display.set_mode((1500,300))
screen_rect = screen_image.get_rect()
pygame.display.set_caption("躲避")
#目标
people_rect = pygame.Rect(100,100,20,20)
people2_rect = pygame.Rect(100,150,20,20)
#目标障碍wall
ob_rect = pygame.Rect(200,0,50,200)
ob2_rect = pygame.Rect(550,100,50,200)
ob3_rect = pygame.Rect(900,0,50,200)
ob4_rect = pygame.Rect(1250,100,50,200)
#目标移动障碍
obm_rect = pygame.Rect(250,0,110,110)
obm2_rect = pygame.Rect(400,0,110,110)
obm3_rect = pygame.Rect(400,150,110,110)
obm4_rect = pygame.Rect(250,150,110,110)
#目标移动障碍第二关
obm5_rect = pygame.Rect(630,0,30,270)
obm6_rect = pygame.Rect(660,30,30,270)
obm7_rect = pygame.Rect(840,0,30,270)
obm8_rect = pygame.Rect(870,0,30,270)
#目标移动障碍第三关
obm9_rect = pygame.Rect(950,0,40,40)
obm10_rect = pygame.Rect(1210,0,40,40)
obm11_rect = pygame.Rect(1210,260,40,40)
obm12_rect = pygame.Rect(950,260,40,40)
obm13_rect = pygame.Rect(1080,130,40,40)
obm14_rect = pygame.Rect(1080,130,40,40)
obm15_rect = pygame.Rect(1080,130,40,40)
obm16_rect = pygame.Rect(1080,130,40,40)
#障碍整体速度变量
speed = 1
speed_run = 3
#目标移动障碍变量定义
direct_x = 1
direct_y = 1
direct_x6 = 1
direct_x7 = -1
direct_z = 1
direct_f = -1
direct_z2 = 1
direct_f2 = -1
#持续控制移动变量
moving_LEFT = False
moving_RIGHT = False
moving_UP = False
moving_DOWN = False
moving_LEFT2 = False
moving_RIGHT2 = False
moving_UP2 = False
moving_DOWN2 = False
#winner变量
winner = 0
#main
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
moving_LEFT2 = True
if event.key == pygame.K_RIGHT:
moving_RIGHT2 = True
if event.key == pygame.K_UP:
moving_UP2 = True
if event.key == pygame.K_DOWN:
moving_DOWN2 = True
if event.key == pygame.K_a:
moving_LEFT = True
if event.key == pygame.K_d:
moving_RIGHT = True
if event.key == pygame.K_w:
moving_UP = True
if event.key == pygame.K_s:
moving_DOWN = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
moving_LEFT2 = False
if event.key == pygame.K_RIGHT:
moving_RIGHT2 = False
if event.key == pygame.K_UP:
moving_UP2 = False
if event.key == pygame.K_DOWN:
moving_DOWN2 = False
if event.key == pygame.K_a:
moving_LEFT = False
if event.key == pygame.K_d:
moving_RIGHT = False
if event.key == pygame.K_w:
moving_UP = False
if event.key == pygame.K_s:
moving_DOWN = False
#elif moving_DOWN2 == True or moving_LEFT == True or moving_RIGHT == True or moving_UP == True or moving_DOWN == True:
#wasd玩家上障碍
if 180 == people_rect.x and 0 < people_rect.y < 200:
moving_RIGHT = False
if 250 == people_rect.x and 0 < people_rect.y < 200:
moving_LEFT = False
if 880 == people_rect.x and 0 < people_rect.y < 200:
moving_RIGHT = False
if 950 == people_rect.x and 0 < people_rect.y < 200:
moving_LEFT = False
if 200 == people_rect.y and 180 < people_rect.x < 250:
moving_UP = False
if 200 == people_rect.y and 880 < people_rect.x < 950:
moving_UP = False
#wasd玩家下障碍
if 530 == people_rect.x and 80 < people_rect.y < 300:
moving_RIGHT = False
if 600 == people_rect.x and 80 < people_rect.y < 300:
moving_LEFT = False
if 1230 == people_rect.x and 80 < people_rect.y < 300:
moving_RIGHT = False
if 1300 == people_rect.x and 80 < people_rect.y < 300:
moving_LEFT = False
if 80 == people_rect.y and 530 < people_rect.x < 600:
moving_DOWN = False
if 80 == people_rect.y and 1230 < people_rect.x < 1300:
moving_DOWN = False
# 方向键玩家上障碍
if 180 == people2_rect.x and 0 < people2_rect.y < 200:
moving_RIGHT2 = False
if 250 == people2_rect.x and 0 < people2_rect.y < 200:
moving_LEFT2 = False
if 880 == people2_rect.x and 0 < people2_rect.y < 200:
moving_RIGHT2 = False
if 950 == people2_rect.x and 0 < people2_rect.y < 200:
moving_LEFT2 = False
if 200 == people2_rect.y and 180 < people2_rect.x < 250:
moving_UP2 = False
if 200 == people2_rect.y and 880 < people2_rect.x < 950:
moving_UP2 = False
# 方向键玩家下障碍
if 530 == people2_rect.x and 80 < people2_rect.y < 300:
moving_RIGHT2 = False
if 600 == people2_rect.x and 80 < people2_rect.y < 300:
moving_LEFT2 = False
if 1230 == people2_rect.x and 80 < people2_rect.y < 300:
moving_RIGHT2 = False
if 1300 == people2_rect.x and 80 < people2_rect.y < 300:
moving_LEFT2 = False
if 80 == people2_rect.y and 530 < people2_rect.x < 600:
moving_DOWN2 = False
if 80 == people2_rect.y and 1230 < people2_rect.x < 1300:
moving_DOWN2 = False
if moving_LEFT2 and people2_rect.left > 0 :
people2_rect.x -= 1
if moving_RIGHT2 and people2_rect.right < screen_rect.right:
people2_rect.x += 1
if moving_UP2 and people2_rect.y > 0:
people2_rect.y -= 1
if moving_DOWN2 and people2_rect.y < screen_rect.bottom - 20:
people2_rect.y += 1
if moving_LEFT and people_rect.left > 0:
people_rect.x -= 1
if moving_RIGHT and people_rect.right < screen_rect.right:
people_rect.x += 1
if moving_UP and people_rect.y > 0:
people_rect.y -= 1
if moving_DOWN and people_rect.y < screen_rect.bottom - 20:
people_rect.y += 1
# 判断胜利
if people_rect.x == 1480 and people2_rect.x != 1480:
winner = 1
screen_image.fill(red_color)
pygame.display.flip()
time.sleep(2)
break
if people2_rect.x == 1480 and people_rect.x != 1480:
winner = 2
screen_image.fill(blue_color)
pygame.display.flip()
time.sleep(2)
break
speed += 1
if speed == speed_run:
#障碍目标移动
#1
if 250 <= obm_rect.x < 290 and obm_rect.y == 0:
obm_rect.x += 1
if 0 <= obm_rect.y < 40 and obm_rect.x == 290:
obm_rect.y += 1
if 250 < obm_rect.x <= 290 and obm_rect.y == 40:
obm_rect.x -= 1
if 0 < obm_rect.y <= 40 and obm_rect.x == 250:
obm_rect.y -= 1
# 2
if 400 <= obm2_rect.x < 440 and obm2_rect.y == 0:
obm2_rect.x += 1
if 0 <= obm2_rect.y < 40 and obm2_rect.x == 440:
obm2_rect.y += 1
if 400 < obm2_rect.x <= 440 and obm2_rect.y == 40:
obm2_rect.x -= 1
if 0 < obm2_rect.y <= 40 and obm2_rect.x == 400:
obm2_rect.y -= 1
# 3
if 400 <= obm3_rect.x < 440 and obm3_rect.y == 150:
obm3_rect.x += 1
if 150 <= obm3_rect.y < 190 and obm3_rect.x == 440:
obm3_rect.y += 1
if 400 < obm3_rect.x <= 440 and obm3_rect.y == 190:
obm3_rect.x -= 1
if 150 < obm3_rect.y <= 190 and obm3_rect.x == 400:
obm3_rect.y -= 1
# 4
if 250 <= obm4_rect.x < 290 and obm4_rect.y == 150:
obm4_rect.x += 1
if 150 <= obm4_rect.y < 190 and obm4_rect.x == 290:
obm4_rect.y += 1
if 250 < obm4_rect.x <= 290 and obm4_rect.y == 190:
obm4_rect.x -= 1
if 150 < obm4_rect.y <= 190 and obm4_rect.x == 250:
obm4_rect.y -= 1
#6
obm6_rect.x = obm6_rect.x + 1 * direct_x6
if obm6_rect.x == obm5_rect.right:
direct_x6 = 1
if obm6_rect.right == obm8_rect.left:
direct_x6 = -1
# 7
obm7_rect.x = obm7_rect.x + 1 * direct_x7
if obm7_rect.x == obm5_rect.right:
direct_x7 = 1
if obm7_rect.right == obm8_rect.left:
direct_x7 = -1
#斜行运动
obm9_rect.x = obm9_rect.x + 1 * direct_z
obm9_rect.y = obm9_rect.y + 1 * direct_z
obm10_rect.x = obm10_rect.x + 1 * direct_f
obm10_rect.y = obm10_rect.y + 1 * direct_z
obm11_rect.x = obm11_rect.x + 1 * direct_f
obm11_rect.y = obm11_rect.y + 1 * direct_f
obm12_rect.x = obm12_rect.x + 1 * direct_z
obm12_rect.y = obm12_rect.y + 1 * direct_f
if obm9_rect.x == 1210 :
direct_z = -1
direct_f = 1
if obm9_rect.y == 0:
direct_z = 1
direct_f = -1
#直行运动
obm13_rect.y = obm13_rect.y + 1 * direct_f2
obm14_rect.x = obm14_rect.x + 1 * direct_z2
obm15_rect.y = obm15_rect.y + 1 * direct_z2
obm16_rect.x = obm16_rect.x + 1 * direct_f2
if obm13_rect.y == 0:
direct_z2 = -1
direct_f2 = 1
if obm13_rect.y == 260:
direct_z2 = 1
direct_f2 = -1
speed = 1
#障碍碰撞阻止
#第一关
if ((obm_rect.left - 20 < people_rect.x < obm_rect.right) or (obm2_rect.left -20 < people_rect.x < obm2_rect.right)) and ((obm_rect.top -20 < people_rect.y < obm_rect.bottom) or (obm3_rect.top -20 < people_rect.y < obm3_rect.bottom)):
moving_LEFT = False
moving_RIGHT = False
moving_UP = False
moving_DOWN = False
if ((obm_rect.left - 20 < people2_rect.x < obm_rect.right) or (obm2_rect.left - 20 < people2_rect.x < obm2_rect.right)) and ((obm_rect.top -20 < people2_rect.y < obm_rect.bottom) or (obm3_rect.top -20 < people2_rect.y < obm3_rect.bottom)):
moving_LEFT2 = False
moving_RIGHT2 = False
moving_UP2 = False
moving_DOWN2 = False
#第二关
if obm5_rect.right >= people_rect.x >= obm5_rect.left - 20 and people_rect.y < obm5_rect.bottom :
moving_RIGHT = False
moving_LEFT = False
if people_rect.y <= obm5_rect.bottom and obm5_rect.right > people_rect.x > obm5_rect.left - 20 :
moving_UP = False
moving_DOWN = False
if obm8_rect.right >= people_rect.x >= obm8_rect.left - 20 and people_rect.y < obm8_rect.bottom :
moving_RIGHT = False
moving_LEFT = False
if people_rect.y <= obm8_rect.bottom and obm8_rect.right > people_rect.x > obm8_rect.left - 20 :
moving_UP = False
moving_DOWN = False
if obm6_rect.right >= people_rect.x >= obm6_rect.left - 20 and people_rect.y > obm6_rect.top :
moving_RIGHT = False
moving_LEFT = False
if people_rect.y >= obm6_rect.top -20 and obm6_rect.right > people_rect.x > obm6_rect.left - 20 :
moving_DOWN = False
moving_UP = False
if obm7_rect.right >= people_rect.x >= obm7_rect.left - 20 and people_rect.y < obm7_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if people_rect.y <= obm7_rect.bottom and obm7_rect.right > people_rect.x > obm7_rect.left - 20:
moving_UP = False
moving_DOWN = False
if obm5_rect.right >= people2_rect.x >= obm5_rect.left - 20 and people2_rect.y < obm5_rect.bottom :
moving_RIGHT2 = False
moving_LEFT2 = False
if people2_rect.y <= obm5_rect.bottom and obm5_rect.right > people2_rect.x > obm5_rect.left - 20 :
moving_UP2 = False
moving_DOWN2 = False
if obm8_rect.right >= people2_rect.x >= obm8_rect.left - 20 and people2_rect.y < obm8_rect.bottom :
moving_RIGHT2 = False
moving_LEFT2 = False
if people2_rect.y <= obm8_rect.bottom and obm8_rect.right > people2_rect.x > obm8_rect.left - 20 :
moving_UP2 = False
moving_DOWN2 = False
if obm6_rect.right >= people2_rect.x >= obm6_rect.left - 20 and people2_rect.y > obm6_rect.top :
moving_RIGHT2 = False
moving_LEFT2 = False
if people2_rect.y >= obm6_rect.top -20 and obm6_rect.right > people2_rect.x > obm6_rect.left - 20 :
moving_DOWN2 = False
moving_UP2 = False
if obm7_rect.right >= people2_rect.x >= obm7_rect.left - 20 and people2_rect.y < obm7_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if people2_rect.y <= obm7_rect.bottom and obm7_rect.right > people2_rect.x > obm7_rect.left - 20:
moving_UP2 = False
moving_DOWN2 = False
#第三关判断运动阻止
if obm9_rect.right >= people_rect.x >= obm9_rect.left - 20 and obm9_rect.top - 20 < people_rect.y < obm9_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm9_rect.right > people_rect.x > obm9_rect.left - 20 and obm9_rect.top - 20 <= people_rect.y <= obm9_rect.bottom:
moving_UP = False
moving_DOWN = False
if obm10_rect.right >= people_rect.x >= obm10_rect.left - 20 and obm10_rect.top - 20 < people_rect.y < obm10_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm10_rect.right > people_rect.x > obm10_rect.left - 20 and obm10_rect.top - 20 <= people_rect.y <= obm10_rect.bottom:
moving_UP = False
moving_DOWN = False
if obm11_rect.right >= people_rect.x >= obm11_rect.left - 20 and obm11_rect.top - 20 < people_rect.y < obm11_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm11_rect.right > people_rect.x > obm11_rect.left - 20 and obm11_rect.top - 20 <= people_rect.y <= obm11_rect.bottom:
moving_UP = False
moving_DOWN = False
if obm12_rect.right >= people_rect.x >= obm12_rect.left - 20 and obm12_rect.top - 20 < people_rect.y < obm12_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm12_rect.right > people_rect.x > obm12_rect.left - 20 and obm12_rect.top - 20 <= people_rect.y <= obm12_rect.bottom:
moving_UP = False
moving_DOWN = False
if obm13_rect.right >= people_rect.x >= obm13_rect.left - 20 and obm13_rect.top - 20 < people_rect.y < obm13_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm13_rect.right > people_rect.x > obm13_rect.left - 20 and obm13_rect.top - 20 <= people_rect.y <= obm13_rect.bottom:
moving_UP = False
moving_DOWN = False
if obm14_rect.right >= people_rect.x >= obm14_rect.left - 20 and obm14_rect.top - 20 < people_rect.y < obm14_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm14_rect.right > people_rect.x > obm14_rect.left - 20 and obm14_rect.top - 20 <= people_rect.y <= obm14_rect.bottom:
moving_UP = False
moving_DOWN = False
if obm15_rect.right >= people_rect.x >= obm15_rect.left - 20 and obm15_rect.top - 20 < people_rect.y < obm15_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm15_rect.right > people_rect.x > obm15_rect.left - 20 and obm15_rect.top - 20 <= people_rect.y <= obm15_rect.bottom:
moving_UP = False
moving_DOWN = False
if obm16_rect.right >= people_rect.x >= obm16_rect.left - 20 and obm16_rect.top - 20 < people_rect.y < obm16_rect.bottom:
moving_RIGHT = False
moving_LEFT = False
if obm16_rect.right > people_rect.x > obm16_rect.left - 20 and obm16_rect.top - 20 <= people_rect.y <= obm16_rect.bottom:
moving_UP = False
moving_DOWN = False
#people2
if obm9_rect.right >= people2_rect.x >= obm9_rect.left - 20 and obm9_rect.top - 20 < people2_rect.y < obm9_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm9_rect.right > people2_rect.x > obm9_rect.left - 20 and obm9_rect.top - 20 <= people2_rect.y <= obm9_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
if obm10_rect.right >= people2_rect.x >= obm10_rect.left - 20 and obm10_rect.top - 20 < people2_rect.y < obm10_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm10_rect.right > people2_rect.x > obm10_rect.left - 20 and obm10_rect.top - 20 <= people2_rect.y <= obm10_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
if obm11_rect.right >= people2_rect.x >= obm11_rect.left - 20 and obm11_rect.top - 20 < people2_rect.y < obm11_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm11_rect.right > people2_rect.x > obm11_rect.left - 20 and obm11_rect.top - 20 <= people2_rect.y <= obm11_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
if obm12_rect.right >= people2_rect.x >= obm12_rect.left - 20 and obm12_rect.top - 20 < people2_rect.y < obm12_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm12_rect.right > people2_rect.x > obm12_rect.left - 20 and obm12_rect.top - 20 <= people2_rect.y <= obm12_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
if obm13_rect.right >= people2_rect.x >= obm13_rect.left - 20 and obm13_rect.top - 20 < people2_rect.y < obm13_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm13_rect.right > people2_rect.x > obm13_rect.left - 20 and obm13_rect.top - 20 <= people2_rect.y <= obm13_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
if obm14_rect.right >= people2_rect.x >= obm14_rect.left - 20 and obm14_rect.top - 20 < people2_rect.y < obm14_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm14_rect.right > people2_rect.x > obm14_rect.left - 20 and obm14_rect.top - 20 <= people2_rect.y <= obm14_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
if obm15_rect.right >= people2_rect.x >= obm15_rect.left - 20 and obm15_rect.top - 20 < people2_rect.y < obm15_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm15_rect.right > people2_rect.x > obm15_rect.left - 20 and obm15_rect.top - 20 <= people2_rect.y <= obm15_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
if obm16_rect.right >= people2_rect.x >= obm16_rect.left - 20 and obm16_rect.top - 20 < people2_rect.y < obm16_rect.bottom:
moving_RIGHT2 = False
moving_LEFT2 = False
if obm16_rect.right > people2_rect.x > obm16_rect.left - 20 and obm16_rect.top - 20 <= people2_rect.y <= obm16_rect.bottom:
moving_UP2 = False
moving_DOWN2 = False
time.sleep(0.008)
# 胜利判断
# 绘制图像
screen_image.fill(white_color)
pygame.draw.rect(screen_image, red_color, people_rect)
pygame.draw.rect(screen_image, blue_color, people2_rect)
pygame.draw.rect(screen_image, black_color, ob_rect)
pygame.draw.rect(screen_image, black_color, ob2_rect)
pygame.draw.rect(screen_image, black_color, ob3_rect)
pygame.draw.rect(screen_image, black_color, ob4_rect)
#移动目标绘制
pygame.draw.rect(screen_image, black_color, obm_rect)
pygame.draw.rect(screen_image, black_color, obm2_rect)
pygame.draw.rect(screen_image, black_color, obm3_rect)
pygame.draw.rect(screen_image, black_color, obm4_rect)
pygame.draw.rect(screen_image, black_color, obm5_rect)
pygame.draw.rect(screen_image, black_color, obm6_rect)
pygame.draw.rect(screen_image, black_color, obm7_rect)
pygame.draw.rect(screen_image, black_color, obm8_rect)
pygame.draw.rect(screen_image, black_color, obm9_rect)
pygame.draw.rect(screen_image, black_color, obm10_rect)
pygame.draw.rect(screen_image, black_color, obm11_rect)
pygame.draw.rect(screen_image, black_color, obm12_rect)
pygame.draw.rect(screen_image, black_color, obm13_rect)
pygame.draw.rect(screen_image, black_color, obm14_rect)
pygame.draw.rect(screen_image, black_color, obm15_rect)
pygame.draw.rect(screen_image, black_color, obm16_rect)
pygame.display.flip()