- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能 - 添加 youxijiemian.py 文件,创建游戏开始界面 - 使用 turtle 和 tkinter 模块分别实现游戏和界面 - 支持选择不同难度的游戏模式
153 lines
3.5 KiB
Plaintext
153 lines
3.5 KiB
Plaintext
'''游戏优化
|
|
|
|
要求如下:
|
|
|
|
1. 绘制‘钻石’角色
|
|
|
|
2. 钻石随背景同步移动,移出舞台后从右侧重新进入
|
|
|
|
3. 摩托碰到钻石后,钻石从右侧重新进入
|
|
|
|
4. 变量 score 记录收集到的钻石数量
|
|
|
|
5. 在窗口内,绘制文字‘收集宝石:×颗’'''
|
|
import pgzrun
|
|
import random
|
|
from pgzhelper import *
|
|
|
|
# 游戏窗口大小
|
|
WIDTH = 1500
|
|
HEIGHT = 800
|
|
|
|
# 创建背景角色
|
|
bg1 = Actor('连续道路', topleft=(0, 0))
|
|
bg2 = Actor('连续道路', topleft=(1500, 0))
|
|
|
|
# 创建障碍物
|
|
obstacles = []
|
|
for _ in range(3): # 障碍物数量
|
|
x = random.randint(WIDTH, WIDTH + 1500) # 障碍物出现在屏幕右侧外
|
|
y = random.randint(100, HEIGHT - 50)
|
|
obstacles.append(Actor('路障', (x, y)))
|
|
|
|
# 创建摩托车
|
|
motor = Actor('摩托1', (200, HEIGHT // 2))
|
|
# 将摩托造型存放在列表内
|
|
motor.images = ['摩托1', '摩托2']
|
|
|
|
# 创建终点线
|
|
line = Actor('终点线', topleft=(20000, 163))
|
|
|
|
# 创建钻石
|
|
zuan = Actor('钻石', topleft=(WIDTH + 100, random.randint(100, HEIGHT - 50)))
|
|
|
|
# 初始化设置
|
|
speed = 15
|
|
flag = 0
|
|
score = 0
|
|
|
|
|
|
def draw():
|
|
# 绘制连续背景
|
|
bg1.draw()
|
|
bg2.draw()
|
|
if flag == 1:
|
|
music.stop()
|
|
screen.blit('摩托成功', (0, 0))
|
|
elif flag == 2:
|
|
music.stop()
|
|
screen.blit('摩托失败', (0, 0))
|
|
else:
|
|
# 绘制障碍物
|
|
for obs in obstacles:
|
|
obs.draw()
|
|
# 绘制摩托
|
|
motor.draw()
|
|
# 绘制终点线
|
|
line.draw()
|
|
# 绘制钻石
|
|
zuan.draw()
|
|
# 绘制距离文字
|
|
x = int(line.x - motor.x)
|
|
screen.draw.text(f'距终点:{x} m', (50, 700), fontname='simkai', fontsize=50)
|
|
# 绘制钻石数量文字
|
|
screen.draw.text(f'收集钻石:{score}颗', (50, 100), fontname='simkai', fontsize=50)
|
|
|
|
|
|
# 背景、终点线移动
|
|
def bg_move():
|
|
bg1.x -= speed
|
|
bg2.x -= speed
|
|
if bg1.right <= 0:
|
|
bg1.left = bg2.right
|
|
if bg2.right <= 0:
|
|
bg2.left = bg1.right
|
|
line.x -= speed
|
|
zuan.x -= speed
|
|
if zuan.x <= 0:
|
|
zuan.x = random.randint(WIDTH, WIDTH + 1500)
|
|
zuan.y = random.randint(100, HEIGHT - 50)
|
|
|
|
# 更新障碍物位置
|
|
|
|
|
|
def obs_move():
|
|
for obs in obstacles:
|
|
obs.x -= speed # 障碍物随背景一起移动
|
|
if obs.x < 0:
|
|
obs.x = random.randint(WIDTH, WIDTH + 1500) # 障碍物继续出现在屏幕外
|
|
obs.y = random.randint(100, HEIGHT - 50)
|
|
|
|
|
|
# 最后冲刺
|
|
def line_collide():
|
|
global flag
|
|
if motor.collide_pixel(line):
|
|
flag = 1
|
|
else:
|
|
motor.x += 15
|
|
|
|
|
|
# 判断是否与障碍物相撞
|
|
def obs_collide():
|
|
global flag
|
|
for obs in obstacles:
|
|
if motor.collide_pixel(obs):
|
|
flag = 2
|
|
|
|
|
|
# 收集钻石
|
|
def zuan_collide():
|
|
global score
|
|
if motor.collide_pixel(zuan):
|
|
score += 1
|
|
zuan.x = random.randint(WIDTH, WIDTH + 1500)
|
|
zuan.y = random.randint(100, HEIGHT - 50)
|
|
|
|
|
|
def update():
|
|
if flag == 0:
|
|
if line.x > WIDTH - 200: # 正常行驶
|
|
bg_move()
|
|
obs_move()
|
|
else: # 最后冲刺
|
|
line_collide()
|
|
# 控制赛车移动,限制不能出界
|
|
if keyboard.up and motor.top > 0:
|
|
motor.y -= 7
|
|
if keyboard.down and motor.bottom < HEIGHT - 50:
|
|
motor.y += 7
|
|
# 是否碰撞障碍物
|
|
obs_collide()
|
|
# 是否碰到钻石
|
|
zuan_collide()
|
|
|
|
|
|
def change_image():
|
|
motor.next_image()
|
|
|
|
|
|
clock.schedule_interval(change_image, 0.2)
|
|
music.play('速度与激情')
|
|
|
|
pgzrun.go() |