查看原文
其他

Python中的第一个游戏:在不到30分钟内创建一个阿肯色克隆人

点击上方 "Python人工智能技术关注,星标或者置顶
22点24分准时推送,第一时间送达
后台回复“大礼包”,送你特别福利

编辑:乐乐 | 来自:Python编程学习圈

Pythn人工智能技术(ID:coder_experience)第670期推文

上一篇:30 个Python代码实现的常用功能,精心整理版


正文


大家好,我是Python人工智能技术



pip install pgzero

import pgzrun

TITLE = "Arkanoid clone"
WIDTH = 800
HEIGHT = 500

pgzrun.go()

标题

WIDTH = 800
HEIGHT = 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 files

paddle.x = 120
paddle.y = 420

抽签()

def draw():
paddle.draw()
ball.draw()

def update():
pass

def 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 += 70

def 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 = 100

for 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 += 50

def 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 = 1
ball_y_speed = 1

def 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

ball_x_speed *= -1

if (ball.y >= HEIGHT) or (ball.y <=0):
ball_y_speed *= -1

def 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 *= -1

def 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()

for bar in bars_list:
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 *= -1

if 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--


往日热文:

3W 字!Python 操作 Excel 报表自动化指南!

抖音的服务器究竟有多大?

如何从 100 亿 URL 中找出相同的 URL?

工作群里常见表情的真正含义……

最全 14 张思维导图,带你构建 Python 编程的核心知识体系!


Python程序员深度学习的“四大名著”:



这四本书着实很不错!我们都知道现在机器学习、深度学习的资料太多了,面对海量资源,往往陷入到“无从下手”的困惑出境。而且并非所有的书籍都是优质资源,浪费大量的时间是得不偿失的。给大家推荐这几本好书并做简单介绍。


获得方式:

1.扫码关注本公众号
2.后台回复关键词:名著

▲长按扫描关注,回复名著即可获取

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存