[course]07 游戏模块01-2

添加和删除图形

使用List添加删除图形 ,不使用class的形式

from game_graphics import *
from tkinter import *

def appStarted(app):
    app.circleCenters = [ ]

def mousePressed(app, event):
    newCircleCenter = (event.x, event.y)
    app.circleCenters.append(newCircleCenter)

def keyPressed(app, event):
    if (event.key == 'd'):
        if (len(app.circleCenters) > 0):
            app.circleCenters.pop(0)
        else:
            print('No more circles to delete!')

def redrawAll(app, canvas):
    # draw the circles
    for circleCenter in app.circleCenters:
        (cx, cy) = circleCenter
        r = 20
        canvas.create_oval(cx-r, cy-r, cx+r, cy+r, fill='cyan')
    # draw the text
    canvas.create_text(app.width/2, 20,
                       text='Example: Adding and Deleting Shapes')
    canvas.create_text(app.width/2, 40,
                       text='Mouse clicks create circles')
    canvas.create_text(app.width/2, 60,
                       text='Pressing "d" deletes circles')

runApp(width=400, height=400)

使用class添加

作业:使用A按钮,来随机添加一个正方形,使用D可以删除

通过鼠标选择相应的grid

modelToView 和 viewToModel

自己运动的方块

贪食蛇

作业: 1. 增加游戏难度选择 1. normal:保持原本 2. hard:地图扩大1倍,游戏速度增加1倍 3. hell:地图扩大2倍,游戏速度增加2倍 2. 游戏结束后给出分数 3. 将最高分数进行记录,结束后显示 1. 只在运行时保存,运行结束后清零 2. 一直保存 4. 一次多加几个Food

Last updated

Was this helpful?