如何使用pygame制作游戏
程序员文章站
2024-01-17 21:57:46
...
便宜game下载途径不用多说,别的地方有,下面上代码。
import sys,pygame,random
from pygame.locals import*
pygame.init()
def printtext(font,x,y,text,color=(155,155,155)):
Text=font.render(text,True,color)
screen.blit(Text,(x,y))
font2=pygame.font.SysFont('fangsong',24)
pigg=0
x=300
y=400
xx=650
yy=550
shoot=True
dx=300
dy=-50
screen=pygame.display.set_mode((600,500),0,32)
pygame.display.set_caption('傻逼大战')
pygame.mouse.set_visible(False)
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
keys=pygame.key.get_pressed()
if keys[K_ESCAPE]:
pygame.quit()
sys.exit()
screen.fill(color=(125,125,0))
printtext(font2,0,0,'分数:'+str(pigg))
if keys[K_DOWN]:
y=y+0.2
if keys[K_UP]:
y=y-0.2
if keys[K_RIGHT]:
x=x+0.2
if keys[K_LEFT]:
x=x-0.2
if keys[K_SPACE]:
shoot=False
if shoot:
xx=x+20
yy=y+20
else:
yy=yy-9
if yy<999:
shoot=True
if dx<x:
dx=dx+0.02
if dx>x:
dx=dx-0.02
if dy>500:
dx=random.randint(0,600)
dy=-50
else:
dy=dy+0.1
if dx>(x-40)and dx<(x+40)and dy<(y+40) and dy>(y-40):
pygame.quit()
sys.exit()
if xx>dx and xx<(dx+40) and yy>dy and yy<dy+40:
dx=random.randint(0,600)
dy=-50
pigg=pigg+1
pygame.draw.rect(screen,(255,255,255),(int(x),int(y),40,40),0)
pygame.draw.rect(screen,(255,0,0),(int(dx),int(dy),40,40),0)
pygame.draw.circle(screen,(255,255,255),(int(xx),int(yy)),5,0)
pygame.display.update()
这里分别用x,y等来记录坐标,键盘控制,一个小游戏就做好了。