作业一: 思聪爱吃热狗游戏
游戏介绍:
一款单人版的思聪吃热狗游戏,你可以自己调节思聪的位置, 移动时会消耗能量
10, 游戏中吃到热狗分数加 1, 能量加 20,最后的目标就是称霸世界咯, 吃掉
所有的热狗即游戏胜利。王思聪能量消耗完毕即游戏失败。
如何开始:
玩家:键盘方向键↑↓←→控制王思聪的移动。
游戏进阶要求:能否实现一个双人版吃热狗游戏?
代码实现如下:
"""
Date: 2019--16 10:18
User: yz
Email: 1147570523@qq.com
Desc:
"""
import random
import pygame
import sys
from pygame.locals import *
width = 640
height = 480
pygame.init()
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption('思聪吃面包小游戏') # 窗口定义标题
background = pygame.image.load("./img/bg.jpg").convert()
breadImg = pygame.image.load("./img/bread.png").convert_alpha()
sicongImg = pygame.image.load("./img/sicong.png").convert_alpha()
fpsClock = pygame.time.Clock()
# 成绩显示
count1 = 0
count2 = 0
font = pygame.font.SysFont("arial", 40)
score1 = font.render("score1:%d" % count1, True, (255, 255, 255))
score2 = font.render("score2:%d" % count2, True, (255, 255, 255))
w_width = sicongImg.get_width() - 5
w_height = sicongImg.get_width() - 5
y_width = breadImg.get_width() - 5
y_height = breadImg.get_height() - 5
# 思聪类
class Sicong:
def __init__(self, hp=100):
self.x = random.randint(0, width - 10)
self.y = random.randint(0, height - 10)
self.hp = hp
def move(self, new_x, new_y):
if new_x < 0:
self.x = width - w_width
elif new_x > width:
self.x = 0
else:
self.x = new_x
if new_y < 0:
self.y = height - w_height
elif new_y > height:
self.y = 0
else:
self.y = new_y
self.hp -= 1
def eat_bread(self):
self.hp += 20
if self.hp > 100:
self.hp = 100
# 面包类
class Bread:
def __init__(self):
self.x = random.randint(0, width)
self.y = random.randint(0, height)
def move(self):
new_x = self.x + random.choice([-10])
if new_x < 0:
self.x = width
else:
self.x = new_x
# 实例化思聪和面包
sicong1 = Sicong()
sicong2 = Sicong()
breads = [Bread() for i in range(30)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit() # 0为正常退出
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
sicong1.move(sicong1.x - 20, sicong1.y)
if event.key == pygame.K_RIGHT:
sicong1.move(sicong1.x + 20, sicong1.y)
if event.key == pygame.K_UP:
sicong1.move(sicong1.x, sicong1.y - 20)
if event.key == pygame.K_DOWN:
sicong1.move(sicong1.x, sicong1.y + 20)
if event.key == pygame.K_a:
sicong2.move(sicong2.x - 20, sicong2.y)
if event.key == pygame.K_d:
sicong2.move(sicong2.x + 20, sicong2.y)
if event.key == pygame.K_w:
sicong2.move(sicong2.x, sicong2.y - 20)
if event.key == pygame.K_s:
sicong2.move(sicong2.x, sicong2.y + 20)
screen.blit(background, (0, 0))
screen.blit(score2, (500, 20)) # 绘制分数
screen.blit(score1, (0, 20)) # 绘制分数
# 绘制面包的图案
for item in breads:
screen.blit(breadImg, (item.x, item.y))
item.move()
# 绘制思聪的图案
screen.blit(sicongImg, (sicong1.x, sicong1.y))
screen.blit(sicongImg, (sicong2.x, sicong2.y))
# 当两个玩家体力都为0 或者面包全被吃掉后 游戏结束
if (sicong1.hp < 0 and sicong2.hp <= 0) or len(breads) == 0:
if count1 > count2:
print("Game Over - ")
print("玩家1胜利,共吃到%d个面包" % count1)
sys.exit()
elif count1 < count2:
print("Game Over - ")
print("玩家2胜利,共吃到%d个面包" % count2)
sys.exit()
elif count1 == count2:
print("Game Over - ")
print("平局")
sys.exit()
for item in breads:
if ((sicong1.x < item.x + w_width) and (sicong1.x + w_width > item.x) and (sicong1.y < item.y + y_width) and
(sicong1.y + w_height > item.y)):
# 思聪的坐标加长思聪的长度和面包的坐标和面包的长度 重合时,思聪吃掉面包
sicong1.eat_bread()
breads.remove(item) # 思聪吃掉面包后,删除掉该面包
count1 = count1 + 1
score1 = font.render("score1: %d" % count1, True, (255, 255, 255))
elif ((sicong2.x < item.x + w_width) and (sicong2.x + w_width > item.x) and
(sicong2.y < item.y + y_width) and (sicong2.y + w_height > item.y)):
sicong2.eat_bread()
breads.remove(item) # 思聪吃掉面包后,删除掉该面包
count2 = count2 + 1
score2 = font.render("score2: %d" % count1, True, (255, 255, 255))
pygame.display.update()
pygame.display.update()
fpsClock.tick(10)
游戏界面如下
作业二: 按奇偶排序数组
方法一:
A = [1,5,8,3,4,2]
sortList = sorted(A, key=lambda x: (x % 2 == 1))
print(sortList)
方法二:
numList = [1, 5, 4, 2, 7, 8]
evenList = list(filter(lambda x: (x % 2 == 0), numList))
oddList = list(filter(lambda x: (x % 2 != 0), numList))
result =evenList+oddList # 再把奇数加到偶数后面
print(result)
作业三: 电话号码的字母组合
具体实现:
keyboard= {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
def letter_compose(nums):
letter_compose_List = []
# 判断输入是否为空
if len(nums) == 0:
return []
# # 递归出口,返回最后一个数字对应的字母
if len(nums) == 1:
return keyboard.get(nums[0])
# 一直递归找到输入的最后一个数字对应的字母
num = letter_compose(nums[1:])
for i in keyboard.get(nums[0]):
for j in num:
letter_compose_List.append(i + j)
return letter_compose_List
num = input('输入两个2~9之间的数字:')
letter_compose = letter_compose(num)
print(letter_compose)