1.一款单人版的思聪吃热狗游戏,你可以自己调节思聪的位置, 移动时会消耗能量
10, 游戏中吃到热狗分数加 1, 能量加 20,最后的目标就是称霸世界咯, 吃掉
所有的热狗即游戏胜利。王思聪能量消耗完毕即游戏失败。
import random
import time
import pygame
import sys
from pygame.locals import * #导入一些常用函数
width = 640
height = 480
pygame.init() #pygame初始化
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption('思聪爱吃热狗小游戏') #定义窗口的标题
background = pygame.image.load('./img/bg.jpg').convert()
hamburger = pygame.image.load('./img/image.OK7G3Z.png').convert_alpha()
wscImg = pygame.image.load('./img/image.Y4HF3Z.png').convert_alpha()
#成绩文字显示
count = 0
font = pygame.font.SysFont('arial', 40)
score = font.render('score %d' %count, True, (255,255,255))
#显示游戏状态
status = font.render('Gaming',True,(255,255,255))
h_width = hamburger.get_width() -5
h_height = hamburger.get_height() - 5
w_width = wscImg.get_width() - 5
w_height = wscImg.get_height() -5
fpsClock = pygame.time.Clock() #创建一个新的Clock对象,可以用来跟踪总共的时间
#创建类
class Wsc(object):
def __init__(self):
self.power = 200 #体力值为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 = 0 - new_x
elif new_x > width:
self.x = 0
else:
self.x = new_x
if new_y < 0:
self.y = 0 - new_y
elif new_y > height:
self.y = 0
else:
self.y = new_y
self.power -= 10
def eat(self):
self.power += 20
if self.power > 200:
self.power = 200
#创建类
class Wsc2(object):
def __init__(self):
self.power = 200 #体力值为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 = 0 - new_x
elif new_x > width:
self.x = 0
else:
self.x = new_x
if new_y < 0:
self.y = 0 - new_y
elif new_y > height:
self.y = 0
else:
self.y = new_y
self.power -= 10
def eat(self):
self.power += 20
if self.power > 200:
self.power = 200
class Hamburger(object):
def __init__(self):
self.x = random.randint(0, width - h_width)
self.y = random.randint(0, height - h_height)
def move(self):
new_x = self.x + random.choice([-10])
if new_x < 0:
self.x = width
else:
self.x = new_x
wsc = Wsc()
wsc2 = Wsc2()
hum = [Hamburger() for i in range(10, 41)]
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:
wsc.move(wsc.x - 10, wsc.y)
if event.key == pygame.K_RIGHT:
wsc.move(wsc.x + 10, wsc.y)
if event.key == pygame.K_UP:
wsc.move(wsc.x, wsc.y - 10)
if event.key == pygame.K_DOWN:
wsc.move(wsc.x, wsc.y + 10)
elif event.type == KEYDOWN:
if event.key == pygame.K_D:
wsc2.move(wsc2.x - 10, wsc2.y)
if event.key == pygame.K_A:
wsc2.move(wsc2.x + 10, wsc2.y)
if event.key == pygame.K_W:
wsc2.move(wsc2.x, wsc2.y - 10)
if event.key == pygame.K_S:
wsc2.move(wsc2.x, wsc2.y + 10)
screen.blit(background, (0, 0))
screen.blit(score, (500, 20))
screen.blit(status, (0, 20))
for item in hum:
screen.blit(hamburger, (item.x, item.y))
item.move()
screen.blit(wscImg, (wsc.x, wsc.y))
screen.blit(wscImg, (wsc2.x, wsc2.y))
if wsc.power < 0:
print('Game Over')
status = font.render('Game Over,wsc power is 0', True, (255, 255, 255))
pygame.display.update()
time.sleep(2)
sys.exit(0)
elif len(hum) == 0:
status = font.render('Game Over, no humburgers', True, (255, 255, 255))
pygame.display.update()
time.sleep(2)
sys.exit(0)
for item in hum:
if ((wsc.x < item.x + h_width) and (wsc.x + w_width > item.x )and (wsc.y < item.y + h_height) and (w_height + wsc.y > item.y)):
hum.remove(item)
count += 1
score = font.render('score %d' %count, True, (255, 255, 255))
pygame.display.update()
fpsClock.tick(10)
目前只实现了可以出现两个玩家,但是玩家二的操作还没有实现。
2.
方法一:
A = [3, 1, 2, 4]
odd_nums = [num for num in A if num%2==0]
even_nums = [num for num in A if num%2!=0]
result = odd_nums + even_nums
print(result)
方法二:
def sortnums(A:list) ->list:
new_list = []
for item in A:
if item % 2 == 0:
new_list.append(item)
for item in A:
if item % 2 != 0:
new_list.append(item)
return new_list
print(sortnums([3, 1, 2, 4]))
3.
def phoneLetter(digits):
if not digits:
return []
keyboard = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
result1 = []
if len(digits) == 0:
return []
if len(digits) == 1:
return keyboard[digits]
result = phoneLetter(digits[1:])
for item in result:
for i in keyboard[digits[0]]:
result1.append(item + i)
return result1
print(phoneLetter("23"))