Python小作业就这么解决
关注我,解决更多python小作业,不会的地方可以关注+私信或者留言
目录
前言
推箱子是一个经典的小游戏,在很多c语言的课堂上老师会布置类似的作业,今天我将教会大家如何用python制作推箱子的游戏。
一、引入库
pygame.py
import pygame, sys
import css
import copy
from pygame import mixer
import time
二、文件导入以及游戏页面设置
snow_monster = pygame.transform.scale(pygame.image.load("snow_monster.png"), (60, 60))
wall = pygame.transform.scale(pygame.image.load("wall.gif"), (60, 60))
win = pygame.transform.scale(pygame.image.load("win.png"), (60, 60))
snowball = pygame.transform.scale(pygame.image.load("snowball.png"), (60, 60))
gun = pygame.transform.scale(pygame.image.load("gun.png"), (60, 60))
ice = pygame.transform.scale(pygame.image.load("ice.gif"), (60, 60))
root = pygame.display.set_mode((60 * 12, 60 * 12), 0, 32) # game screen size
clock = pygame.time.Clock()
mixer.init()# import music
mixer.music.load('Enemy Approaching.mp3')
mixer.music.play(-1)
在这里导入了箱子、怪兽、bgm等元素,由于文件这里无法上传,我会上传至我的github中,或者关注公主号私信领取。
三、主体代码
地图绘制,分别对应三张地图
cs={
"cs1":[
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,2,2,2,0,0,0,0,0,0],
[0,0,0,2,4,2,0,0,0,0,0,0],
[0,0,0,2,1,2,2,2,2,0,0,0],
[0,2,2,2,5,1,5,4,2,0,0,0],
[0,2,4,1,5,3,2,2,2,0,0,0],
[0,2,2,2,2,5,2,0,0,0,0,0],
[0,0,0,0,2,4,2,0,0,0,0,0],
[0,0,0,0,2,2,2,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0]],
"cs2":[
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,2,2,2,2,2,0,0,0,0,0,0],
[0,2,3,1,1,2,0,0,0,0,0,0],
[0,2,1,5,5,2,0,2,2,2,0,0],
[0,2,1,5,1,2,0,2,4,2,0,0],
[0,2,2,2,1,2,2,2,4,2,0,0],
[0,0,2,2,1,1,1,1,4,2,0,0],
[0,0,2,1,1,1,2,1,1,2,0,0],
[0,0,2,1,1,1,2,2,2,2,0,0],
[0,0,2,2,2,2,2,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0]],
"cs3":[
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,2,2,2,2,2,0,0,0,0],
[0,0,0,2,1,1,2,2,2,0,0,0],
[0,0,0,2,3,5,1,1,2,0,0,0],
[0,0,2,2,2,1,2,1,2,2,0,0],
[0,0,2,4,2,1,2,1,1,2,0,0],
[0,0,2,4,5,1,1,2,1,2,0,0],
[0,0,2,4,1,1,1,5,1,2,0,0],
[0,0,2,2,2,2,2,2,2,2,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0]],
}
def __init__(self):
self.css = copy.deepcopy(cs) # 复制地图数据
self.level = 1
self.name = f"Snowball Pusher: Level{self.level} "
self.cs = self.css[f"cs{self.level}"]
self.cslist =self.listfun()
self.canvas = pygame.display.set_mode((12*60,12*60))
def draw(self, root,text,direct): # 绘制游戏地图
for y in range(len(self.cs)):
for x in range(len(self.cs[y])):
root.blit(text, (40 * 12, 10))
if self.cs[y][x] == 2:
root.blit(ice, (x * 60, y * 60))
elif self.cs[y][x] == 3:
if direct == "左":
img_copy = snow_monster.copy()
img_with_flip = pygame.transform.flip(img_copy, True, False)
root.blit(img_with_flip, (x * 60, y * 60))
elif direct == "右":
root.blit(snow_monster, (x * 60, y * 60))
else:
root.blit(snow_monster, (x * 60, y * 60))
elif self.cs[y][x] == 4:
root.blit(gun, (x * 60, y * 60))
elif self.cs[y][x] == 5:
root.blit(snowball, (x * 60, y * 60))
elif self.cs[y][x] == 6:
root.blit(win, (x * 60, y * 60))
def listfun(self):
list=[]
for y in range(len(self.cs)):
for x in range(len(self.cs[y])):
if self.cs[y][x] == 4 or self.cs[y][x] == 6:
list.append([y, x])
return list
def updatefun(self): # 更新地图状态的逻辑
for y in range(len(self.cs)):
for x in range(len(self.cs[y])):
for i in self.cslist:
if self.cs[y][x]==5 and y==i[0] and x==i[1]:
self.cs[y][x]=6
if (self.cs[y][x]==0 or self.cs[y][x]==1) and y==i[0] and x==i[1]:
self.cs[y][x]=4
def move(self, dir): #角色移动逻辑
for y in range(len(self.cs)):
for x in range(len(self.cs[y])):
if self.cs[y][x] == 3:
if dir == "left":
if self.cs[y][x - 2] != 2 and self.cs[y][x - 2] != 6 and self.cs[y][x - 2] != 5 and self.cs[y][x - 1] == 5:
self.cs[y][x - 2] = 5
self.cs[y][x - 1] = 3
self.cs[y][x] = 1
return
elif self.cs[y][x - 2] != 2 and self.cs[y][x - 2] != 6 and self.cs[y][x - 2] != 5 and self.cs[y][x - 1] == 6:
self.cs[y][x - 2] = 5
self.cs[y][x - 1] = 3
self.cs[y][x] = 1
return
elif self.cs[y][x - 1] != 2 and self.cs[y][x - 1] != 6 and self.cs[y][x - 1] != 5:
self.cs[y][x - 1] = 3
self.cs[y][x] = 0
return
elif dir == "up":
if self.cs[y-2][x] != 2 and self.cs[y-2][x] != 6 and self.cs[y-2][x] != 5 and self.cs[y-1][x] == 5:
self.cs[y-2][x] = 5
self.cs[y-1][x] = 3
self.cs[y][x] = 1
return
elif self.cs[y - 2][x] != 2 and self.cs[y - 2][x] != 6 and self.cs[y - 2][x] != 5 and self.cs[y - 1][x] == 6:
self.cs[y - 2][x] = 5
self.cs[y - 1][x] = 3
self.cs[y][x] = 1
return
elif self.cs[y-1][x] != 2 and self.cs[y-1][x] != 6 and self.cs[y-1][x] != 5:
self.cs[y-1][x] = 3
self.cs[y][x] = 0
return
elif dir == "right":
if self.cs[y][x + 2] != 2 and self.cs[y][x + 2] != 6 and self.cs[y][x + 2] != 5 and self.cs[y][x +1] == 5:
self.cs[y][x + 2] = 5
self.cs[y][x + 1] = 3
self.cs[y][x] = 1
return
elif self.cs[y][x + 2] != 2 and self.cs[y][x + 2] != 6 and self.cs[y][x + 2] != 5 and self.cs[y][x + 1] == 6:
self.cs[y][x + 2] = 5
self.cs[y][x + 1] = 3
self.cs[y][x] = 1
return
elif self.cs[y][x + 1] != 2 and self.cs[y][x + 1] != 6 and self.cs[y][x + 1] != 5:
self.cs[y][x + 1] = 3
self.cs[y][x] = 0
return
elif dir == "down":
if self.cs[y+2][x] != 2 and self.cs[y+2][x] != 6 and self.cs[y+2][x] != 5 and self.cs[y+1][x] == 5:
self.cs[y+2][x] = 5
self.cs[y+1][x] = 3
self.cs[y][x] = 1
return
elif self.cs[y + 2][x] != 2 and self.cs[y + 2][x] != 6 and self.cs[y + 2][x] != 5 and self.cs[y + 1][x] == 6:
self.cs[y + 2][x] = 5
self.cs[y + 1][x] = 3
self.cs[y][x] = 1
return
elif self.cs[y+1][x] != 2 and self.cs[y+1][x] != 6 and self.cs[y+1][x] != 5:
self.cs[y+1][x] = 3
self.cs[y][x] = 0
return
def slfun(self): # 进入下一关
global start_time
for i in self.cslist:
if self.cs[i[0]][i[1]] != 6:
return
self.level += 1
if self.level > len(self.css):
txt = "congratulation"
start_time = time.time()
font = pygame.font.Font('汉仪雪峰体简.ttf', 60)
newfont = font.render(txt, True,(173, 216, 230), (255, 255, 255))
self.canvas.blit(newfont, (1.5*60, 5*60))
else:
self.name = f"Snowball Pusher: Level{self.level} "
self.cs = self.css[f"cs{self.level}"]
pygame.display.set_caption(self.name)
self.cslist = self.listfun()
计时器
def countdown(my,root,direct):
global game_over, countdown_time,game_time,start_time
pygame.font.init()
font = pygame.font.Font('汉仪雪峰体简.ttf', 60)
if countdown_time and not game_over:
mins, secs = divmod(countdown_time, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
text = font.render(timeformat, True, (255, 255, 255))
my.draw(root,text,direct)
t1 = int(time.time() - start_time) # 计时时间间隔
countdown_time = game_time - t1
if countdown_time ==0:
root1 = pygame.display.set_mode((60 * 12, 60 * 12), 0, 32)
font = pygame.font.Font('汉仪雪峰体简.ttf', 30)
text = font.render('You are out of time!', True, (255, 0, 0))
root1.blit(text,(60 * 2, 60 * 6))
pygame.display.update()
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key== pygame.K_SPACE :
countdown_time = game_time
start_time = time.time()
主函数
def main():
global root,clock,direct
global game_over, countdown_time,game_time,start_time
direct = "右"
game_over = False
game_time = 90
start_time = time.time()
countdown_time = 10
pygame.init()
my = My()
pygame.display.set_caption(my.name)
root = pygame.display.set_mode((60 * 12, 60 * 12), 0, 32)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
my.move('up')
elif event.key == pygame.K_DOWN:
my.move('down')
elif event.key == pygame.K_LEFT:
my.move('left')
direct = "左"
elif event.key == pygame.K_RIGHT:
my.move('right')
direct = "右"
elif event.key==pygame.K_SPACE: # replay
my.css = copy.deepcopy(cs)
my.name = f"Snowball Pusher: Level{my.level} "
my.cs = my.css[f"cs{my.level}"]
my.cslist = my.listfun()
root.fill((173, 216, 230))
countdown(my,root,direct)
my.slfun()
my.updatefun()
pygame.display.update()
clock.tick(30)
注意事项(完整的代码和数据在公主号和Github)
大家白嫖代码的时候一定要注意我标明的三方库版本,版本不对很有可能会出错,此外最后保存数据的方法有很多大家可以自行挑选更改。
最重要的一点,如果大家觉得有用跪求大家给我一个关注和点赞,有不懂的问题可以私信或者留言,欢迎大家关注我的公主号,上面有更多更详尽的代码,喜欢白嫖的有福了。
GitHub - Maekfei/Spider-projects: 爬虫实战,集合了数十个爬虫实战代码,全都亲测可用,借鉴麻烦点个star谢谢 同时欢迎访问我的github主页,copy代码的同时别忘了点个star 谢谢!