首页 > 编程语言 >Pygame游戏编程

Pygame游戏编程

时间:2023-01-01 17:44:37浏览次数:43  
标签:__ Pipeline 游戏 self 编程 pygame wallx Bird Pygame

1、篮球自动弹跳

import sys
import pygame
pygame.init()
size=width,height=640,480
screen=pygame.display.set_mode(size)
color=(0,0,0)
ball=pygame.image.load("ball3.jpg")
ballrect=ball.get_rect()
speed=[5,5]
clock=pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
ballrect=ballrect.move(speed)
if ballrect.left<0 or ballrect.right>width:
speed[0]=-speed[0]
if ballrect.top<0 or ballrect.bottom>height:
speed[1]=-speed[1]
screen.fill(color)
screen.blit(ball,ballrect)
pygame.display.flip()

 

 2、Flappy Bird游戏

import pygame
import sys
import random
class Bird(object):
def __init__(self):
self.birdRect=pygame.Rect(65,50,50,50)
self.birdStatus=[pygame.image.load("bird1.jpg"),
pygame.image.load("bird2.jpg"),
pygame.image.load("birddead.jpg")]
self.status=0
self.birdX=120
self.birdY=350
self.jump=False
self.jumpSpeed=10
self.gravity=5
self.dead=False
def birdUpdate(self):
if self.jump:
self.jumpSpeed-=1
self.birdY-=self.jumpSpeed
else:
self.gravity+=0.2
self.birdY+=self.gravity
self.birdRect[1]=self.birdY
class Pipeline(object):
def __init__(self):
self.wallx=400
self.pineUp=pygame.image.load("top.jpg")
self.pineDown=pygame.image.load("bottom.jpg")
def updatePipeline(self):
self.wallx-=5
if self.wallx<-80:
global score
score+=1
self.wallx=400
def createMap():
screen.fill((255,255,255))
screen.blit(background,(0,0))
screen.blit(Pipeline.pineUp,(Pipeline.wallx,-300))
screen.blit(Pipeline.pineDown,(Pipeline.wallx,500))
Pipeline.updatePipeline()
if Bird.dead:
Bird.status=2
elif Bird.jump:
Bird.status=1
screen.blit(font.render('Score:'+str(score),-1,(255,255,255)),(100,50))
Bird.birdUpdate()
pygame.display.update()
def checkDead():
upRect=pygame.Rect(Pipeline.wallx,-300,
Pipeline.pineUp.get_width()-10,
Pipeline.pineUp.get_height())
downRect=pygame.Rect(Pipeline.wallx,500,
Pipeline.pineDown.get_width()-10,
Pipeline.pineDown.get_height())
if upRect.colliderect(Bird.birdRect) or downRect.colliderecr(Bird.birdRect):
Bird.dead=True
return True
else:
return False
if __name__=='__main__':
pygame.init()
pygame.font.init()
font=pygame.font.SysFont(None,50)
size=width,height=400,650
screen=pygame.display.set_mode(size)
clock=pygame.time.Clock()
Pipeline=Pipeline()
Bird=Bird()
score=0
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if (event.type==pygame.KEYDOWN or event.type==pygame.MOUSEBUTTONDOWN) and not Bird.dead:
Bird.jump=True
Bird.gravity=5
Bird.jumpSpeed=10
background=pygame.image.load("background4.jpg")
createMap()

 

标签:__,Pipeline,游戏,self,编程,pygame,wallx,Bird,Pygame
From: https://www.cnblogs.com/666u/p/17018340.html

相关文章