python小游戏

Python025

python小游戏,第1张

idea +python3.8+pygame

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n5" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: var(--monospace)font-size: 0.9emdisplay: blockbreak-inside: avoidtext-align: leftwhite-space: normalbackground-image: inheritbackground-position: inheritbackground-size: inheritbackground-repeat: inheritbackground-attachment: inheritbackground-origin: inheritbackground-clip: inheritbackground-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: 400letter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-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)]

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n10" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: var(--monospace)font-size: 0.9emdisplay: blockbreak-inside: avoidtext-align: leftwhite-space: normalbackground-image: inheritbackground-position: inheritbackground-size: inheritbackground-repeat: inheritbackground-attachment: inheritbackground-origin: inheritbackground-clip: inheritbackground-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: 400letter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-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:

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

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

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

单词 游戏 是一种简单的 游戏 ,

计算机从指定单词列表中抽取一个单词,通过算法,把单词的字母顺序打乱,然后输出给玩家猜测。

玩家根据乱序的字母,组合猜测输入正确的单词。计算机确定是否猜测正确。

使用元组或列表构建待猜测的英文单词库列表WORDS,使用random模块的choice函数从单词的元组中随机抽取一个英文单词word。

然后把该英文单词的字母乱序排列

方法:每次随机抽取一个位置的字符放入乱序的jumble字符串中,并从原word中删除该字符

游戏 一开始先显示乱序后的字符串jumble(语义化:混乱),并提示用户输入猜测的结果,如果错误,提示继续输入,直至输入正确。猜对之后,可以询问是否继续 游戏 。 游戏 也可以通过Ctr1+C强制中断运行。

读者也可以扩展程序,例如从文件中读入单词列表,记录 游戏 玩家的得分情况等。

word-guess.py

程序运行结果如下:

word="Telephone"

word=str.lower(word)

#g储存单词

g="* * * * * * * * *"

#把g变成个list

g=g.split()

for i in range(len(word)):

for n in range(len(g)):

print g[n],

print ""

guess=raw_input("Enter a letter: ")

#让用户输入个字母,如果一样就replace原来的

for j in range(len(word)):

if(word[j]==guess):

g[j]=guess

print ""

空格都没了。。。你自己indent...

这个好用不?给分不?