zoukankan      html  css  js  c++  java
  • python pygame做成的第一个真的意义上的游戏!

    #-*- coding:utf-8 -*-
    import  pygame , sys
    from pygame.locals import *
    
    class Myballclass(pygame.sprite.Sprite):
        def __init__(self,image_file,speed,location):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load(image_file)
            self.rect = self.image.get_rect()
            self.rect.left, self.rect.top = location
            self.speed = speed
    
        def move(self):
            self.rect = self.rect.move(self.speed)
            if self.rect.left < 0 or self.rect.right > screen.get_width():
                self.speed[0] = -self.speed[0]
            if self.rect.top <= 0 :
                self.speed[1] = -self.speed[1]
            
        def over(self):
            self.rect=self.rect.move(self.speed)
            if self.rect.bottom>screen.get_height():
                font =pygame.font.SysFont("宋体",40)
                text_surface=font.render(u"GAME OVER",True,(0,0,255))
                screen.blit(text_surface,(320,240))
    
                return 0
          
            
    class Mypaddleclass(pygame.sprite.Sprite):
        def __init__(self, location):
            pygame.sprite.Sprite.__init__(self)
            image_surface = pygame.surface.Surface([100, 20])
            #控制蓝色标签的大小
            image_surface.fill([213,213,123])
            #控制颜色
            self.image = image_surface.convert()
    
            self.rect = self.image.get_rect()
            self.rect.left, self.rect.top = location
    
    pygame.init()
    screen = pygame.display.set_mode([1280,960])
    background=pygame.image.load("6.jpg").convert()
    clock = pygame.time.Clock()
    ball_speed = [4, -4]
    score=0
    #控制移方向的,参数为正时并且越大的移动越快t
    #最后一个参数控制图像初始位置的
    myball = Myballclass('5.png', ball_speed, [10,50])
    ballgroup = pygame.sprite.Group(myball)
    paddle = Mypaddleclass([270,400])
    #控制蓝色标签的初始位置
    time=20
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                paddle.rect.centerx = event.pos[0]
        if pygame.sprite.spritecollide(paddle, ballgroup, 0):
            myball.speed[1] = -myball.speed[1]
            time=time+1
            score=score+10 
        clock.tick(time)
    
        screen.fill([255,255,255])
        font =pygame.font.SysFont("宋体",40)
        text_surface=font.render(u'分数'+str(score),True,(0,0,255))
        screen.blit(text_surface,(32,24))
        
        #open()参数的用法a+连续写入不覆盖,r+写入最后一个,w+写入的内容全覆盖
        scorefile=open('a.txt','r+')
        for a in scorefile.readline(10):
            text_surface1=font.render(u'最高记录   '+str(a),True,(0,0,255))    
            screen.blit(text_surface1,(320,24))
            print "woqu"
            print a
            print "nima"
            print score
            if (a<score):
                print a
                print a*a
                print score
                scorefile.flush()
                scorefile.writelines(str(score))
        
        scorefile.close()
        myball.move()
        myball.over()
        screen.blit(myball.image, myball.rect)
        screen.blit(paddle.image, paddle.rect)
        pygame.display.flip()
    

    这个是一个接物游戏!接到之后速度不断变快,直到接不住为止

  • 相关阅读:
    基于jQuery解决ios10以上版本缩放问题
    移动端h5模拟长按事件
    一篇讲SpringBoot+kafka很好的文章
    Liquibase+SpringBoot的简单使用笔记!update+rollback
    集合异同,找出新增元素和删除元素
    spring-security-结合JWT的简单demo
    IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统
    提取swagger内容到csv表格,excel可打开
    spring mvc 黑马 笔记
    手机页面图片显示高低不一致
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3262893.html
Copyright © 2011-2022 走看看