coursera-miniproject Pang任务总结
球是纯白的,板子也是纯白的。球已经画出来了,但是加了几个全局变量(球的位置,球的初速度)
# Implementation of classic arcade game Pong import simplegui
import random # initialize globals - pos and vel encode vertical info for paddles
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20
PAD_WIDTH = 8
PAD_HEIGHT = 80
HALF_PAD_WIDTH = PAD_WIDTH / 2
HALF_PAD_HEIGHT = PAD_HEIGHT / 2
LEFT = False
RIGHT = True
ball_pos = [WIDTH / 2, HEIGHT / 2]#需要么?
vel = [-3, -1]#需要么?
paddle1_pos = 200#建议是数字不是list
paddle2_pos = 200
paddle1_vel = 0#建议是数字不是list
paddle2_vel = 0
soccera = 0
soccerb = 0 # initialize ball_pos and ball_vel for new bal in middle of table
# if direction is RIGHT, the ball's velocity is upper right, else upper left
def spawn_ball(direction):
global ball_pos, vel # these are vectors stored as lists
#产生球的函数,功能是给一个初始位置和初始速度
if direction == RIGHT:#True和FALSE是啥?
vel[0] = random.randrange(2, 4)#速度有问题?太快了?
vel[1] = - random.randrange(1, 3)
ball_pos = [300, 200] if direction == LEFT:
vel[0] = - random.randrange(2, 4)
vel[1] = - random.randrange(1, 3)
ball_pos = [300, 200] # define event handlers
def new_game():
global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel # these are numbers
global soccera, soccerb # these are ints
soccera = 0
soccerb = 0
paddle1_pos = 200
paddle2_pos = 200
paddle1_vel = 0
paddle2_vel = 0
spawn_ball(RIGHT) def draw(canvas):
global score1, score2, paddle1_pos, paddle2_pos, ball_pos, vel, soccera, soccerb
# draw mid line and gutters
canvas.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "White")
canvas.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "White")
canvas.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "White")
# update ball
ball_pos[0] += vel[0]
ball_pos[1] += vel[1]
#撞击上下墙壁反弹
if ball_pos[1] <= BALL_RADIUS:
vel[1] = - vel[1]
elif ball_pos[1] >= HEIGHT - 1 - BALL_RADIUS:
vel[1] = - vel[1]
#球撞击gutter后要复位游戏,调用spawn_ball
elif ball_pos[0] <= BALL_RADIUS + PAD_WIDTH and paddle1_pos - 40 <= ball_pos[1] <= paddle1_pos + 40:
vel[0] = - vel[0] * 1.1
vel[1] = vel[1] * 1.1
elif ball_pos[0] >= WIDTH - 1 - BALL_RADIUS - PAD_WIDTH and paddle2_pos - 40 <= ball_pos[1] <= paddle2_pos + 40:
vel[0] = - vel[0] * 1.1
vel[1] = vel[1] * 1.1
elif ball_pos[0] <= BALL_RADIUS + PAD_WIDTH and ball_pos[1] < paddle1_pos - 40:
spawn_ball(RIGHT)
soccerb += 1
elif ball_pos[0] <= BALL_RADIUS + PAD_WIDTH and ball_pos[1] > paddle1_pos + 40:
spawn_ball(RIGHT)
soccerb += 1
elif ball_pos[0] >= WIDTH - 1 - BALL_RADIUS - PAD_WIDTH and ball_pos[1] < paddle2_pos - 40:
spawn_ball(LEFT)
soccera += 1
elif ball_pos[0] >= WIDTH - 1 - BALL_RADIUS - PAD_WIDTH and ball_pos[1] > paddle2_pos + 40:
spawn_ball(LEFT)
soccera += 1
# draw ball
canvas.draw_circle(ball_pos, BALL_RADIUS, 2, "White", "White")
# update paddle's vertical position, keep paddle on the screen
paddle1_pos += paddle1_vel
if paddle1_pos <= 40:
paddle1_pos = 40
elif paddle1_pos >= 360:
paddle1_pos = 360 paddle2_pos += paddle2_vel
if paddle2_pos <= 40:
paddle2_pos = 40
elif paddle2_pos >= 360:
paddle2_pos = 360
# draw paddles
canvas.draw_line([0, paddle1_pos], [8, paddle1_pos], 80, "White")
canvas.draw_line([592,paddle2_pos], [600, paddle2_pos], 80, "White")
# draw scores
canvas.draw_text(str(soccera), [140, 100], 50, "White")
canvas.draw_text(str(soccerb), [450, 100], 50, "White")
def keydown(key):
global paddle1_vel, paddle2_vel
if key == simplegui.KEY_MAP["w"]:
paddle1_vel -= 4
elif key == simplegui.KEY_MAP["s"]:
paddle1_vel += 4
elif key == simplegui.KEY_MAP["up"]:
paddle2_vel -= 4
elif key == simplegui.KEY_MAP["down"]:
paddle2_vel += 4 def keyup(key):
global paddle1_vel, paddle2_vel
if key == simplegui.KEY_MAP["w"]:
paddle1_vel = 0
elif key == simplegui.KEY_MAP["s"]:
paddle1_vel = 0
elif key == simplegui.KEY_MAP["up"]:
paddle2_vel = 0
elif key == simplegui.KEY_MAP["down"]:
paddle2_vel = 0
# create frame
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
frame.add_button("Restart", new_game,100) # start frame
new_game()
frame.start()
多年以后,我会看到我的挣扎
coursera-miniproject Pang任务总结的更多相关文章
- Coursera,Udacity,Edx 课程列表(更新ing)
Coursera,Udacity,Edx 课程列表(更新ing) Coursera有很多特别好的课程,平时没有机会听到国外大牛的课程,通过Coursera算是可以弥补一下吧,国外的课程普遍比国内的老师 ...
- Coursera上一个不错的Java课
地址:https://www.coursera.org/learn/java-chengxu-sheji/home/welcome 复习天昏地暗,看点视频调剂一下.发现这个讲的还是很不错的.北大毕竟比 ...
- Coursera系列-R Programming第二周
博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html --- 好久没发博客 且容我大吼一句 终于做完这周R Progra ...
- Coursera系列-R Programming第三周-词法作用域
完成R Programming第三周 这周作业有点绕,更多地是通过一个缓存逆矩阵的案例,向我们示范[词法作用域 Lexical Scopping]的功效.但是作业里给出的函数有点绕口,花费了我们蛮多心 ...
- Coursera Robotics系列课心得
Robotics Perception Professor Kostas and Jianbo Shi week 1: camera model 凸透镜成像原理:凸透镜焦点与焦距是固定的,这是物理性质 ...
- coursera机器学习-聚类,降维,主成分分析
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
- coursera机器学习-支持向量机SVM
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
- coursera机器学习笔记-建议,系统设计
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
- coursera机器学习笔记-神经网络,学习篇
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
- coursera机器学习笔记-神经网络,初识篇
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
随机推荐
- 程序员的恶性循环:加班->没空学习->老是写同等水平代码->无法提升代码质量->老是出BUG->老是需要修改->加班->...
程序员的恶性循环:加班->没空学习->老是写同等水平代码->无法提升代码质量->老是出BUG->老是需要修改->加班->...
- aac格式解析
AAC格式有以下两种: ADIF:Audio Data Interchange Format 音频数据交换格式.这种格式的特征是可以确定的找到这个音频数据的开始,不需进行在音频数据流中间开始的解码,即 ...
- Spring的profile属性
使用示例 //注解方式 public class DataSourceConfig { @Bean @Profile("prod") public DataSource dataS ...
- Remainder
Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- BZOJ3673 : 可持久化并查集
题目没有强制在线! 考虑离线做法. 把操作建立成一棵树的结构,然后按照欧拉序遍历,每次转移要么是一次合并操作,要么是一次撤销合并操作,可以看成是分离操作. 用LCT维护集合,合并就是加边,分离就是删边 ...
- 一个spring jdbc实例
一.使用示例 (1)springJdbcContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- 【BZOJ】1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 【BZOJ】1041: [HAOI2008]圆上的整点(几何)
http://www.lydsy.com:808/JudgeOnline/problem.php?id=1041 所谓的神题,我不会,直接题解..看了半天看懂题解了.详见hzwer博客 这题呢,我只能 ...
- HDU 4614 Vases and Flowers(线段树+二分)
题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...
- BZOJ4143 [AMPPZ2014]The Lawyer
Description Byteasar要制订m天的会议计划,一共有n场会议,第i场会议开始于第d[i]天的第a[i]秒,结束于第d[i]天的第b[i]秒. 对于每一天,请找出这一天的两场会议i,j, ...