用pygame写一个贪吃鬼游戏

Person类的创建

import pygame


pygame.init()  # 初始化

clock = pygame.time.Clock() # 设置时间


SCREEN = pygame.display.set_mode((400, 400))  # 设置窗口


#  魔鬼的类

class Person():

    def __init__(self):

        # 初始化方法会在类在创建对象时执行一次,所以我们放一些初始化内容

        self.person = pygame.image.load('person.png')

        # 获取他的表层对象,方便我们控制

        # 括号里面没写位置的话,默认返回的就是左上角

        self.person_rect = self.person.get_rect()


    #  通过屏幕对象进行绘制

    def person_update(self):

        # 绘制,第二个rect其实是包含他的位置的,

        SCREEN.blit(self.person,self.person_rect)



per = Person()  # 创建对象,方便我们调用

while True:

    SCREEN.fill((255, 255, 255))  # 填充

    per.person_update()  # 调用person类的方法

    pygame.display.update()  # 更新

    clock.tick(60)  # 刷新帧率

当我们创建对象以后,我们可以尝试着让这个火焰移动起来

这里用了一种新的移动方式,跟之前的移动方式有所不同,简单一点说就是他会更加丝滑

import pygame


pygame.init()  # 初始化

clock = pygame.time.Clock() # 设置时间


SCREEN = pygame.display.set_mode((400, 400))  # 设置窗口


#  魔鬼的类

class Person():

    def __init__(self):

        # 初始化方法会在类在创建对象时执行一次,所以我们放一些初始化内容

        self.person = pygame.image.load('person.png')

        # 获取他的表层对象,方便我们控制

        # 括号里面没写位置的话,默认返回的就是左上角

        self.person_rect = self.person.get_rect()


    #  通过屏幕对象进行绘制

    def person_update(self):

        # 绘制,第二个rect其实是包含他的位置的,

        SCREEN.blit(self.person,self.person_rect)


    def event_move(self):

        # 首先监听我们的退出部分

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                exit()

        # 这跟之前的教程的移动方式有所不同,至于有什么不同,大家可以自行测试

        key_presse = pygame.key.get_pressed()

        if key_presse[pygame.K_UP]:

            self.person_rect.y -= 3

        if key_presse[pygame.K_DOWN]:

            self.person_rect.y += 3

        if key_presse[pygame.K_LEFT]:

            self.person_rect.x -= 3

        if key_presse[pygame.K_RIGHT]:

            self.person_rect.x += 3



per = Person()  # 创建对象,方便我们调用

while True:

    SCREEN.fill((255, 255, 255))  # 填充

    per.person_update()  # 调用person类的方法

    per.event_move()



    pygame.display.update()  # 更新

    clock.tick(60)  # 刷新帧率

    1. 绘制苹果

接下来我们绘制苹果,苹果也可以作为一个类

import pygame


pygame.init()  # 初始化

clock = pygame.time.Clock() # 设置时间


SCREEN = pygame.display.set_mode((400, 400))  # 设置窗口


#  魔鬼的类

class Person():

    def __init__(self):

        # 初始化方法会在类在创建对象时执行一次,所以我们放一些初始化内容

        self.person = pygame.image.load('person.png')

        # 获取他的表层对象,方便我们控制

        # 括号里面没写位置的话,默认返回的就是左上角

        self.person_rect = self.person.get_rect()


    #  通过屏幕对象进行绘制

    def person_update(self):

        # 绘制,第二个rect其实是包含他的位置的,

        SCREEN.blit(self.person,self.person_rect)


    def event_move(self):

        # 首先监听我们的退出部分

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                exit()

        # 这跟之前的教程的移动方式有所不同,至于有什么不同,大家可以自行测试

        key_presse = pygame.key.get_pressed()

        if key_presse[pygame.K_UP]:

            self.person_rect.y -= 3

        if key_presse[pygame.K_DOWN]:

            self.person_rect.y += 3

        if key_presse[pygame.K_LEFT]:

            self.person_rect.x -= 3

        if key_presse[pygame.K_RIGHT]:

            self.person_rect.x += 3

