作业一: 思聪爱吃热狗游戏
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()
dogImg = pygame.image.load("dog.png").convert_alpha()
wscImg1 = pygame.image.load("pri1.png").convert_alpha()
wscImg2 = pygame.image.load("pri2.png").convert_alpha()
pygame.mixer.music.load("game_music.mp3")
pygame.mixer.music.play(loops=0, start=0.0)
# 成绩文字显示
count1 = 0
font1 = pygame.font.SysFont("arial", 40)
score1 = font1.render("score1 %d" % count1, True, (255, 255, 255))
status1 = font1.render("Gaming" , True, (255, 255, 255))
count2 = 0
font2 = pygame.font.SysFont("arial", 40)
score2 = font2.render("score2 %d" % count2, True, (255, 255, 255))
status2 = font2.render("Gaming" , True, (255, 255, 255))
w_width1 = wscImg1.get_width() - 5
w_height1 = wscImg1.get_height() - 5
w_width2 = wscImg2.get_width() - 5
w_height2 = wscImg2.get_height() - 5
d_width = dogImg.get_width() - 5
d_height = dogImg.get_height() - 5
fpsClock = pygame.time.Clock()
class Turtle1:
def __init__(self):
self.power1 = 200
self.x1 = random.randint(0, width - w_width1)
self.y1 = random.randint(0, height - w_height1)
def move(self, new_x1, new_y1):
if new_x1 < 0:
self.x1 = 0 - new_x1
elif new_x1 > width:
self.x1 = 0
else:
self.x1 = new_x1
if new_y1 < 0:
self.y1 = 0 - new_y1
elif new_y1 > height:
self.y1 = 0
else:
self.y1 = new_y1
self.power1 -= 10
def eat(self):
self.power1 += 20
if self.power1 > 100:
self.power1 = 100
class Turtle2:
def __init__(self):
self.power2 = 200
self.x2 = random.randint(0, width - w_width2)
self.y2 = random.randint(0, height - w_height2)
def move(self, new_x2, new_y2):
if new_x2 < 0:
self.x2 = 0 - new_x2
elif new_x2 > width:
self.x2 = 0
else:
self.x2 = new_x2
if new_y2 < 0:
self.y2 = 0 - new_y2
elif new_y2 > height:
self.y2 = 0
else:
self.y2 = new_y2
self.power2 -= 10
def eat(self):
self.power2 += 20
if self.power2 > 100:
self.power2 = 100
class Dog:
def __init__(self):
self.x = random.randint(0, width - d_width)
self.y = random.randint(0, height - d_height)
def move(self):
new_x = self.x + random.choice([-10])
if new_x < 0:
self.x = width
else:
self.x = new_x
tur1 = Turtle1()
tur2 = Turtle2()
dog = []
for item in range(20):
newdog = Dog()
dog.append(newdog)
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:
tur1.move(tur1.x1 - 10, tur1.y1)
if event.key == pygame.K_RIGHT:
tur1.move(tur1.x1 + 10, tur1.y1)
if event.key == pygame.K_UP:
tur1.move(tur1.x1, tur1.y1 - 10)
if event.key == pygame.K_DOWN:
tur1.move(tur1.x1, tur1.y1 + 10)
if event.type == KEYDOWN:
if event.key == pygame.K_a:
tur2.move(tur2.x2 - 10, tur2.y2)
if event.key == pygame.K_d:
tur2.move(tur2.x2 + 10, tur2.y2)
if event.key == pygame.K_w:
tur2.move(tur2.x2, tur2.y2 - 10)
if event.key == pygame.K_s:
tur2.move(tur2.x2, tur2.y2 + 10)
screen.blit(background, (0, 0))
screen.blit(background, (0, 0))
screen.blit(score1, (200, 10))
screen.blit(status1, (0, 10))
screen.blit(score2, (400, 10))
screen.blit(status2, (0,10))
for item in dog:
screen.blit(dogImg, (item.x, item.y))
item.move()
screen.blit(wscImg1, (tur1.x1, tur1.y1))
screen.blit(wscImg2, (tur2.x2, tur2.y2))
# 判断游戏是否结束
if tur1.power1 < 0 and tur2.power2 < 0:
print("Game Over")
status1 = font1.render("Game Over", True, (255, 255, 255))
pygame.display.update()
time.sleep(1)
sys.exit(0)
elif len(dog) == 0:
status1 = font1.render("Game Over", True, (255, 255, 255))
pygame.display.update()
sys.exit(0)
for item in dog:
if ((tur1.x1 < item.x + d_width) and (tur1.x1 + w_width1 > item.x)
and (tur1.y1 < item.y + d_height) and (w_height1 + tur1.y1 > item.y)) :
dog.remove(item)
count1 = count1 + 1
score1 = font1.render("score1 %d" % count1, True, (255, 255, 255))
for item in dog:
if ((tur2.x2 < item.x + d_width) and (tur2.x2 + w_width2 > item.x) and (tur2.y2 < item.y + d_height) and (w_height2 + tur2.y2 > item.y)) :
dog.remove(item)
count2 = count2 + 1
score2 = font2.render("score2 %d" % count2, True, (255, 255, 255))
pygame.display.update()
fpsClock.tick(10)
作业二: 按奇偶排序数组
#方法一
list_input = [3,1,2,4]
even = []
odd = []
for i in list_input:
if i % 2 == 0:
even.append(i)
else:
odd.append(i)
list_input = even + odd
print(list_input)
#方法二
list_input = [3,1,2,4]
index = 0
for i in range(len(list_input)):
if list_input[index] % 2 == 0:
index += 1
else:
list_input.append(list_input.pop(index))
print(list_input)
作业三: 电话号码的字母组合
phone = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
"6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
num1,num2 = input('请输入两个数字:')
item = []
for i in phone[num1]:
for j in phone[num2]:
result = i + j
item.append(result)
print(item)