python小游戏

python小游戏

工具:

idea +python3.8+pygame

1.猜字小游戏

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n5" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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(",快和你朋友较量一下...")</pre>

成果图

[图片上传失败...(image-6ef72d-1619168958721)]

2.植物大战僵尸简化版

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n10" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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)</pre>

成果[图片上传失败...(image-983fba-1619168958718)]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容