其他
Python中的第一个游戏:在不到30分钟内创建一个阿肯色克隆人
编辑:乐乐 | 来自:Python编程学习圈
大家好,我是Python人工智能技术
TITLE = "Arkanoid clone"
WIDTH = 800
HEIGHT = 500
pgzrun.go()
标题
WIDTH = 800HEIGHT = 500
paddle = Actor("paddleblue.png")
paddle.x = 120
paddle.y = 420
ball = Actor("ballblue.png")
ball.x = 30
ball.y = 300
演员影象
➜ tree.
├── game.py
└── images
├── ballblue.png
└── paddleblue.png
1 directory, 3 filespaddle.x = 120
paddle.y = 420
抽签()
def draw():paddle.draw()
ball.draw()
def update():
passdef draw():
screen.blit("background.png", (0,0))
paddle.draw()
ball.draw()
Background.png影象布利特()
bar = Actor("element_blue_rectangle_glossy.png")bar.x=120
bar.y=100
def draw():
bar.draw()def draw():
bar_x = 120
bar_y = 100
for i in range(8):
bar = Actor("element_blue_rectangle_glossy.png")
bar.x = bar_x
bar.y = bar_y
bar.draw()
bar_x += 70
棒x棒Y
bar_x += 70def draw():screen.blit("background.png", (0,0))
paddle.draw()
ball.draw()
place_blue_bars()
def place_blue_bars():
bar_x = 120
bar_y = 100
for i in range(8):
bar = Actor("element_blue_rectangle_glossy.png")
bar.x = bar_x
bar.y = bar_y
bar.draw()
bar_x += 70
放置蓝条()
Create_red_bar(),Place_bar():
def place_bars(x,y,image):条形表=[]
def place_bars(x,y,image):bar_x = x
bar_y = y
for i in range(8):
bar = Actor(image)
bar.x = bar_x
bar.y = bar_y
bar_x += 70
bars_list.append(bar)
Pgzero.run()
coloured_box_list = ["element_blue_rectangle_glossy.png", "element_green_rectangle_glossy.png","element_red_rectangle_glossy.png"]x = 120
y = 100for coloured_box in coloured_box_list:
place_bars(x, y, coloured_box)
y += 50
Y+=50
coloured_box_list =["element_blue_rectangle_glossy.png",
"element_green_rectangle_glossy.png",
"element_red_rectangle_glossy.png"]
x = 120
y = 100
for coloured_box in coloured_box_list:
place_bars(x, y, coloured_box)
y += 50def draw()
for bar in bars_list:
bar.draw()
更新()
def update():if keyboard.left:
paddle.x = paddle.x - 5
if keyboard.right:
paddle.x = paddle.x + 5
如果键盘左
更新球()更新() .
def update():update_ball()
def update_ball():
ball.x -= 1
ball.y -= 1
Ball.x-=1
Ball.y-=1
ball_x_speed = 1ball_y_speed = 1def update_ball():
global ball_x_speed, ball_y_speed
ball.x -= ball_x_speed
ball.y -= ball_y_speedif (ball.x >= WIDTH) or (ball.x <=0):
ball_x_speed *= -1ball_x_speed *= -1if (ball.y >= HEIGHT) or (ball.y <=0):
ball_y_speed *= -1def update_ball():
global ball_x_speed, ball_y_speed
ball.x -= ball_x_speed
ball.y -= ball_y_speed
if (ball.x >= WIDTH) or (ball.x <=0):
ball_x_speed *= -1
if (ball.y >= HEIGHT) or (ball.y <=0):
ball_y_speed *= -1def update():
update_ball()
for bar in bars_list:
if ball.colliderect(bar):
bars_list.remove(bar)for bar in bars_list:if ball.colliderect(bar):if ball.colliderect(bar):
bars_list.remove(bar)
抽签()
for bar in bars_list:bar.draw()
if ball.colliderect(bar):
bars_list.remove(bar)
ball_y_speed *= -1 # ==> this is the new code# randomly move ball left or right on hit
rand = random.randint(0,1)
if rand:
ball_move_x *= -1if paddle.colliderect(ball):
ball_y_speed *= -1
# randomly move ball left or right on hit
rand = random.randint(0,1)
if rand:
ball_x_speed *= -1
.
你还有什么想要补充的吗?
免责声明:本文内容来源于网络,文章版权归原作者所有,意在传播相关技术知识&行业趋势,供大家学习交流,若涉及作品版权问题,请联系删除或授权事宜。
技术君个人微信
添加技术君个人微信即送一份惊喜大礼包
→ 技术资料共享
→ 技术交流社群
--END--
往日热文:
最全 14 张思维导图,带你构建 Python 编程的核心知识体系!
Python程序员深度学习的“四大名著”:
这四本书着实很不错!我们都知道现在机器学习、深度学习的资料太多了,面对海量资源,往往陷入到“无从下手”的困惑出境。而且并非所有的书籍都是优质资源,浪费大量的时间是得不偿失的。给大家推荐这几本好书并做简单介绍。
获得方式: