question1.思聪爱吃热狗游戏
游戏要求:
1. 游戏背景可以为黑色或者其他图片(自定义即可);
2. 王思聪可以上下左右移动, 热狗只能向左移动, 当移动到最左边时, 穿越屏
幕,到达最右端,继续向左移动。
3. 王思聪默认能量值(power)为 200,每移动一次消耗能量值 10, 当吃到一个热
狗, 能量值增加 20。
4. 王思聪只有一个, 而热狗的个数是随机的(10~40 个之间)。
5. 当热狗被彻底消灭掉或者思聪毫无能量值时,游戏结束。
游戏进阶要求:能否实现一个双人版吃热狗游戏?
玩家 1:键盘方向键↑↓←→控制移动。
玩家 2:键盘 WSAD 控制控制移动。
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('思聪恰热狗') # 定义窗口的标题为'思聪恰热狗'
HotDogImg = pygame.image.load("热狗.png").convert_alpha()
SiCongImg = pygame.image.load("思聪.png").convert_alpha()
# 成绩文字显示
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_height() - 5 # 得到思聪图片的高度
y_width = HotDogImg.get_width() - 5 # 得到热狗图片的宽度
y_height = HotDogImg.get_height() - 5 # 得到热狗# 图片的高度
fpsClock = pygame.time.Clock() # 创建一个新的Clock对象,可以用来跟踪总共的时间
# 思聪
class SiCong(object):
def __init__(self, power=200):
self.power = power # 体力
# 乌龟坐标
self.x = random.randint(0, width - w_width)
self.y = random.randint(0, height - w_height)
# 乌龟移动的方法:移动方向均随机 第四条
def move(self, newX, newY):
# 边界判断,移动不到边界外
if newX < 0:
self.x = 0
elif newX > width - w_width:
self.x = width - w_width
else:
self.x = newX
if newY < 0:
self.y = 0
elif newY > height - w_height:
self.y = height - w_height
else:
self.y = newY
self.power -= 10
def eat(self):
self.power += 20 # 乌龟吃掉鱼,乌龟体力增加20
if self.power > 200:
self.power = 200 # 乌龟体力100(上限)
# 热狗
class HotDog(object):
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
sicong1 = SiCong() # 生成思聪1
sicong2 = SiCong() # 生成思聪2
hotdog = [HotDog() for i in range(20)] # 生成20个热狗
power1 = font.render("power1: %d" % sicong1.power, True, (255, 255, 255))
power2 = font.render("power2: %d" % sicong2.power, True, (255, 255, 255))
# 开始循环
while True:
# 绘制屏幕及背景
screen.fill((111, 111, 111))
screen.blit(score1, (450, 20)) # 绘制思聪1分数
screen.blit(score2, (0, 20)) # 绘制思聪2分数
screen.blit(power1, (450, 60)) # 绘制思聪1power
screen.blit(power2, (0, 60)) # 绘制思聪2power
# 绘制热狗
for item in hotdog:
screen.blit(HotDogImg, (item.x, item.y))
item.move() # 热狗移动
screen.blit(SiCongImg, (sicong1.x, sicong1.y)) # 绘制思聪1
screen.blit(SiCongImg, (sicong2.x, sicong2.y)) # 绘制思聪2
# 思聪1,2的移动控制
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
# 通过上下左右方向键控制思聪1的动向,wasd来控制思聪2的移动
if event.key == pygame.K_LEFT:
sicong1.move(sicong1.x - 10, sicong1.y)
power = font.render("power: %d" % sicong1.power, True, (255, 255, 255))
if event.key == pygame.K_RIGHT:
sicong1.move(sicong1.x + 10, sicong1.y)
power = font.render("power: %d" % sicong1.power, True, (255, 255, 255))
if event.key == pygame.K_UP:
sicong1.move(sicong1.x, sicong1.y - 10)
power = font.render("power: %d" % sicong1.power, True, (255, 255, 255))
if event.key == pygame.K_DOWN:
sicong1.move(sicong1.x, sicong1.y + 10)
power = font.render("power: %d" % sicong1.power, True, (255, 255, 255))
if event.key == pygame.K_a:
sicong2.move(sicong2.x - 10, sicong2.y)
power = font.render("power: %d" % sicong2.power, True, (255, 255, 255))
if event.key == pygame.K_d:
sicong2.move(sicong2.x + 10, sicong2.y)
power = font.render("power: %d" % sicong2.power, True, (255, 255, 255))
if event.key == pygame.K_w:
sicong2.move(sicong1.x, sicong1.y - 10)
power = font.render("power: %d" % sicong2.power, True, (255, 255, 255))
if event.key == pygame.K_s:
sicong2.move(sicong2.x, sicong2.y + 10)
power = font.render("power: %d" % sicong2.power, True, (255, 255, 255))
# 判断热狗是否被吃掉
for item in hotdog:
# 判断思聪1是否吃掉热狗?
if ((sicong1.x < item.x + y_width) and (sicong1.x + w_width > item.x)
and (sicong1.y < item.y + y_height) and (w_height + sicong1.y > item.y)):
hotdog.remove(item) # 热狗被吃掉
count1 = count1 + 1 # 累加
score1 = font.render('score1: %d' % count1, True, (255, 255, 255))
power1 = font.render('power1: %d' % sicong1.power, True, (255, 255, 255))
elif ((sicong2.x < item.x + y_width) and (sicong2.x + w_width > item.x)
and (sicong2.y < item.y + y_height) and (w_height + sicong2.y > item.y)):
hotdog.remove(item) # 热狗被吃掉
count2 = count2 + 1 # 累加
score2 = font.render('score2: %d' % count2, True, (255, 255, 255))
power2 = font.render('power2: %d' % sicong2.power, True, (255, 255, 255))
# 判断游戏是否结束:当两个思聪的体力值均为0或者面包的数量为0游戏结束
if sicong1.power < 0 or sicong2.power < 0 or len(hotdog)== 0:
if len(hotdog)==0:
if count1 > count2:
print('思聪1获胜')
elif count2 > count1:
print('思聪2获胜')
else:
print('平局')
elif len(hotdog)>0 and sicong1.power < 0:
print('思聪2获胜')
elif len(hotdog) > 0 and sicong2.power < 0:
print('思聪1获胜')
print('Game Over!!!!!!')
sys.exit(0)
question2.按奇偶排序数组
给定一个非负整数数组 A,返回一个数组,在该数组中, A 的所有偶数元素之后跟着所有奇数元素。
你可以返回满足此条件的任何数组作为答案。
示例:
输入:[3,1,2,4]
输出:[2,4,3,1]
输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。
第一种解答:(利用列表排序)
li =[]
try:
while True:
num = int(input('请输入一个非负整数,输入q结束:'))
li.append(num)
except ValueError:
pass
print(sorted(li, key=lambda x: 1 if x % 2 == 0 else 0,reverse=True))
第二种解答:(函数解答)
li = []
new_li = []
even_li = []
odd_li = []
try:
while True:
num = int(input('请输入一个非负整数,输入q结束:'))
li.append(num)
except ValueError:
pass
for i in li:
if i % 2 == 0:
even_li.append(i)
else:
odd_li.append(i)
new_li = even_li + odd_li
print(new_li)
question3.电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
dict = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
def LetterCombinations(nums):
list = []
if len(nums) == 0:
return []
if len(nums) == 1:
for i in dict.get(nums[0]):
list.append(i)
return list
result = LetterCombinations(nums[1:])
for i in dict.get(nums[0]):
for j in result:
list.append(i + j)
return list
nums = input('请输入一个仅包含数字 2-9 的字符串:(不可重复)')
result = LetterCombinations(nums)
print(result)