哎呀呀,编程界的小可爱们,是不是觉得编程枯燥无味,想要找点乐子呢?别急,今天我要给大家带来一大波简单游戏代码,让你在轻松愉快的氛围中学习编程,是不是很期待呀?
猜拳小游戏,相信大家都不陌生吧!这个游戏不仅考验你的反应速度,还能锻炼你的编程技巧。下面,我就来给大家展示一个简单的猜拳游戏代码。
```python
import random
游戏开始
num = 1
yinnum = 0
shunum = 0
while num < 3:
if yinnum == 2 or shunum == 2:
break
user = int(input('请出拳 0(石头) 1(剪刀) 2(布)'))
if user > 2:
print('不能出大于2的值')
else:
data = ['石头', '剪刀', '布']
com = random.randint(0, 2)
print(f'您出的是{data[user]},电脑出的是{data[com]}')
if user == com:
print('平局')
continue
elif (user == 0 and com == 1) or (user == 1 and com == 2) or (user == 2 and com == 0):
print('你赢了')
yinnum = 1
else:
print('你输了')
shunum = 1
num = 1
数字炸弹游戏,听起来是不是很刺激?这个游戏需要你根据提示猜测一个数字,猜对了就赢了,猜错了就“爆炸”啦!下面是数字炸弹游戏的代码。
```python
import random
import time
游戏开始
bomb = random.randint(1, 99)
print(f'猜一个1到99之间的数字,你有3次机会。')
for i in range(3):
guess = int(input('请输入你的猜测:'))
if guess == bomb:
print('恭喜你,猜对了!')
break
else:
print(f'猜错了,还剩{2 - i}次机会。')
time.sleep(1)
print(f'提示:数字是{bomb // 10}位数。')
else:
print(f'很遗憾,你的3次机会都用完了。数字是{bomb}。')
飞机大战游戏,是不是让你想起了小时候的街机游戏?下面,我就来给大家展示一个简单的飞机大战游戏代码。
```python
import random
import pygame
初始化pygame
pygame.init()
SCREEN_WIDTH, HEIGHT = 288, 512
info = pygame.display.Info()
width = info.currentw
height = info.currenth
if width > height:
win = pygame.display.set_mode((SCREEN_WIDTH, HEIGHT), pygame.NOFRAME)
else:
win = pygame.display.set_mode((SCREEN_WIDTH, HEIGHT), pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
clock = pygame.time.Clock()
游戏循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
游戏逻辑
...
渲染
win.fill((0, 0, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
贪吃蛇游戏,相信大家都不陌生吧!这个游戏不仅考验你的反应速度,还能锻炼你的编程技巧。下面,我就来给大家展示一个简单的贪吃蛇游戏代码。
```python
import pygame
import random
初始化pygame
pygame.init()
SCREEN_WIDTH, HEIGHT = 640, 480
win = pygame.display.set_mode((SCREEN_WIDTH, HEIGHT))
pygame.display.set_caption('贪吃蛇')
游戏变量
snake_pos = [(100, 50), (90, 50), (80, 50)]
snake_speed = 10
food_pos = (random.randrange(1, (SCREEN_WIDTH//10)) 10, random.randrange(1, (SCREEN_HEIGHT//10)) 10)
food_spawn = True
game_over = False
游戏循环
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
游戏逻辑
...
渲染
win.fill((0, 0, 0))
...
pygame.display.flip()
clock.tick(snake_speed)
pygame.quit()