class Apple():

    # 跟上面一样的初始化

    def __init__(self):

        self.apple = pygame.image.load('apple.png')

        # center是指定他的位置

        self.apple_rect = self.apple.get_rect(center = (100,100))

       

    # 绘制的方法

    def apple_update(self):

        SCREEN.blit(self.apple,self.apple_rect)


per = Person()  # 创建对象,方便我们调用

app = Apple()

while True:

    SCREEN.fill((255, 255, 255))  # 填充

    per.person_update()  # 调用person类的方法

    app.apple_update()

    per.event_move()



    pygame.display.update()  # 更新

    clock.tick(60)  # 刷新帧率

绘制完苹果之后,我们可以让火焰接触到苹果时,苹果会消失在换个位置,那这个方法可以写在苹果类中

苹果的移动

import pygame

import random

pygame.init()  # 初始化

clock = pygame.time.Clock() # 设置时间


SCREEN = pygame.display.set_mode((400, 400))  # 设置窗口


#  魔鬼的类

class Person():

    def __init__(self):

        # 初始化方法会在类在创建对象时执行一次,所以我们放一些初始化内容

        self.person = pygame.image.load('person.png')

        # 获取他的表层对象,方便我们控制

        # 括号里面没写位置的话,默认返回的就是左上角

        self.person_rect = self.person.get_rect()


    #  通过屏幕对象进行绘制

    def person_update(self):

        # 绘制,第二个rect其实是包含他的位置的,

        SCREEN.blit(self.person,self.person_rect)


    def event_move(self):

        # 首先监听我们的退出部分

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                exit()

        # 这跟之前的教程的移动方式有所不同,至于有什么不同,大家可以自行测试

        key_presse = pygame.key.get_pressed()

        if key_presse[pygame.K_UP]:

            self.person_rect.y -= 3

        if key_presse[pygame.K_DOWN]:

            self.person_rect.y += 3

        if key_presse[pygame.K_LEFT]:

            self.person_rect.x -= 3

        if key_presse[pygame.K_RIGHT]:

            self.person_rect.x += 3

class Apple():

    # 跟上面一样的初始化

    def __init__(self):

        self.apple = pygame.image.load('apple.png')

        # center是指定他的位置

        self.apple_rect = self.apple.get_rect(center = (100,100))

        print(2)


    # 绘制的方法

    def apple_update(self):

        SCREEN.blit(self.apple,self.apple_rect)



    def apple_move(self):

        # 由于我们在不停的绘制,且self.apple_rect坐标没有发生变化

        # 所以当碰到这个火焰时,我们只需要改动一下这个的x,y

        # 他就会自动更新位置

        if self.apple_rect.colliderect(per.person_rect):

            self.apple_rect.x = random.randint(50,380)

            self.apple_rect.x = random.randint(50,380)


per = Person()  # 创建对象,方便我们调用

app = Apple()

while True:

    SCREEN.fill((255, 255, 255))  # 填充

    per.person_update()  # 调用person类的方法


    per.event_move()

    app.apple_move()

    app.apple_update()


    pygame.display.update()  # 更新

    clock.tick(60)  # 刷新帧率

这个时候当再触碰苹果的时候,苹果就会移动了

让火焰碰到苹果后逐渐变大,逐渐变大就要用到pygame中控制图片的功能

import pygame

import random

pygame.init()  # 初始化

clock = pygame.time.Clock() # 设置时间


SCREEN = pygame.display.set_mode((400, 400))  # 设置窗口

person_scale = 1

#  魔鬼的类

