python手机版做小游戏代码大全-python简单小游戏代码 怎么用Python制作简单小游戏...

1、Python猜拳小游戏代码:

2、import random #导入随机模块

3、

4、num = 1

5、yin_num = 0

6、shu_num = 0

7、while num <= 3:

8、 if shu_num == 2 or yin_num == 2:

9、 break

10、 user = int(input('请出拳 0(石头) 1(剪刀) 2(布)'))

11、 if user > 2:

12、 print('不能出大于2的值')

13、 else:

14、 data = ['石头', '剪刀', '布']

15、 com = random.randint(0, 2)

16、 print(您出的是{},电脑出的是{}.format(data[user], data[com]))

17、 if user == com:

18、 print('平局')

19、 continue

20、 elif (user == 0 and com == 1) or (user == 1 and com == 2) or (user == 2 and com == 0):

21、 print('你赢了')

22、 yin_num += 1

23、 else:

24、 print('你输了')

25、 shu_num += 1

26、 num += 1

27、Python数字炸弹小游戏代码:

28、import random

29、import time

30、

31、bomb = random.randint(1, 99)

32、print(bomb)

33、start = 0

34、end = 99

35、while 1 == 1:

36、

37、 people = int(input('请输入{}到{}之间的数:'.format(start, end)))

38、 if people > bomb:

39、 print('大了')

40、 end = people

41、 elif people < bomb:

42、 print('小了')

43、 start = people

44、 else:

45、 print('BOOM!!!')

46、 break

47、 print('等待电脑了输入{}到{}之间的数:'.format(start, end))

48、 time.sleep(1)

49、 com = random.randint(start + 1, end - 1)

50、 print('电脑输入:{}'.format(com))

51、 if com > bomb:

52、 print('大了')

53、 end = com

54、 elif com < bomb:

55、 print('小了')

56、 start = com

57、 else:

58、 print('BOOM!!!')

59、 break

好的,我可以给你介绍一个基于pygame的Python小游戏——Flappy Bird。 Flappy Bird是一款非常经典的手机游戏,这里我们将使用pygame模块重新创建一个简化的Flappy Bird游戏。 首先,你需要安装pygame模块。在控制台中运行以下命令: ``` pip install pygame ``` 接下来,我们需要导入pygame和其他必要的模块: ```python import pygame import random import sys ``` 然后,我们需要定义一些常量,包括窗口大小、背景颜色、地面高度等: ```python WIDTH = 288 HEIGHT = 512 FPS = 60 GROUND_HEIGHT = 112 BACKGROUND_COLOR = (153, 204, 255) ``` 接下来,我们需要定义游戏中的各种元素,包括鸟、管道、地面等: ```python class Bird(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load("bird.png").convert_alpha() self.rect = self.image.get_rect(center=(50, HEIGHT // 2)) self.vel = 0 self.gravity = 0.5 def update(self): self.vel += self.gravity self.rect.y += self.vel class Pipe(pygame.sprite.Sprite): def __init__(self, height): super().__init__() self.image = pygame.image.load("pipe.png").convert_alpha() self.image = pygame.transform.scale(self.image, (52, height)) self.rect = self.image.get_rect(midtop=(WIDTH, 0)) self.vel = 2 def update(self): self.rect.x -= self.vel class Ground(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load("ground.png").convert_alpha() self.rect = self.image.get_rect(bottomleft=(0, HEIGHT)) self.vel = 2 def update(self): self.rect.x -= self.vel ``` 我们还需要定义一些辅助函数,比如创建管道、检测碰撞等: ```python def create_pipe(): pipe_height = random.randint(100, HEIGHT - GROUND_HEIGHT - 200) top_pipe = Pipe(pipe_height) bottom_pipe = Pipe(HEIGHT - GROUND_HEIGHT - pipe_height - 80) bottom_pipe.rect.y = top_pipe.rect.bottom + 80 return top_pipe, bottom_pipe def check_collision(bird, pipes): if bird.rect.bottom >= HEIGHT - GROUND_HEIGHT: return True for pipe in pipes: if bird.rect.colliderect(pipe.rect): return True return False ``` 最后,我们可以开始编写主游戏循环: ```python pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() bird = Bird() pipes = pygame.sprite.Group() ground = Ground() score = 0 font = pygame.font.Font(None, 36) while True: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bird.vel = -8 if len(pipes) < 3: pipe_pair = create_pipe() pipes.add(pipe_pair) bird.update() pipes.update() ground.update() for pipe in pipes: if pipe.rect.right < 0: pipes.remove(pipe) score += 1 if check_collision(bird, pipes): pygame.quit() sys.exit() screen.fill(BACKGROUND_COLOR) pipes.draw(screen) screen.blit(ground.image, ground.rect) screen.blit(bird.image, bird.rect) score_text = font.render("Score: " + str(score), True, (0, 0, 0)) screen.blit(score_text, (10, 10)) pygame.display.update() ``` 这就是一个简化的Flappy Bird游戏,你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值