import pygame,sys
from pygame.locals import *
import random
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
RED=(255, 0, 0)
TEXTCOLOR = BLACK
def drawText(text,font,surface,x,y):
textobj=font.render(text,1,TEXTCOLOR)
textrect=textobj.get_rect()
textrect.topleft=(x,y)
surface.blit(textobj,textrect)
pygame.init()
font=pygame.font.SysFont(None,48)
windowSurface=pygame.display.set_mode((1200,800))
brand=pygame.image.load("brand.png")
brand_size=pygame.transform.scale(brand,(120,120))
brand_position=pygame.Rect(10,10,300,260)
panda=pygame.image.load("pandas.png")
panda_size=pygame.transform.scale(panda,(120,120))
position=pygame.Rect(500,680,120,120)
bamboo=pygame.image.load("bamboo2.png")
bamboo_size=pygame.transform.scale(bamboo,(50,50))
bamboo_list=[]
for i in range(100):
bamboo_list.append(pygame.Rect(random.randint(100,1000),random.randint(-6000,100),1,1))
mainClock=pygame.time.Clock()
moveLeft = moveRight = False
PLAYERMOVERATE = 3
speed=20
score=0
start=False
pygame.display.set_caption("俞兆林品牌小游戏_熊猫大战竹子(空格键开始游戏!)")
while True:
windowSurface.fill((255,250,205))
windowSurface.blit(panda_size,position)
windowSurface.blit(brand_size,brand_position)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_LEFT or event.key==ord('a'):
moveRight=False
moveLeft=True
if event.key==K_RIGHT or event.key==ord('d'):
moveLeft=False
moveRight=True
if event.type==KEYUP:
if event.key==K_LEFT or event.key==ord('a'):
moveLeft=False
if event.key==K_RIGHT or event.key==ord('d'):
moveRight=False
if event.key==K_SPACE:
start=True
if start ==True:
if moveRight and position.right<1200:
position.right+=PLAYERMOVERATE
if moveLeft and position.left > 0:
position.left-=PLAYERMOVERATE
for i in bamboo_list:
i.move_ip(0,1 * PLAYERMOVERATE)
for i in bamboo_list:
if i.colliderect(position)==False:
windowSurface.blit(bamboo_size,i)
elif i.colliderect(position)==True:
bamboo_list.remove(i)
score +=1
drawText("score:",font, windowSurface,950,40 )
drawText(str(score) + " / 100",font, windowSurface,1060,42 )
pygame.display.update()
mainClock.tick(80)
图片公司版权所有!