面向对象编程(三)面向对象高级特性(练习)

介绍了一款单人版思聪吃热狗游戏,玩家可调节思聪位置,移动消耗 10 能量,吃到热狗分数加 1、能量加 20,吃掉所有热狗游戏胜利,王思聪能量耗尽则失败。目前仅能出现两个玩家,玩家二操作未实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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"))

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值