class Person():

    def __init__(self):

        # 初始化方法会在类在创建对象时执行一次,所以我们放一些初始化内容

        self.person = pygame.image.load('person.png')

        # 我们进行修改图片,1,是修改的图片,2是他的旋转角度,3,3是缩放大小

        # 我们在绘制的时候要绘制他的新图片

        self.new_person01 = pygame.transform.rotozoom(self.person, 0, person_scale)


        # 获取他的表层对象,方便我们控制

        # 括号里面没写位置的话,默认返回的就是左上角

        self.person_rect = self.person.get_rect()


    #  通过屏幕对象进行绘制

    def person_update(self):

        # 绘制,第二个rect其实是包含他的位置的,

        SCREEN.blit(self.new_person01,self.person_rect)


    def event_move(self):

        # 首先监听我们的退出部分

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                exit()

               

               

        # 判断碰撞,碰撞后进行扩大

        if self.person_rect.colliderect(app.apple_rect):

            global person_scale

            person_scale += 1

            self.new_person01 = pygame.transform.rotozoom(self.person, 0, person_scale)





        # 这跟之前的教程的移动方式有所不同,至于有什么不同,大家可以自行测试

        key_presse = pygame.key.get_pressed()

        if key_presse[pygame.K_UP]:

            self.person_rect.y -= 3

        if key_presse[pygame.K_DOWN]:

            self.person_rect.y += 3

        if key_presse[pygame.K_LEFT]:

            self.person_rect.x -= 3

        if key_presse[pygame.K_RIGHT]:

            self.person_rect.x += 3

class Apple():

    # 跟上面一样的初始化

    def __init__(self):

        self.apple = pygame.image.load('apple.png')


        # center是指定他的位置

        self.apple_rect = self.apple.get_rect(center = (100,100))


    # 绘制的方法

    def apple_update(self):

        SCREEN.blit(self.apple,self.apple_rect)



    def apple_move(self):

        # 由于我们在不停的绘制,且self.apple_rect坐标没有发生变化

        # 所以当碰到这个火焰时,我们只需要改动一下这个的x,y

        # 他就会自动更新位置

        if self.apple_rect.colliderect(per.person_rect):

            self.apple_rect.x = random.randint(50,380)

            self.apple_rect.x = random.randint(50,380)




per = Person()  # 创建对象,方便我们调用

app = Apple()

while True:

    SCREEN.fill((255, 255, 255))  # 填充

    per.person_update()  # 调用person类的方法


    per.event_move()

    app.apple_move()

    app.apple_update()


    pygame.display.update()  # 更新

    clock.tick(60)  # 刷新帧率

这个代码已经可以做到扩大了,但是还有问题,变大后的矩形碰撞无法检测准确,这是因为我们的图形扩大了,但是没有重新去获取他的矩形范围

这种写法你会发现他是可以变大,但是他只有有时会变大,这个道理很简单,我们在两个类中都写了检测碰撞,一个检测了要马上跑,一个检测到了要变大,万一下面的刚检测到了,他马上开始跑了,但再当他上去判断时,发下已经判断不到了,所以我们要改善这种方法,只让他检测一次

import pygame

import random

pygame.init()  # 初始化

clock = pygame.time.Clock() # 设置时间


SCREEN = pygame.display.set_mode((400, 400))  # 设置窗口

person_scale = 1

#  魔鬼的类

class Person():

    def __init__(self):

        # 初始化方法会在类在创建对象时执行一次,所以我们放一些初始化内容

        self.person = pygame.image.load('person.png')

        # 我们进行修改图片,1,是修改的图片,2是他的旋转角度,3,3是缩放大小

        # 我们在绘制的时候要绘制他的新图片

        self.new_person01 = pygame.transform.rotozoom(self.person, 0, person_scale)


        # 获取他的表层对象,方便我们控制

        # 括号里面没写位置的话,默认返回的就是左上角

        self.person_rect = self.person.get_rect()


    #  通过屏幕对象进行绘制

    def person_update(self):

        # 绘制,第二个rect其实是包含他的位置的,

        SCREEN.blit(self.new_person01,self.person_rect)


    def event_move(self):

        # 首先监听我们的退出部分

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                exit()



        # 判断碰撞,碰撞后进行扩大

        if self.person_rect.colliderect(app.apple_rect):

            global person_scale

            person_scale += 1

            self.new_person01 = pygame.transform.rotozoom(self.person, 0, person_scale)

            self.person_rect = self.person.get_rect()

            per.person_update()


        # 这跟之前的教程的移动方式有所不同,至于有什么不同,大家可以自行测试

        key_presse = pygame.key.get_pressed()

        if key_presse[pygame.K_UP]:

            self.person_rect.y -= 3

        if key_presse[pygame.K_DOWN]:

            self.person_rect.y += 3

        if key_presse[pygame.K_LEFT]:

            self.person_rect.x -= 3

        if key_presse[pygame.K_RIGHT]:

            self.person_rect.x += 3

