Python中的第一个游戏:在不到30分钟内创建一个阿肯色克隆人
安装游戏零:
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
更新VS绘图功能
画桨和球
def draw():
paddle.draw()
ball.draw()
def update():
pass
def draw():
screen.blit("background.png", (0,0))
paddle.draw()
ball.draw()
布置栏杆
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
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
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)
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
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
关于PYGERO零坐标系的一点看法
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
在PyameZero中实现碰撞检测
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
下一步
文章转载:Python编程学习圈
(版权归原作者所有,侵删)
点击下方“阅读原文”查看更多