用户:天上的流星_tianqiVirus查看:0 回复:1 评论:0 创建时间:2019-06-30T10:37:29
教你用pygame开发游戏
今日游戏:薯架(自创)
如果对几行代码没有看懂,那么你可能是没看过上一次的结果
那就去看看我之前的帖子
这一次我们来玩玩薯条和番茄酱
先弄下来图片
(这番茄酱和薯条不成比例啊)
不妨再定义两个类,分别为FrenchFried和tomatoJam
同时我们照样不知道它们的坐标,所以我就乱设了(200,200)和(300,300)
class FrenchFried:
def __init__(self):
self.image = pygame.image.load("assets/image/FrenchFried/2.png")
self.x = 200
self.y = 200
class tomatoJam:
def __init__(self):
self.image = pygame.image.load("assets/image/tomatoJam/1.png")
self.x = 300
self.y = 300
再用blit显示
screen.blit(myCat.image,(myCat.x,myCat.y))
screen.blit(fried1.image,(fried1.x,fried1.y))
screen.blit(jam1.image,(jam1.x,jam1.y))
看看效果
(猫:吃不到)
可以看见,酱太大了,薯条也不小,我们用喵oothscale缩放
class FrenchFried:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/FrenchFried/2.png"),(61,23))
self.x = 200
self.y = 200
class tomatoJam:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/tomatoJam/1.png"),(80,80))
self.x = 300
self.y = 300
正常点了
基于我们之前的思想,薯条和番茄酱会不断向左飞行
所以我们学着做mianCat的update函数一样给薯条和番茄酱加上update
class FrenchFried:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/FrenchFried/2.png"),(61,23))
self.x = 200
self.y = 200
def friedUpdate(self):
self.x-=0.8
class tomatoJam:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/tomatoJam/1.png"),(80,80))
self.x = 300
self.y = 300
def jamUpdate(self):
self.x-=0.5
不要忘记在循环中调用
myCat.catUpdate()
fried1.friedUpdate()
jam1.jamUpdate()
它们动起来了
但是一旦他们飞出屏幕,然后就没有然后了
此外,同时在一个y轴生成也太简单了,所以我们也要加上随机数
所以说我们还要进行侦测,正如我以前飞机大战的那个一样
先说说看随机数,还记得import random吗
由于我们只用random里的randint,所以我们只需要把代码改为from……import……的样式
那么这样,以后我们使用randint就不需要加上random.了
import pygame
from random import randint
import sys
然后我们再改动两个类的代码
class FrenchFried:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/FrenchFried/2.png"),(61,23))
self.x = 200
self.y = 200
def friedUpdate(self):
self.x-=0.8
if self.x<=-61:
self.x = 800
self.y = randint(100,500)
class tomatoJam:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/tomatoJam/1.png"),(80,80))
self.x = 300
self.y = 300
def jamUpdate(self):
self.x-=0.5
if self.x<=-80:
self.x = 800
self.y = randint(100,500)
结果不演示了
到了这里我们的技术任务就完成了
但是大家看懂了两个update函数中的一系列数字了吗
很显然,这样只有数字的情况是很难让人理解的
这种数字,叫做幻数
幻数很糟糕
所以我们得修改,才不至于到时候自己都不知道这个数字是什么意思
从开头开始
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Potato Cat")
由于"Potato Cat"在之后不会调用,所以我们保留这个
看到[800,600],这个就是幻数了
我们先在它们前面定义几个变量
windowWidth = 800
windowHeight = 600
pygame.init()
screen = pygame.display.set_mode([windowWidth,windowHeight])
pygame.display.set_caption("Potato Cat")
这就是消除幻数的一个例子
往后看
class mainCat:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/cat/1.png"),(32,32))
self.x = 100
self.y = 50
self.jump = 0
def catUpdate(self):
self.jump+=0.01
self.y+=self.jump
大家找到幻数了吗
这个幻数就是catUpdate中的0.01
大家知道怎么改的吧
class mainCat:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/cat/1.png"),(32,32))
self.x = 100
self.y = 50
self.jump = 0
self.downSpeed = 0.01
def catUpdate(self):
self.jump+=self.downSpeed
self.y+=self.jump
这样一来,数字的用途和来源就很明显了
同时,不止是数字,还有字符串,甚至代码
代码作为幻数消除方法就是函数了吧
总之,消除幻数,代码的易懂性,调用性更高了
给大家看一下没改好幻数的代码,看看你们能不能把幻数消除
同时看看你们能不能以开发者的身份解释里面(两个update函数)的数字
import pygame
from random import randint
import sys
windowWidth = 800
windowHeight = 600
pygame.init()
screen = pygame.display.set_mode([windowWidth,windowHeight])
pygame.display.set_caption("Potato Cat")
class mainCat:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/cat/1.png"),(32,32))
self.x = 100
self.y = 50
self.jump = 0
self.downSpeed = 0.01
def catUpdate(self):
self.jump+=self.downSpeed
self.y+=self.jump
class FrenchFried:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/FrenchFried/2.png"),(61,23))
self.x = 200
self.y = 200
def friedUpdate(self):
self.x-=0.8
if self.x<=-61:
self.x = 800
self.y = randint(100,500)
class tomatoJam:
def __init__(self):
self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/tomatoJam/1.png"),(80,80))
self.x = 300
self.y = 300
def jamUpdate(self):
self.x-=0.5
if self.x<=-80:
self.x = 800
self.y = randint(100,500)
keepGoing=True
myCat = mainCat()
fried1 = FrenchFried()
jam1 = tomatoJam()
while keepGoing:
screen.fi喵))
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
myCat.jump=-1.5
myCat.catUpdate()
fried1.friedUpdate()
jam1.jamUpdate()
screen.blit(myCat.image,(myCat.x,myCat.y))
screen.blit(fried1.image,(fried1.x,fried1.y))
screen.blit(jam1.image,(jam1.x,jam1.y))
pygame.display.update()
pygame.quit()
想要继续学习pygame和游戏制作的请关注
id:天上的流星
本帖内的所有代码,思想仅供参考,请勿用于商业盈利用途