如何使用pygame制作游戏

便宜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等来记录坐标,键盘控制,一个小游戏就做好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值