"""
作业:
1). 实现游戏重新开始的按钮;
2). 实现一个双人游戏;
"""
import random
import time
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("bg.jpg").convert()
HotDog = pygame.image.load("hotdog.png").convert_alpha()
player1Img = pygame.image.load("wsc.png").convert_alpha()
player2Img = pygame.image.load("wsc.png").convert_alpha()
# 成绩文字显示
count = 0
count1 = 0
font = pygame.font.SysFont("arial", 40)
score = font.render("score %d" % count, True, (255, 255, 255))
score1 = font.render("score1 %d" % count1, True, (255, 255, 255))
w_width = player1Img.get_width() - 5 # 得到图片的宽度,后面留着用
w_height = player1Img.get_height() - 5 # 得到图片的高度
p_width = player2Img.get_width() - 5
p_height = player2Img.get_height() - 5
y_width = HotDog.get_width() - 5 # 得到热狗图片的宽度
y_height = HotDog.get_height() - 5 # 得到热狗图片的高度
fpsClock = pygame.time.Clock() # 创建一个新的Clock对象,可以用来跟踪总共的时间
# 玩家类
class player:
def __init__(self):
self.power = 200 # 体力
# 玩家坐标
self.x = random.randint(0, width - w_width)
self.y = random.randint(0, height - w_height)
# 玩家移动的方法:移动方向均随机 第四条
def move(self, new_x, new_y):
# 判断移动后是否超出边界
if new_x < 0:
self.x = width
elif new_x > width:
# self.x=width-(new_x-width)
self.x = 0
else:
self.x = new_x
if new_y < 0:
self.y = height
elif new_y > height:
# self.y=height-(new_y-height)
self.y = 0
else:
self.y = new_y
self.power -= 10 # 玩家每移动一次,体力消耗10
pygame.display.update()
print(self.power)
def eat(self):
self.power += 30 # 玩家吃掉热狗,玩家体力增加30
if self.power > 200:
self.power = 200 # 玩家体力200(上限)
# 热狗类
class Hotdog:
def __init__(self):
# 热狗坐标
self.x = random.randint(0, width - y_width)
self.y = random.randint(0, height - y_height)
def move(self):
new_x = self.x + random.choice([-10])
if new_x < 0:
self.x = width
else:
self.x = new_x
# 开始测试数据
player1 = player()
player2 = player()
# 生成玩家
status = font.render("HP %d" %player1.power , True, (255, 255, 255))
status1 = font.render("HP %d" %player2.power , True, (255, 255, 255))
hotdog = [] # 随机生成热狗
for item in range(random.randint(10,40)):
newhotdog = Hotdog()
hotdog.append(newhotdog)
# pygame有一个事件循环,不断检查用户在做什么。事件循环中,如何让循环中断下来(pygame形成的窗口中右边的插号在未定义前是不起作用的)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
# 通过上下左右方向键控制玩家的动向
if event.key == pygame.K_LEFT:
player1.move(player1.x - 10, player1.y)
if event.key == pygame.K_RIGHT:
player1.move(player1.x + 10, player1.y)
if event.key == pygame.K_UP:
player1.move(player1.x, player1.y - 10)
if event.key == pygame.K_DOWN:
player1.move(player1.x, player1.y + 10)
if event.key == pygame.K_a:
player2.move(player2.x - 10, player2.y)
if event.key == pygame.K_d:
player2.move(player2.x + 10, player2.y)
if event.key == pygame.K_w:
player2.move(player2.x, player2.y - 10)
if event.key == pygame.K_s:
player2.move(player2.x, player2.y + 10)
pygame.display.update()
status = font.render("HP %d" % player1.power, True, (255, 255, 255))
status1 = font.render("HP %d" % player2.power, True, (255, 255, 255))
screen.blit(background, (0, 0)) # 绘制背景图片
screen.blit(score, (430, 30)) # 绘制分数
screen.blit(score1, (430, 60)) # 绘制分数
screen.blit(status, (70, 30)) # 绘制分数
screen.blit(status1, (70, 60)) # 绘制分数
# 绘制鱼
for item in hotdog:
screen.blit(HotDog, (item.x, item.y))
# pygame.time.delay(100)
item.move() # 热狗移动
screen.blit(player1Img, (player1.x, player1.y)) # 绘制玩家
screen.blit(player2Img, (player2.x, player2.y)) # 绘制玩家
# 判断游戏是否结束:当玩家体力值为0(挂掉)或者热狗的数量为0游戏结束
if player1.power < 0 or player2.power < 0:
if count > count1:
print("王思聪赢了")
else:
print('屌丝赢了')
# 显示游戏状态
status = font.render("Game Over: player power is 0", True, (255, 255, 255))
pygame.display.update() # 更新到游戏窗口
time.sleep(1)
sys.exit(0)
elif len(hotdog) == 0:
if count > count1:
print("王思聪赢了")
else:
print('屌丝赢了')
status = font.render("Game Over: Hotdog is empty", True, (255, 255, 255))
pygame.display.update() # 更新到游戏窗口
sys.exit(0)
for item in hotdog:
# 判断玩家和热狗是否碰撞?
if ((player1.x < item.x + y_width) and (player1.x + w_width > item.x)
and (player1.y < item.y + y_height) and (w_height + player1.y > item.y)) :
hotdog.remove(item) # 热狗被吃
player1.eat()
status = font.render("HP %d" % player1.power, True, (255, 255, 255))
count = count + 1 # 累加
score = font.render("score %d" % count, True, (255, 255, 255))
pygame.display.update()
if ((player2.x < item.x + y_width) and (player2.x + w_width > item.x)
and (player2.y < item.y + y_height) and (w_height + player2.y > item.y)) :
hotdog.remove(item) # 热狗被吃
player2.eat()
status1 = font.render("HP %d" % player2.power, True, (255, 255, 255))
count1 = count1 + 1 # 累加
score1 = font.render("score1 %d" % count1, True, (255, 255, 255))
pygame.display.update()
pygame.display.update() # 更新到游戏窗口
fpsClock.tick(10) # 通过每帧调用一次fpsClock.tick(10),这个程序就永远不会以超过每秒10帧的速度运行
A = [3,1,2,4]
print(sorted(A, key=lambda x: 1 if x % 2 == 0 else 0,reverse=True))
二:
A = [3,1,2,4]
for i in A:
if i%2 == 0:
A.remove(i)
A.insert(0,i)
print(A)
keyboard = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
“6”: “mno”, “7”: “pqrs”, “8”: “tuv”, “9”: “wxyz”}
def number(nums):
list = []
if len(nums) == 0:
return []
if len(nums) == 1:
for i in keyboard.get(nums[0]):
list.append(i)
return list
result = number(nums[1:])
for i in keyboard.get(nums[0]):
for j in result:
list.append(i + j)
return list
nums = input('输入字符')
result = number(nums)
print(result)