日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第六周作业--校长吃热狗游戏--奇偶排序--字母组合

發布時間:2023/12/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第六周作业--校长吃热狗游戏--奇偶排序--字母组合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作業一: 思聰愛吃熱狗游戲

import random import timeimport pygame import sys from pygame.locals import * width = 640 height = 480pygame.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() - 5d_width = dogImg.get_width() - 5 d_height = dogImg.get_height() - 5 fpsClock = pygame.time.Clock()class Turtle1:def __init__(self):self.power1 = 200self.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_x1elif new_x1 > width:self.x1 = 0else:self.x1 = new_x1if new_y1 < 0:self.y1 = 0 - new_y1elif new_y1 > height:self.y1 = 0else:self.y1 = new_y1self.power1 -= 10def eat(self):self.power1 += 20if self.power1 > 100:self.power1 = 100 class Turtle2:def __init__(self):self.power2 = 200self.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_x2elif new_x2 > width:self.x2 = 0else:self.x2 = new_x2if new_y2 < 0:self.y2 = 0 - new_y2elif new_y2 > height:self.y2 = 0else:self.y2 = new_y2self.power2 -= 10def eat(self):self.power2 += 20if 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 = widthelse:self.x = new_xtur1 = 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 + 1score1 = 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 + 1score2 = 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 += 1else: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 + jitem.append(result) print(item)

總結

以上是生活随笔為你收集整理的第六周作业--校长吃热狗游戏--奇偶排序--字母组合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。