100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python游戏编程入门书籍推荐-Python游戏编程入门3

python游戏编程入门书籍推荐-Python游戏编程入门3

时间:2022-09-19 23:18:36

相关推荐

python游戏编程入门书籍推荐-Python游戏编程入门3

用户输入:Bomb Catcher游戏

本章介绍使用键盘和鼠标获得用户输入。包括如下主题:

学习pygame事件

学习实时循环

学习键盘和鼠标事件

学习轮询键盘和鼠标的状态

编写Bomb Catcher游戏

1本章所涉及pygame事件

QUIT

KEYDOWN

KEYUP

MOUSEMOTION

MOUSEBUTTONUP

MOUSEBUTTONDOWN

1.1实时事件循环

while True:

for event in pygame.event.get():

if event.type==QUIT:

sys.exit()

在循环中不断地对事件队列中的特定事件进行相应的处理。

1.2键盘事件

KEYUP KEYDOWN

1.3鼠标事件

MOUSEBUTTONDOWN MOUSEBUTTONUP MOUSEMOTION

具体属性见《pygame几个重要模块》

2设备轮询

pygame中的事件系统并非我们可以用来检测用户输入的唯一方法。我们可以轮询输入设备,看看用户是否与我们的

程序交互。

2.1轮询键盘

在pygame中,使用pygame.key.get_pressed()来轮询键盘接口。该方法返回布尔值的一个列表,对应于键盘

上的按键,每个键一个标志。使用相同的键常量值来索引布尔值数组。一次轮询所有的键的好处是不必遍历事件

系统就可以检测多个键的按下。

以下示例检测escape键。

keys=pygame.key.get_pressed()

if keys[K_ESCAPE]:

sys.exit()

2.2轮询鼠标

pygame.mouse.get_pos()

pygame.mouse.get_rel()

pygame.mouse.get_pressed()

具体使用参考《pygame几个重要模块》

import sys,pygame

from pygame.locals import *

pygame.init()

def print_text(font,x,y,text,color=(255,255,255)):

imgtext=font.render(text,True,color)

screen.blit(imgtext,(x,y))

#main program begins

screen=pygame.display.set_mode((600,500))

pygame.display.set_caption("Mouse Demo")

font1=pygame.font.Font(None,24)

white=255,255,255

mouse_x=mouse_y=0

move_x=move_y=0

mouse_down=mouse_up=0

mouse_down_x=mouse_down_y=0

mouse_up_x=mouse_up_y=0

#repeating loop

while True:

for event in pygame.event.get():

if event.type==QUIT:

sys.exit()

elif event.type==MOUSEMOTION:

mouse_x,mouse_y=event.pos

move_x,move_y=event.rel

elif event.type==MOUSEBUTTONDOWN:

mouse_down=event.button

mouse_down_x,mouse_down_y=event.pos

elif event.type==MOUSEBUTTONUP:

mouse_up=event.button

mouse_up_x,mouse_up_y=event.pos

keys=pygame.key.get_pressed()

if keys[K_ESCAPE]:

sys.exit()

screen.fill((0,100,0))

print_text(font1,0,0,"Mouse Events")

print_text(font1,0,20,"Mouse position: "+str(mouse_x)+","+str(mouse_y))

print_text(font1,0,40,"Mouse relative: "+str(move_x)+","+str(move_y))

print_text(font1,0,60,"Mouse button down: "+str(mouse_down)+" at "+str(mouse_down_x)+","+str(mouse_down_y))

print_text(font1,0,80,"Mouse button up: "+str(mouse_up)+" at "+str(mouse_up_x)+","+str(mouse_up_y))

print_text(font1,0,160,"Mouse Polling")

x,y=pygame.mouse.get_pos()

print_text(font1,0,180,"Mouse Position: "+str(x)+str(y))

b1,b2,b3=pygame.mouse.get_pressed()

print_text(font1,0,200,"Mouse buttons: "+str(b1)+","+str(b2)+","+str(b3))

pygame.display.update()

3游戏简介

bomb Catcher游戏综合了鼠标输入、一些基本图形绘制和少量冲突检测逻辑。

炸弹是不断重复地从屏幕顶端落下的黄色圆圈。

当炸弹到达屏幕底部的时候,玩家未接住炸弹就会丢掉一条生命。

当炸弹撞击到挡板,算作玩家接住炸弹,另一个炸弹还会继续落下。

import sys,random,time,pygame

from pygame.locals import *

pygame.init()

def print_text(font,x,y,text,color=(255,255,255)):

imgtext=font.render(text,True,color)

screen.blit(imgtext,(x,y))

screen=pygame.display.set_mode((600,500))

pygame.display.set_caption("Bomb Catching Game")

font1=pygame.font.Font(None,24)

pygame.mouse.set_visible(False)

white=255,255,255

red=220,50,50

yellow=230,230,50

black=0,0,0

lives=3

score=0

game_over=True

mouse_x=mouse_y=0

pos_x=300

pos_y=460

bomb_x=random.randint(0,500)

bomb_y=-50

vel_y=3

#repeating loop

while True:

for event in pygame.event.get():

if event.type==QUIT:

sys.exit()

elif event.type==MOUSEMOTION:

mouse_x,mouse_y=event.pos

move_x,move_y=event.rel

elif event.type==MOUSEBUTTONUP:

if game_over:

game_over=False

lives=3

score=0

keys=pygame.key.get_pressed()

if keys[K_ESCAPE]:

sys.exit()

screen.fill((0,0,100))

if game_over:

print_text(font1,100,200,"")

else:

#move the bomb

bomb_y+=vel_y

#has player missed the bomb?

if bomb_y>500:

bomb_x=random.randint(0,500)

bomb_y=-50

lives-=1

if lives==0:

game_over=True

#see if player has caught the bomb

elif bomb_y>pos_y:

if bomb_x>pos_x and bomb_x

score+=10

bomb_x=random.randint(0,500)

bomb_y=-50

#draw the bomb

pygame.draw.circle(screen,black,(bomb_x-4,int(bomb_y)-4),30,0)

pygame.draw.circle(screen,yellow,(bomb_x,int(bomb_y)),30,0)

#set basket position

pos_x=mouse_x

if pos_x<0:

pos_x=0

elif pos_x>480:

pos_x=500

#draw basket

pygame.draw.rect(screen,black,(pos_x-4,pos_y-4,120,40),0)

pygame.draw.rect(screen,red,(pos_x,pos_y,120,40),0)

#print # of lives

print_text(font1,0,0,"Lives: "+str(lives))

#print score

print_text(font1,500,0,"Score: "+str(score))

pygame.display.update()

可以改进的地方很多,比如说难度的提升,炸弹的显示效果等等。

现在我会改进这个程序,但是并不能独立写出这个程序来。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。