微信关注公众号:夜寒信息
致力于为每一位用户免费提供更优质技术帮助与资源供给,感谢支持!
前几天学习了Pygame,想试着用以用,于是写了这个小游戏。优化比较差,游戏精灵的碰撞检测没学好,不会利用,导致代码冗余,感兴趣的同学可以拿来代码自己优化一下,很抱歉发了半成品,若有学习交流意向可以与我一起研究。
#上长的上边缘为211像素,下边缘为332像素。
#下长的上边缘为68像素,下边缘为189像素。
#中长的上边缘为156像素,下边缘为279像素。
#管间距300
#管上下间距为121
import pygame, sys, random, time
from pygame.locals import *
class Pipe(pygame.sprite.Sprite):
def __init__(self, bg, bar, bar2, pipe_sg, pipe_xg):
super().__init__()
self.bg = pygame.image.load(bg).convert()
self.bar = pygame.image.load(bar).convert()
self.bar2 = pygame.image.load(bar2).convert()
self.sg = pygame.image.load(pipe_sg).convert()
self.xg = pygame.image.load(pipe_xg).convert()
self.rect_bg = self.bg.get_rect()
self.rect_sg1 = self.sg.get_rect()
self.rect_sg2 = self.sg.get_rect()
self.rect_sg3 = self.sg.get_rect()
self.rect_sg4 = self.sg.get_rect()
self.rect_xg1 = self.xg.get_rect()
self.rect_xg2 = self.xg.get_rect()
self.rect_xg3 = self.xg.get_rect()
self.rect_xg4 = self.xg.get_rect()
self.rect_bar = self.bar.get_rect()
self.rect_bar2 = self.bar2.get_rect()
class Bird(pygame.sprite.Sprite):
def __init__(self, bird_fly, bird_jmp, bird_die):
super().__init__()
self.fly = pygame.image.load(bird_fly).convert_alpha()
self.jmp = pygame.image.load(bird_jmp).convert_alpha()
self.die = pygame.image.load(bird_die).convert_alpha()
self.rect_fly = self.fly.get_rect()
self.rect_jmp = self.jmp.get_rect()
self.rect_die = self.die.get_rect()
def check(b, p, pipe_key):
if pipe_key == 2:
if p.rect_sg1.right > b.rect_fly.left and b.rect_fly.top = p.rect_sg1.left:
return True
elif b.rect_fly.right >= p.rect_xg1.left and b.rect_fly.bottom >= p.rect_xg1.top and b.rect_fly.left b.rect_jmp.left and b.rect_jmp.top = p.rect_sg1.left:
return True
elif b.rect_jmp.right >= p.rect_xg1.left and b.rect_jmp.bottom >= p.rect_xg1.top and b.rect_jmp.left
b.rect_fly.left and b.rect_fly.top = p.rect_sg2.left:
return True
elif b.rect_fly.right >= p.rect_xg2.left and b.rect_fly.bottom >= p.rect_xg2.top and b.rect_fly.left
b.rect_jmp.left and b.rect_jmp.top = p.rect_sg2.left:
return True
elif b.rect_jmp.right >= p.rect_xg2.left and b.rect_jmp.bottom >= p.rect_xg2.top and b.rect_jmp.left
b.rect_fly.left and b.rect_fly.top = p.rect_sg3.left:
return True
elif b.rect_fly.right >= p.rect_xg3.left and b.rect_fly.bottom >= p.rect_xg3.top and b.rect_fly.left
b.rect_jmp.left and b.rect_jmp.top = p.rect_sg3.left:
return True
elif b.rect_jmp.right >= p.rect_xg3.left and b.rect_jmp.bottom >= p.rect_xg3.top and b.rect_jmp.left
b.rect_fly.left and b.rect_fly.top = p.rect_sg4.left:
return True
elif b.rect_fly.right >= p.rect_xg4.left and b.rect_fly.bottom >= p.rect_xg4.top and b.rect_fly.left
b.rect_jmp.left and b.rect_jmp.top = p.rect_sg4.left:
return True
elif b.rect_jmp.right >= p.rect_xg4.left and b.rect_jmp.bottom >= p.rect_xg4.top and b.rect_jmp.left
= 352:
bg_speed = [0, 0]
jmp_key = 0
# fly_speed = [0, 0]
screen.blit(b.die, b.rect_fly)
# b.rect_fly.bottom = 364
die_key = 1
if jmp_key and jmp_speed[1] >= -12:
jmp_speed[1] -= 2
screen.blit(b.jmp, b.rect_fly)
b.rect_fly = b.rect_fly.move(jmp_speed)
else:
jmp_key = 0
jmp_speed = [0, -1]
if die_key:
if b.rect_fly.bottom = 352:
fly_speed = [0, 0]
b.rect_fly.bottom = 364
die_key = 0
else:
bg_speed = [0, 0]
screen.blit(b.die, b.rect_fly)'''
# b.rect_fly = b.rect_fly.move(fly_speed)
if p.rect_bar.left 0:
p.rect_bar2.left = p.rect_bar.right
elif p.rect_bar.right <= 0 and p.rect_bar.left < 0:
p.rect_bar.left = p.rect_bar2.right
if die_key and vol_allow:
pygame.mixer.music.stop()
die_sound.play()
vol_allow = 0
#pygame.time.delay(100)
#die_sound.set_volume(0)
screen.blit(p.bar, p.rect_bar)
screen.blit(p.bar2, p.rect_bar2)
p.rect_bar = p.rect_bar.move(bg_speed)
p.rect_bar2 = p.rect_bar2.move(bg_speed)
p.rect_sg1 = p.rect_sg1.move(bg_speed)
p.rect_xg1 = p.rect_xg1.move(bg_speed)
p.rect_sg2 = p.rect_sg2.move(bg_speed)
p.rect_xg2 = p.rect_xg2.move(bg_speed)
p.rect_sg3 = p.rect_sg3.move(bg_speed)
p.rect_xg3 = p.rect_xg3.move(bg_speed)
p.rect_sg4 = p.rect_sg4.move(bg_speed)
p.rect_xg4 = p.rect_xg4.move(bg_speed)
clock.tick(30)
pygame.display.update()
if __name__ == "__main__":
main()
这是素材文件:
https://www.lanzous.com/iabevof
微信关注公众号:夜寒信息
致力于为每一位用户免费提供更优质技术帮助与资源供给,感谢支持!