python小游戏

这篇博客介绍了两个使用Python和pygame库编写的简单游戏:一是猜数字游戏,用户在指定范围内猜测随机数;二是植物大战僵尸简化版,玩家通过键盘控制豌豆射手射击僵尸,僵尸会从屏幕右侧出现。游戏包含豌豆射手的移动、射击以及僵尸的移动和碰撞检测。
摘要由CSDN通过智能技术生成

python小游戏

工具:

idea +python3.8+pygame

1.猜字小游戏

import random
rang1 = int(input("请设置本局游戏的最小值:"))
rang2 = int(input("请设置本局游戏的最大值:"))
num = random.randint(rang1,rang2)
guess = "guess"
print("数字猜谜游戏!")
i = 0
while guess != num:
    i += 1
    guess = int(input("请输入你猜的数字:"))

    if guess == num:
        print("恭喜,你猜对了!")
    elif guess < num:
        print("你猜的数小了...")
    else:
        print("你猜的数大了...")

print("你总共猜了%d" %i + "次",end = '')
print(",快和你朋友较量一下...")

成果图
在这里插入图片描述

2.植物大战僵尸简化版

import pygame
import random
from pygame.locals import *


# 设置窗口的宽度
WIDTH = 1200
# 设置窗口的高度
HEIGHT = 600


# 定义一个豌豆对象
class Peas:
    # 初始化方法
    def __init__(self):
        # 加载一个豌豆图片
        self.image = pygame.image.load("./res/peas.gif")
        # 获取到豌豆图片的大小和位置
        self.image_rect = self.image.get_rect()
        # 设置豌豆显示的位置
        self.image_rect.top = 285
        self.image_rect.left = 30
        # 判断豌豆是否向上移动
        self.is_move_up = False
        # 判断豌豆是否向下移动
        self.is_move_down = False
        # 判断豌豆是否射炮弹
        self.is_shout = False

    def display(self):
        """显示豌豆的方法"""
        screen.blit(self.image, self.image_rect)

    def move_up(self):
        """豌豆向上移动"""
        if self.image_rect.top > 30:
            # move_ip 可以改变图片的显示对x,y的坐标位置
            self.image_rect.move_ip(0, -10)
        else:
            for z in Zombie.zombie_list:
                if self.image_rect.colliderect(z.image_rect):
                    pygame.quit()
                    exit()

    def move_down(self):
        """豌豆向下移动"""
        if self.image_rect.bottom < HEIGHT - 10:
            self.image_rect.move_ip(0, 10)
        else:
            for z in Zombie.zombie_list:
                if self.image_rect.colliderect(z.image_rect):
                    pygame.quit()
                    exit()

    def shout_bullet(self):
        """发射炮弹方法"""
        # 创建一个炮弹
        bullet = Bullet(self)
        Bullet.bullet_list.append(bullet)

# 定义炮弹对象
class Bullet:
    # 所有的炮弹信息
    bullet_list = []
    # 创建炮弹间隔
    interval = 0

    def __init__(self, peas):
        # 加载一个豌豆图片
        self.image = pygame.image.load("./res/bullet.gif")
        # 获取到豌豆图片的大小和位置
        self.image_rect = self.image.get_rect()
        # 设置豌豆显示的位置
        self.image_rect.top = peas.image_rect.top
        self.image_rect.left = peas.image_rect.right

    def display(self):
        """显示炮弹的方法"""
        screen.blit(self.image, self.image_rect)

    def move(self):
        """移动炮弹"""
        self.image_rect.move_ip(8, 0)
        if self.image_rect.right > WIDTH - 20:
            Bullet.bullet_list.remove(self)
        else:
            for z in Zombie.zombie_list[:]:
                if self.image_rect.colliderect(z.image_rect):
                    Zombie.zombie_list.remove(z)
                    Bullet.bullet_list.remove(self)
                    break


class Zombie:
    # 所有的僵尸信息
    zombie_list = []
    # 创建炮弹间隔
    interval = 0

    def __init__(self):
        # 加载一个豌豆图片
        self.image = pygame.image.load("./res/zombie.gif")
        self.image = pygame.transform.scale(self.image, (70, 70))
        # 获取到豌豆图片的大小和位置
        self.image_rect = self.image.get_rect()
        # 设置豌豆显示的位置
        self.image_rect.top = random.randint(10, HEIGHT-70)
        self.image_rect.left = WIDTH

    def display(self):
        """显示炮弹的方法"""
        screen.blit(self.image, self.image_rect)

    def move(self):
        """移动僵尸"""
        self.image_rect.move_ip(-2, 0)
        if self.image_rect.left < 0:
            Zombie.zombie_list.remove(self)
        else:
            # 撞到豌豆
            if self.image_rect.colliderect(peas.image_rect):
                pygame.quit()
                exit()
            for b in Bullet.bullet_list[:]:
                if self.image_rect.colliderect(b.image_rect):
                    Bullet.bullet_list.remove(b)
                    Zombie.zombie_list.remove(self)
                    break

# 键盘控制事件监听抽取
def key_control():
    # 获取到所有的事件
    for event in pygame.event.get():
        # 如果事件类型是退出, 点击关闭按钮
        if event.type == QUIT:
            # 退出pygame
            pygame.quit()
            # 退出程序
            exit()
        # 事件类型是键盘按下
        if event.type == KEYDOWN:
            # 向下键
            if event.key == K_UP:
                # 设置豌豆的移动方向
                peas.is_move_up = True
                peas.is_move_down = False
            elif event.key == K_DOWN:
                peas.is_move_up = False
                peas.is_move_down = True
            elif event.key == K_SPACE:
                peas.is_shout = True
        # 事件类型是键盘松开
        if event.type == KEYUP:
            if event.key == K_UP:
                peas.is_move_up = False
            elif event.key == K_DOWN:
                peas.is_move_down = False
            elif event.key == K_SPACE:
                peas.is_shout = False


if __name__ == '__main__':
    # 初始化pygame
    pygame.init()
    # 显示界面  窗口的大小
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    # 设置背景图片
    background_image = pygame.image.load("./res/background.png")
    # 调整背景图片大小 设置和窗口一样大小
    scale_background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))
    # 获取到背景图片的位置和大小 (x,y,width,height) 矩形区域
    scale_background_image_rect = scale_background_image.get_rect()
    # 定义一个时钟对象
    clock = pygame.time.Clock()
    # 创建一个豌豆对象
    peas = Peas()
    while True:
        # 设置背景颜色为黑色
        screen.fill((0,0,0))
        # 设窗口设置背景图片
        screen.blit(scale_background_image, scale_background_image_rect)
        peas.display()
        key_control()
        if peas.is_move_up:
            peas.move_up()
        if peas.is_move_down:
            peas.move_down()
        # 每隔20次发送一个炮弹
        Bullet.interval += 1
        if peas.is_shout and Bullet.interval >= 15:
            Bullet.interval = 0
            peas.shout_bullet()

        # 每隔一段时间创建炮弹
        Zombie.interval += 1
        if Zombie.interval >= 15:
            Zombie.interval = 0
            Zombie.zombie_list.append(Zombie())

        for bullet in Bullet.bullet_list:
            # 显示炮弹
            bullet.display()
            # 移动炮弹
            bullet.move()

        for zombie in Zombie.zombie_list:
            zombie.display()
            zombie.move()
        # 重新显示窗口 显示图片
        pygame.display.update()
        clock.tick(60)

成果
在这里插入图片描述
联系拿图哦

  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值