直接上图
同样素材没有的需要替换或者找我,代码有不懂的话可以看视频
import pygame
import time
import random
import time
pygame.init()
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((900,300))
fenshu = pygame.font.SysFont("微软雅黑", 30)
new_fenshu = fenshu.render("score:", True, (0, 0, 0))
image_list = []
Cactus_list = []
number_list= []
b = 0
zaoxing = 0
sudu = 0
score = 0
picture = 0
color = (255,255,255)
game_open = 1
defeng = 0
pygame.mixer.Sound('background.ogg').play(-1) # 0为只播放一次
class Way:
def __init__(self,open = False):
if open:
self.image = pygame.image.load('dino/ground.png')
self.rect = self.image.get_rect()
self.rect.y = 280
self.rect.x = 900
else:
self.image = pygame.image.load('dino/ground.png')
self.rect = self.image.get_rect()
self.rect.y = 280
self.rect.x = 0
def draw(self):
self.rect.x -= 8
if self.rect.x <= -900:
self.rect.x = 900
SCREEN.blit(self.image, self.rect)
class Dinosaur(pygame.sprite.Sprite):
def __init__(self):
for i in range(1, 7):
self.image = pygame.image.load("dino/dino_run%s.png" % str(i))
self.rect = self.image.get_rect(left = 60, top = 240, width=30, height=30)
image_list.append(self.image)
def draw(self):
global b
global sudu
if b >= 6:
b = 0
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# exit()
if self.rect.y >= 240:
sudu = 0
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pygame.mixer.Sound('preview.mp3').play(0) # 0为只播放一次
sudu = -20
# 注意这段代码,你看他是每次减一,那就是第一次是-20,然后-19,-18,-17,-16
else:
sudu -= -1
print(self.rect.y)
self.rect.y += sudu
SCREEN.blit(image_list[b], self.rect)
b += 1
class Cactus():
def __init__(self):
for i in range(1, 10):
self.image = pygame.image.load("dino/cactus%s.png" % str(i))
self.rect = self.image.get_rect(left = 800, top = 240, width=30, height=30)
Cactus_list.append(self.image)
def move(self):
global defeng
global zaoxing
global score
global color
self.rect.x -= 8
if self.rect.x <= -20:
defeng += 1
score += 1
self.rect.x = 900
zaoxing =random.randint(1,8)
if score >= 3:
color = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
score = 0
SCREEN.blit(Cactus_list[zaoxing],self.rect)
def event_watch(self):
global game_open
if self.rect.colliderect(din.rect):
game = GameOver()
game.geameover_draw()
restart = Restart()
restart.restart_draw()
pygame.display.update()
game_open = 0
class GameOver:
def __init__(self):
self.image = pygame.image.load('dino/gameover.png')
self.rect = self.image.get_rect(center = (450, 150))
def geameover_draw(self):
SCREEN.blit(self.image,self.rect)
class Restart:
def __init__(self):
self.image = pygame.image.load('dino/restart.png')
self.rect = self.image.get_rect(center = (450, 180))
def restart_draw(self):
SCREEN.blit(self.image,self.rect)
def chick(self):
global game_open
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
print(event.pos)
if self.rect.collidepoint(x, y):
game_open = 1
cactus.rect.x = 900
print(11111)
way = Way()
way1 = Way(True)
din = Dinosaur()
cactus = Cactus()
restart = Restart()
while True:
if game_open == 1:
SCREEN.fill(color)
SCREEN.blit(new_fenshu,(10,10))
new_defeng = fenshu.render(str(defeng), True, (0, 0, 0))
SCREEN.blit(new_defeng,(100,10))
way.draw()
way1.draw()
din.draw()
cactus.move()
cactus.event_watch()
pygame.display.update()
clock.tick(60)
else:
restart.chick()
?
|