[course]08 游戏模块02-2
点击游戏,和游戏帮助
游戏的多个模块,可以使用ModelApp来启动,然后里面的每个模块会继承Model。
一般多个模块的部分,一般会在入口的地方注册每一个model
针对Model模式,我们使用
mode.app.setActiveMode来激活相应的model,只有被激活的模块才能被显示,以及调用其中相应的方法。这样我们就可以在游戏中增加不同的模块了。碰撞检测,如何检测到鼠标点击的地方和其中圆圈的地方重合。当mousePressed事件触发时,判断二者的相关关系
from game_graphics import *
from tkinter import *
import random
class SplashScreenMode(Mode):
def redrawAll(mode, canvas):
font = 'Arial 26 bold'
canvas.create_text(mode.width/2, 150, text='This demos a ModalApp!', font=font)
canvas.create_text(mode.width/2, 200, text='This is a modal splash screen!', font=font)
canvas.create_text(mode.width/2, 250, text='Press any key for the game!', font=font)
def keyPressed(mode, event):
mode.app.setActiveMode(mode.app.gameMode)
class GameMode(Mode):
def appStarted(mode):
mode.score = 0
mode.randomizeDot()
def randomizeDot(mode):
mode.x = random.randint(20, mode.width-20)
mode.y = random.randint(20, mode.height-20)
mode.r = random.randint(10, 20)
mode.color = random.choice(['red', 'orange', 'yellow', 'green', 'blue'])
mode.dx = random.choice([+1,-1])*random.randint(3,6)
mode.dy = random.choice([+1,-1])*random.randint(3,6)
def moveDot(mode):
mode.x += mode.dx
if (mode.x < 0) or (mode.x > mode.width): mode.dx = -mode.dx
mode.y += mode.dy
if (mode.y < 0) or (mode.y > mode.height): mode.dy = -mode.dy
def timerFired(mode):
mode.moveDot()
def mousePressed(mode, event):
d = ((mode.x - event.x)**2 + (mode.y - event.y)**2)**0.5
if (d <= mode.r):
mode.score += 1
mode.randomizeDot()
elif (mode.score > 0):
mode.score -= 1
def keyPressed(mode, event):
if (event.key == 'h'):
mode.app.setActiveMode(mode.app.helpMode)
def redrawAll(mode, canvas):
font = 'Arial 26 bold'
canvas.create_text(mode.width/2, 20, text=f'Score: {mode.score}', font=font)
canvas.create_text(mode.width/2, 50, text='Click on the dot!', font=font)
canvas.create_text(mode.width/2, 80, text='Press h for help screen!', font=font)
canvas.create_oval(mode.x-mode.r, mode.y-mode.r, mode.x+mode.r, mode.y+mode.r,
fill=mode.color)
class HelpMode(Mode):
def redrawAll(mode, canvas):
font = 'Arial 26 bold'
canvas.create_text(mode.width/2, 150, text='This is the help screen!', font=font)
canvas.create_text(mode.width/2, 250, text='(Insert helpful message here)', font=font)
canvas.create_text(mode.width/2, 350, text='Press any key to return to the game!', font=font)
def keyPressed(mode, event):
mode.app.setActiveMode(mode.app.gameMode)
class MyModalApp(ModalApp):
def appStarted(app):
app.splashScreenMode = SplashScreenMode()
app.gameMode = GameMode()
app.helpMode = HelpMode()
app.setActiveMode(app.splashScreenMode)
app.timerDelay = 50
app = MyModalApp(width=500, height=500)画布的延展
感知上的背景变化
如何批量的创建随机点
中心点不变我们通过参照物的整体的位置变化,让玩家感知上觉得是中心点在变化
感知上的物体变化
中央player移动,到边界时移动背景内容,感受背景变化
触碰游戏
getWallHit 碰撞检测
积分
Last updated
Was this helpful?