
做这个游戏要涉及的素材如下,要保存下来!
代码如下
import pygame
from pygame.locals import *
from sys import exit
from random import randint
def text_image(name,size,text,color):
font = pygame.font.SysFont(name,size)
image = font.render(text,True,color)
return image
#img = text_image(None,50,str(score),(255,255,255))
#screen.blit(img,(20,20))
path = 'resources/images/'
index = 0
y = 300
x = 0
score = 0
top = 0
left = 200
width = 60
y_distance = 150
x_distance = 300
color = (100,200,0)
rects1 = []
rects2 = []
for i in range(10):
height = randint(200,400)
rects1.append([left,top,width,height])
rects2.append([left,height+y_distance,width,600-height-y_distance])
left += x_distance
y_distance -= 5
x_distance -= 10
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('flappy bird')
bg = pygame.image.load(path + 'bg_day.png')
bg = pygame.transform.smoothscale(bg, (800, 600))
birds = [pygame.image.load(path + '0.png'),
pygame.image.load(path + '1.png'),
pygame.image.load(path + '2.png')]
pygame.key.set_repeat(1, 10)
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN:
key = event.key
if key == K_SPACE:
y = y - 5
if y <= 0:
y = 0
if key == K_LEFT:
x -= 1
if x <= 0:
x = 0
if key == K_RIGHT:
x += 1
if x >= 760:
x = 760
screen.blit(bg, (0, 0))
screen.blit(birds[index],(x, y))
for i in range(len(rects1)):
pygame.draw.rect(screen,color,rects1[i])
pygame.draw.rect(screen,color,rects2[i])
rects1[i][0] -= 1
rects2[i][0] -= 1
y = y + 2
if y > 560:
y = 560
if index == 2:
index = 0
else:
index = index + 1
if rects1 and rects1[0][0] <= -60:
rects1.pop(0)
rects2.pop(0)
score += 1
if rects1:
if (rects1[0][0] < 48 and rects1[0][3] > y) or (rects2[0][0] < 48 and rects2[0][1] < y + 48):
print("碰撞了")
img = text_image(None, 50,str(score), (255, 255, 255))
screen.blit(img, (20, 20))
pygame.display.update()
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)