python小游戏#魔兽游戏#英雄联盟lol#地图
下面是游戏运行截图
初始化界面
import pygame
import random
import math
pygame.init()
#1. 初始化界面
screen = pygame.display.set_mode((1300, 800), 0, 32)
#创建了一个窗口
pygame.display.set_caption('魔兽')#程序名称
#设置窗口标题
#背景
background_image_filename = 'LOL.png'
mouse_image_filename = '1.png'
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
#鼠标右键位置
pygame.mixer.music.load('bao.mp3')
#加载并转换图像
#地图
mapIng = pygame.image.load('LOL.png')
bgX = -495
bgY = -317
mapstep = 0
mapstep2 = 0
ps1 = 0
ps2 = 0
#英雄
playerImg = pygame.image.load('player.png')
playerX = 400 #玩家的X坐标
playerY = 400 #玩家的Y坐标
playerStep = 0
playerStep2 = 0
score = 0
font = pygame.font.SysFont('simsunnsimsun',32)
#font = pygame.font.SysFont('simsunnsimsun',32) #宋体
显示分数
def show_score():
text = f"Score: {score}"
score_render = font.render(text, True, (0,255,0))
screen.blit(score_render, (10,10))
游戏结束
is_over = False
over_font = pygame.font.Font('freesansbold.ttf', 64)
def check_is_over():
if is_over:
text = "Game Over "
render = over_font.render(text,True,(255,0,0))
screen.blit(render, (200,250))
# 两个点之间的距离
def distance(bx, by, ex, ey):
a = bx - ex
b = by - ey
return math.sqrt(a * a + b * b) # 开根号
技能类
class Bullet():
def __init__(self):
self.img = pygame.image.load('bullet.png')
self.x = playerX + 150
self.y = playerY - 30
self.step = 30
#击中
def hit(self):
global score
for e in enemies:
if(distance(self.x, self.y, e.x, e.y) < 60):
pygame.mixer.music.play()
e.reset()
score += 1
print(score)
bullets = []
显示技能
def show_bullets():
for b in bullets:
screen.blit(b.img, (b.x, b.y))
b.hit() #看看是否击中了敌人
b.x += b.step #移动子弹
#判断子弹是否出了界面,如果出了就移除掉
if b.x-playerX>300:
bullets.remove(b)
敌人类
number_of_enemies = 2 # 敌人的数量
class Enemy():
def __init__(self):
self.img = pygame.image.load('enemy.png')
self.x = random.randint(20, 1000)
self.y = random.randint(20, 700)
self.step = random.randint(1, 2)
# 当被射中时,恢复位置
def reset(self):
self.x = random.randint(50, 1000)
self.y = random.randint(50, 700)
敌人
def show_enemy():
global is_over
for e in enemies:
screen.blit(e.img,(e.x, e.y))
e.x += e.step
e.y += e.step
if(e.x > 1200 or e.x < 0) or (e.y >700 or e.y < 0):
e.step *= -1
if distance(playerX, playerY, e.x, e.y) < 80:
is_over = True
print("游戏结束啦")
enemies.clear()
地图
def map():
global bgX
global bgY
bgX += mapstep
bgY += mapstep2
if bgX < -980:
bgX = -980
if bgX > 0:
bgX = 0
if bgY < -600:
bgY = -600
if bgY >0:
bgY = 0
enemies = [] #保存所有的敌人
for i in range(number_of_enemies):
enemies.append(Enemy())
鼠标
def mouse():
x, y = pygame.mouse.get_pos()
x -= mouse_cursor.get_width() / 2
y -= mouse_cursor.get_height() / 2
# 计算光标的左上角位置
screen.blit(mouse_cursor, (x, y))
# 把光标画上去
英雄
def move_player():
global playerX
global playerY
playerX += playerStep
playerY -= playerStep2
#防止英雄出界
if playerX > 1200:
playerX = 1200
if playerX < 0:
playerX = 0
if playerY > 700:
playerY = 700
if playerY < 0:
playerY = 0
游戏主循环
speed = 5
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
mapstep = 0
mapstep2 = 0
gs2 = 0
gs1 = 0
x, y = pygame.mouse.get_pos()
if y < 20:
mapstep2 = speed
gs2 = -speed
if y > 700:
mapstep2 = -speed
gs2 = speed
if x < 20:
mapstep = speed
gs1 = speed
if x > 1200:
mapstep = -speed
gs1 = -speed
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print("使用技能")
bullets.append(Bullet())
#英雄移动
if event.type == pygame.MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
print('Pressed LEFT Button!')
elif index == 1:
print('The mouse wheel Pressed!' )
elif index == 2:
print('Pressed RIGHT Button!')
mouse_x1, mouse_y1 = pygame.mouse.get_pos()
if (mouse_x1 - playerX > 10):
ps1 = 5
if (mouse_x1 - playerX < -10):
ps1 = -5
if (mouse_y1-playerY > 10 ):
ps2 = -5
if (mouse_y1 - playerY <-10):
ps2 = 5
if event.type == pygame.KEYDOWN: # 按下就移动
if event.key == pygame.K_RIGHT:
ps1 = 5
elif event.key == pygame.K_LEFT:
ps1 = -5
elif event.key == pygame.K_UP:
ps2 = 5
elif event.key == pygame.K_DOWN:
ps2 = -5
if event.type == pygame.KEYUP: # 抬起来就不动
ps1 = 0
ps2 = 0
playerStep = ps1 + gs1
playerStep2 =ps2 + gs2
screen.blit(mapIng, (bgX, bgY))
screen.blit(playerImg, (playerX, playerY))
mouse()#鼠标
map()#移动地图
move_player()#移动玩家
show_bullets()#释放技能
show_enemy()#显示敌人
show_score()#显示分数
check_is_over()#显示游戏结束字段
pygame.display.update()
#刷新一下画面