class Apple():

    # 跟上面一样的初始化

    def __init__(self):

        self.apple = pygame.image.load('apple.png')


        # center是指定他的位置

        self.apple_rect = self.apple.get_rect(center = (100,100))


    # 绘制的方法

    def apple_update(self):

        SCREEN.blit(self.apple,self.apple_rect)



    def apple_move(self):

        # 由于我们在不停的绘制,且self.apple_rect坐标没有发生变化

        # 所以当碰到这个火焰时,我们只需要改动一下这个的x,y

        # 他就会自动更新位置

        if self.apple_rect.colliderect(per.person_rect):

            self.apple_rect.x = random.randint(50,380)

            self.apple_rect.x = random.randint(50,380)




per = Person()  # 创建对象,方便我们调用

app = Apple()

while True:

    SCREEN.fill((255, 255, 255))  # 填充

    per.person_update()  # 调用person类的方法


    per.event_move()

    app.apple_move()

    app.apple_update()


    pygame.display.update()  # 更新

    clock.tick(60)  # 刷新帧率

完整代码

import pygame

import random

pygame.init()  # 初始化

clock = pygame.time.Clock() # 设置时间


SCREEN = pygame.display.set_mode((400, 400))  # 设置窗口

person_scale = 1

#  魔鬼的类

class Person():

    def __init__(self):

        # 初始化方法会在类在创建对象时执行一次,所以我们放一些初始化内容

        self.person = pygame.image.load('person.png')

        # 我们进行修改图片,1,是修改的图片,2是他的旋转角度,3,3是缩放大小

        # 我们在绘制的时候要绘制他的新图片

        self.new_person01 = pygame.transform.rotozoom(self.person, 0, person_scale)


        # 获取他的表层对象,方便我们控制

        # 括号里面没写位置的话,默认返回的就是左上角

        self.person_rect = self.new_person01.get_rect()


    #  通过屏幕对象进行绘制

    def person_update(self):

        # 绘制,第二个rect其实是包含他的位置的,

        SCREEN.blit(self.new_person01,self.person_rect)


    def event_move(self):

        # 首先监听我们的退出部分

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                exit()



        # 这跟之前的教程的移动方式有所不同,至于有什么不同,大家可以自行测试

        key_presse = pygame.key.get_pressed()

        if key_presse[pygame.K_UP]:

            self.person_rect.y -= 3

        if key_presse[pygame.K_DOWN]:

            self.person_rect.y += 3

        if key_presse[pygame.K_LEFT]:

            self.person_rect.x -= 3

        if key_presse[pygame.K_RIGHT]:

            self.person_rect.x += 3


    def person_move(self):

        global person_scale

        # 由于我们在不停的绘制,且self.apple_rect坐标没有发生变化

        # 所以当碰到这个火焰时,我们只需要改动一下这个的x,y

        # 他就会自动更新位置

        if self.person_rect.colliderect(app.apple_rect):

            app.app_move()

            person_scale += 0.3

            self.new_person01 = pygame.transform.rotozoom(self.person, 0, person_scale)

            self.person_rect = self.new_person01.get_rect(center=(self.person_rect.centerx, self.person_rect.centery))

            per.person_update()


class Apple():

    # 跟上面一样的初始化

    def __init__(self):

        self.apple = pygame.image.load('apple.png')


        # center是指定他的位置

        self.apple_rect = self.apple.get_rect(center = (100,100))


    # 绘制的方法

    def apple_update(self):

        SCREEN.blit(self.apple,self.apple_rect)


    def app_move(self):


        self.apple_rect.x = random.randint(50, 380)

        self.apple_rect.x = random.randint(50, 380)






per = Person()  # 创建对象,方便我们调用

app = Apple()

while True:

    SCREEN.fill((255, 255, 255))  # 填充

    per.person_update()  # 调用person类的方法


    per.event_move()

    per.person_move()

    app.apple_update()


    pygame.display.update()  # 更新

    clock.tick(60)  # 刷新帧率
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值