Pygame的工程架构
以现有hellsheep游戏为例:
用bat作为启动:
start.bat 内容:
cd content
pyg.exe hellsheep.py
content里放素材和程序:
hellsheep.py

import pygame from pygame.locals import * from sys import exit from random import randint, choice import math import os ############################################################### def inArea(gX,gY,pointX, pointY,pointX2, pointY2): if pointX<gX<pointX2 and pointY<gY<pointY2: return True else: return False def inCircle(pointX, pointY,gX,gY,distance): xDist = pointX-gX xDist=math.fabs(xDist) yDist = pointY-gY yDist=math.fabs(yDist) xyDist = (xDist*xDist)+(yDist*yDist) if distance >= math.sqrt(xyDist): return True else: return False wball_list=[] for i in range(10): wball_list.append([0, 0, 0, 0]) wball_list[i][0]= choice([-5, 650]) clock=pygame.time.Clock() pygame.mixer.init(44100, 16, 2, 4096) ################# pygame.init() font1= pygame.font.Font("etobicoke.ttf",18) font2= pygame.font.Font("etobicoke.ttf",25) SCENE = 0 joystick=None if pygame.joystick.get_count()>0: joystick=pygame.joystick.Joystick(0) joystick.init() joymow=0 joyturn=0 tonce=0 ############################### sheepX=300 sheepY=240 forward=0 forw=0 direct=0 timing=0 bgnumb=0 gulX, gulY = 100, 67 treeX, treeY = 150, 267 tree2X, tree2Y = 170, 280 tree3X, tree3Y = 200, 320 flowX, flowY = 26, 76 yballx=100 ybally=100 yballxp=-5 yballyp=-5 yballon=1 points=0 knpS=0 knpH=0 knpC=0 knpM=0 knpQ=0 dirindc=0 ndir=0 ############################### screen = pygame.display.set_mode((640,480), 0, 32) pygame.display.set_caption('Sheep in Bullet-Hell') icon = pygame.image.load("sheepicon32.png").convert_alpha() pygame.display.set_icon(icon) main = pygame.image.load("main.png").convert() bg2 = pygame.image.load("bg2.png").convert() startb = pygame.image.load("startb.png").convert() startg = pygame.image.load("startg.png").convert() howb = pygame.image.load("howb.png").convert() howg = pygame.image.load("howg.png").convert() creditb = pygame.image.load("creditsb.png").convert() creditg = pygame.image.load("creditsg.png").convert() quitb = pygame.image.load("quitb.png").convert() quitg = pygame.image.load("quitg.png").convert() mainb = pygame.image.load("mainb.png").convert() maing = pygame.image.load("maing.png").convert() BGX = pygame.image.load("green2.png").convert() BG = pygame.image.load("green2.png").convert() gul = pygame.image.load("gul1.png").convert_alpha() gulI = pygame.image.load("gul1.png").convert_alpha() tree = pygame.image.load("tree.png").convert_alpha() tree2 = pygame.image.load("tree.png").convert_alpha() tree3 = pygame.image.load("tree.png").convert_alpha() flowers = pygame.image.load("flowers.png").convert_alpha() sheepB1 = pygame.image.load("sheepB1.png").convert_alpha() sheepB2 = pygame.image.load("sheepB2.png").convert_alpha() sheepB3 = pygame.image.load("sheepB3.png").convert_alpha() sheepBQ1 = pygame.image.load("sheepBQ1.png").convert_alpha() sheepBQ2 = pygame.image.load("sheepBQ2.png").convert_alpha() sheepBQ3 = pygame.image.load("sheepBQ3.png").convert_alpha() sheepF1 = pygame.image.load("sheepF1.png").convert_alpha() sheepF2 = pygame.image.load("sheepF2.png").convert_alpha() sheepF3 = pygame.image.load("sheepF3.png").convert_alpha() sheepFQ1 = pygame.image.load("sheepFQ1.png").convert_alpha() sheepFQ2 = pygame.image.load("sheepFQ2.png").convert_alpha() sheepFQ3 = pygame.image.load("sheepFQ3.png").convert_alpha() sheepS1 = pygame.image.load("sheepS1.png").convert_alpha() sheepS2 = pygame.image.load("sheepS2.png").convert_alpha() sheepS3 = pygame.image.load("sheepS3.png").convert_alpha() wball = pygame.image.load("whiteball.png").convert_alpha() yball = pygame.image.load("yball.png").convert_alpha() hitsound = pygame.mixer.Sound("hit.ogg") bonus = pygame.mixer.Sound("BONUS7.ogg") click = pygame.mixer.Sound("CLICK10.ogg") pygame.mixer.music.load("music999618.ogg") pygame.mixer.music.play(-1) textjoy= font2.render("Joystick detected" , True, (255,255,255)) textjoy1= font1.render("Joystick detected" , True, (0,0,255)) textjoy0= font1.render("No joystick detected" , True, (255,0,0)) texthow= font2.render("HOW TO PLAY" , True, (255,255,255)) texthow1= font1.render("You move your character by holding down the Arrow Up key." , True, (0,0,0)) texthow2= font1.render("You turn by clicking Arrow Left or Arrow Right key." , True, (0,0,0)) texthow3= font1.render("Avoid getting hit by the white globes." , True, (0,0,0)) texthow4= font1.render("Try to catch the smaller yellow globe." , True, (0,0,0)) texthow5= font1.render("Walking over the edge of any of the boarders of the screen you get to" , True, (0,0,0)) texthow51= font1.render("a different part of the world." , True, (0,0,0)) texthow6= font1.render("If you have caught a yellow globe you need to walk over an edge" , True, (0,0,0)) texthow61= font1.render("to activate a new yellow globe." , True, (0,0,0)) texthow7= font1.render("Only one yellow globe can be active at the same time." , True, (0,0,0)) texthow8= font1.render("You can end the game with the ESC key." , True, (0,0,0)) texthow9= font1.render("Move forward by holding down the left mouse button." , True, (0,0,0)) texthow10= font1.render("Turn in the direction of the cursor by clicking the right mousebutton." , True, (0,0,0)) texthow11= font2.render("Move with Keys" , True, (0,0,0)) texthow12= font2.render("Move with Mouse" , True, (0,0,0)) texthow13= font2.render("Move with Joystick" , True, (0,0,0)) texthow14= font1.render("Hold stick in forward position to move forward." , True, (0,0,0)) texthow15= font1.render("Tilt stick left or right to turn." , True, (0,0,0)) textcred= font2.render("CREDITS" , True, (255,255,255)) textcred1=font2.render("Submission for Pygamer 2021 Jam" , True, (0,0,0)) textcred2=font2.render("a Per K Grok game" , True, (0,0,0)) textcred3=font2.render("Music by" , True, (0,0,0)) textcred4=font2.render("kadepow11" , True, (0,0,0)) textcred5=font2.render("Made with python and pygame" , True, (0,0,0)) textgame= font1.render("key H - How to Play; key C - Credits ",True,(0,255,255)) ######################################## while True: mx, my = pygame.mouse.get_pos() mrx, mry = pygame.mouse.get_rel() timePassed = clock.tick(30) textSurf1= font1.render("Points: " + str(points), True, (255,255,255), (0,0,0)) ############################################################ if SCENE==1: dirindc= math.degrees(math.atan2((sheepX-mx),(sheepY-my)))+180 if dirindc<22: ndir=6 elif dirindc<67: ndir=5 elif dirindc<112: ndir=4 elif dirindc<157: ndir=3 elif dirindc<202: ndir=2 elif dirindc<247: ndir=1 elif dirindc<292: ndir=0 elif dirindc<337: ndir=7 else: ndir=6 for i in range(10): # white ball move, over boarder, hit wball_list[i][0] += wball_list[i][2] wball_list[i][1] += wball_list[i][3] if wball_list[i][0]<0: wball_list[i][0] = 639 wball_list[i][1] = randint(50, 400) wball_list[i][2] = randint(-5, -3) wball_list[i][3] = randint(-5, 5) elif wball_list[i][0]>640: wball_list[i][0] = 1 wball_list[i][1] = randint(50, 400) wball_list[i][2] = randint(3, 5) wball_list[i][3] = randint(-5, 5) elif wball_list[i][1]>480: wball_list[i][1] = 1 wball_list[i][0] = randint(50, 590) wball_list[i][2] = randint(-5, 5) wball_list[i][3] = randint(3, 5) elif wball_list[i][1]<0: wball_list[i][1] = 479 wball_list[i][0] = randint(50, 590) wball_list[i][2] = randint(-5, 5) wball_list[i][3] = randint(-5, -3) if inCircle(wball_list[i][0]+11, wball_list[i][1]+11,sheepX,sheepY-15,30): # hit w ball channel=hitsound.play(0) points -= 10 wball_list[i][0]=650 ################################# yellow ball yballx += yballxp ybally += yballyp if yballx<0: yballx = 5 yballxp = randint(3, 5) elif yballx>640: yballx = 635 yballxp = randint(-5, -3) elif ybally>480: ybally = 475 yballyp = randint(-5, -3) elif ybally<0: ybally = 5 yballyp = randint(3, 5) if inCircle(yballx+7, ybally+7,sheepX,sheepY-15, 15) and yballon==1: channel=bonus.play(0) yballon=0 points +=1 ############################################################################### end 2 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() exit() if event.type==pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() exit() # if event.key == pygame.K_q: # pygame.quit() # exit() ###################################################################### if SCENE==0: if event.type==pygame.KEYDOWN: if event.key == pygame.K_s: points=0 pygame.mixer.music.stop() pygame.mixer.music.load("fast_999618.ogg") pygame.mixer.music.play(-1) SCENE=1 elif event.key == pygame.K_h: SCENE=2 elif event.key == pygame.K_c: SCENE=3 elif event.key == pygame.K_q: pygame.quit() exit() if event.type==pygame.MOUSEBUTTONDOWN: if knpS==1 and event.button==1: points=0 channel=click.play(0) pygame.mixer.music.stop() pygame.mixer.music.load("fast_999618.ogg") pygame.mixer.music.play(-1) SCENE=1 elif knpH==1 and event.button==1: channel=click.play(0) SCENE=2 knpM=0 elif knpC==1 and event.button==1: channel=click.play(0) SCENE=3 knpM=0 elif knpQ==1 and event.button==1: channel=click.play(0) pygame.quit() exit() ###################################################################### if SCENE==1: if event.type==pygame.KEYDOWN: if event.key == pygame.K_UP: forward=1 if event.key == pygame.K_RIGHT: direct += 1 if event.key == pygame.K_LEFT: direct -= 1 if event.key == pygame.K_h: pygame.mixer.music.stop() pygame.mixer.music.load("music999618.ogg") pygame.mixer.music.play(-1) SCENE=2 if event.key == pygame.K_c: pygame.mixer.music.stop() pygame.mixer.music.load("music999618.ogg") pygame.mixer.music.play(-1) SCENE=3 if direct>7: direct=0 if direct<0: direct=7 if event.type==pygame.MOUSEBUTTONDOWN: if event.button==1: forw=1 if event.button==3: direct=ndir if event.type==pygame.MOUSEBUTTONUP: if event.button==1: forw=0 elif event.type==pygame.KEYUP: if event.key == pygame.K_UP: forward=0 elif event.type==pygame.JOYAXISMOTION: if joystick.get_axis(1)< -0.8: joymow=1 else: joymow=0 if joystick.get_axis(0)< -0.8: joyturn=1 elif joystick.get_axis(0)> 0.8: joyturn=2 else: joyturn=0 tonce=0 ############################################ if SCENE==2: if event.type==pygame.KEYDOWN: if event.key == pygame.K_m: SCENE=0 if event.type==pygame.MOUSEBUTTONDOWN: if knpM==1 and event.button==1: channel=click.play(0) SCENE=0 if event.type==pygame.KEYDOWN: if event.key == pygame.K_s: pygame.mixer.music.stop() pygame.mixer.music.load("fast_999618.ogg") pygame.mixer.music.play(-1) SCENE=1 if event.type==pygame.MOUSEBUTTONDOWN: if knpS==1 and event.button==1: channel=click.play(0) pygame.mixer.music.stop() pygame.mixer.music.load("fast_999618.ogg") pygame.mixer.music.play(-1) SCENE=1 if event.type==pygame.KEYDOWN: if event.key == pygame.K_q: pygame.quit() exit() if event.type==pygame.MOUSEBUTTONDOWN: if knpQ==1 and event.button==1: channel=click.play(0) pygame.quit() exit() ################################### if SCENE==3: if event.type==pygame.KEYDOWN: if event.key == pygame.K_m: SCENE=0 if event.key == pygame.K_q: pygame.quit() exit() if event.type==pygame.MOUSEBUTTONDOWN: if knpM==1 and event.button==1: channel=click.play(0) SCENE=0 if knpQ==1 and event.button==1: channel=click.play(0) pygame.quit() exit() ######################################################################### if SCENE==0: if mx>15 and mx<175 and my>300 and my<335 : knpS=1 else: knpS=0 if mx>15 and mx<175 and my>340 and my<375 : knpH=1 else: knpH=0 if mx>15 and mx<175 and my>380 and my<415 : knpC=1 else: knpC=0 if mx>15 and mx<175 and my>420 and my<455 : knpQ=1 else: knpQ=0 ########################################################### elif SCENE==1: if joyturn==1 and tonce==0: direct -=1 tonce=1 elif joyturn==2 and tonce==0: direct +=1 tonce=1 if direct>7: direct=0 if direct<0: direct=7 if tonce>0: tonce +=1 if tonce>10: tonce=0 if forward==1 or forw==1 or joymow==1: if direct==0: sheepX -= 5 if timing<6: sheep= sheepS2 else: sheep= sheepS3 elif direct==1: sheepX -= 3 sheepY -= 3 if timing<6: sheep= pygame.transform.flip(sheepBQ2, True, False) else: sheep= pygame.transform.flip(sheepBQ3, True, False) elif direct==2: sheepY -= 5 if timing<6: sheep= sheepB2 else: sheep= sheepB3 elif direct==3: sheepX += 3 sheepY -= 3 if timing<6: sheep= sheepBQ2 else: sheep= sheepBQ3 elif direct==4: sheepX += 5 if timing<6: sheep= pygame.transform.flip(sheepS2, True, False) else: sheep= pygame.transform.flip(sheepS3, True, False) elif direct==5: sheepX += 3 sheepY+= 3 if timing<6: sheep= pygame.transform.flip(sheepFQ2, True, False) else: sheep= pygame.transform.flip(sheepFQ3, True, False) elif direct==6: sheepY += 5 if timing<6: sheep= sheepF2 else: sheep= sheepF3 elif direct==7: sheepX -= 3 sheepY += 3 if timing<6: sheep= sheepFQ2 else: sheep= sheepFQ3 ############################################## byta screen if sheepX<0: yballon=1 sheepX=630 bgnumb +=1 if bgnumb>3: bgnumb=0 flowX=randint(-50, 50) flowY=randint(50, 410) gulX=randint(100, 400) gulY=randint(100, 350) treeX=randint(100, 400) treeY=randint(100, 300) tree2X=randint(100, 400) tree2Y=randint(treeY+5, 360) tree3X=randint(100, 400) tree3Y=randint(tree2Y+5, 380) flip=randint(1,5) if flip==1: gulI=gul elif flip==2: gulI=pygame.transform.flip(gul, True, False) elif flip==3: gulI=pygame.transform.flip(gul, False, True) elif flip==3: gulI=pygame.transform.flip(gul, True, True) else: gulX=700 flip2=randint(1,5) if flip2>3: treeX=750 ##################################### if sheepX>630: yballon=1 sheepX=0 bgnumb +=1 if bgnumb>3: bgnumb=0 flowX=randint(-50, 50) flowY=randint(50, 410) gulX=randint(100, 400) gulY=randint(100, 350) treeX=randint(100, 400) treeY=randint(100, 300) tree2X=randint(100, 400) tree2Y=randint(treeY+5, 360) tree3X=randint(100, 400) tree3Y=randint(tree2Y+5, 380) flip=randint(1,5) if flip==1: gulI=gul elif flip==2: gulI=pygame.transform.flip(gul, True, False) elif flip==3: gulI=pygame.transform.flip(gul, False, True) elif flip==3: gulI=pygame.transform.flip(gul, True, True) else: gulX=700 flip2=randint(1,5) if flip2>3: treeX=750 ############################### if sheepY<0: yballon=1 sheepY=480 bgnumb +=1 if bgnumb>3: bgnumb=0 flowX=randint(-50, 50) flowY=randint(50, 410) gulX=randint(100, 400) gulY=randint(100, 350) treeX=randint(100, 400) treeY=randint(100, 300) tree2X=randint(100, 400) tree2Y=randint(treeY+5, 360) tree3X=randint(100, 400) tree3Y=randint(tree2Y+5, 380) flip=randint(1,5) if flip==1: gulI=gul elif flip==2: gulI=pygame.transform.flip(gul, True, False) elif flip==3: gulI=pygame.transform.flip(gul, False, True) elif flip==3: gulI=pygame.transform.flip(gul, True, True) else: gulX=700 flip2=randint(1,5) if flip2>3: treeX=750 ################################## if sheepY>480: yballon=1 sheepY=0 bgnumb +=1 if bgnumb>3: bgnumb=0 flowX=randint(-50, 50) flowY=randint(50, 410) gulX=randint(100, 400) gulY=randint(100, 350) treeX=randint(100, 400) treeY=randint(100, 300) tree2X=randint(100, 400) tree2Y=randint(treeY+5, 360) tree3X=randint(100, 400) tree3Y=randint(tree2Y+5, 380) flip=randint(1,5) if flip==1: gulI=gul elif flip==2: gulI=pygame.transform.flip(gul, True, False) elif flip==3: gulI=pygame.transform.flip(gul, False, True) elif flip==3: gulI=pygame.transform.flip(gul, True, True) else: gulX=700 flip2=randint(1,5) if flip2>3: treeX=750 if bgnumb==0: BGX=BG elif bgnumb==1: BGX=pygame.transform.flip(BG, True, False) elif bgnumb==2: BGX=pygame.transform.flip(BG, False, True) elif bgnumb==3: BGX=pygame.transform.flip(BG, True, True) elif forward==0: if direct==0: sheep=sheepS1 elif direct==1: sheep=pygame.transform.flip(sheepBQ1, True, False) elif direct==2: sheep=sheepB1 elif direct==3: sheep=sheepBQ1 elif direct==4: sheep=pygame.transform.flip(sheepS1, True, False) elif direct==5: sheep=pygame.transform.flip(sheepFQ1, True, False) elif direct==6: sheep=sheepF1 elif direct==7: sheep=sheepFQ1 timing+=1 if timing>10: timing=0 ############################################### if SCENE==2: if mx>7 and mx<167 and my>350 and my<385 : knpM=1 else: knpM=0 if mx>7 and mx<167 and my>390 and my<425 : knpS=1 else: knpS=0 if mx>7 and mx<167 and my>430 and my<465 : knpQ=1 else: knpQ=0 ########################### elif SCENE==3: if mx>7 and mx<167 and my>350 and my<385 : knpM=1 else: knpM=0 if mx>7 and mx<167 and my>390 and my<425 : knpQ=1 else: knpQ=0 ######################################################################### #############################################################################BLIT if SCENE==0: screen.blit(main,(0,0)) if knpS==0: screen.blit(startb,(15,300)) else: screen.blit(startg,(15,300)) if knpH==0: screen.blit(howb,(15,340)) else: screen.blit(howg,(15,340)) if knpC==0: screen.blit(creditb,(15,380)) else: screen.blit(creditg,(15,380)) if knpQ==0: screen.blit(quitb,(15,420)) else: screen.blit(quitg,(15,420)) if joystick: screen.blit(textjoy,(15,200)) ######################################### elif SCENE==1: screen.blit(BGX,(0,0)) screen.blit(gulI, (gulX, gulY)) screen.blit(flowers, (flowX, flowY)) if treeY<sheepY: screen.blit(tree,(treeX-56, treeY-88)) if tree2Y<sheepY: screen.blit(tree2,(tree2X-56, tree2Y-88)) if tree3Y<sheepY: screen.blit(tree3,(tree3X-56, tree3Y-88)) screen.blit(sheep,(sheepX-25,sheepY-30)) if treeY>=sheepY: screen.blit(tree,(treeX-56, treeY-88)) if tree2Y>=sheepY: screen.blit(tree2,(tree2X-56, tree2Y-88)) if tree3Y>=sheepY: screen.blit(tree3,(tree3X-56, tree3Y-88)) for i in range(10): screen.blit(wball, (wball_list[i][0], wball_list[i][1])) if yballon==1: screen.blit(yball, (yballx, ybally)) screen.blit(textSurf1,(10,10)) screen.blit(textgame,(10,460)) ################################ elif SCENE==2: screen.blit(bg2,(0,0)) screen.blit(texthow,(10,10)) screen.blit(sheepS1,(120,30)) screen.blit(wball,(178,25)) screen.blit(yball,(180,50)) screen.blit(texthow3,(200,30)) screen.blit(texthow4,(200,50)) screen.blit(texthow5,(200,70)) screen.blit(texthow51,(200,90)) screen.blit(texthow6,(200,110)) screen.blit(texthow61,(200,130)) screen.blit(texthow7,(200,150)) screen.blit(texthow8,(200,170)) screen.blit(texthow11,(200,200)) screen.blit(texthow1,(200,220)) screen.blit(texthow2,(200,240)) screen.blit(texthow12,(200,270)) screen.blit(texthow9,(200,290)) screen.blit(texthow10,(200,310)) screen.blit(texthow13,(200,340)) screen.blit(texthow14,(200,360)) screen.blit(texthow15,(200,380)) if joystick: screen.blit(textjoy1,(200,400)) else: screen.blit(textjoy0,(200,400)) if knpM==0: screen.blit(mainb,(7,350)) else: screen.blit(maing,(7,350)) if knpS==0: screen.blit(startb,(7,390)) else: screen.blit(startg,(7,390)) if knpQ==0: screen.blit(quitb,(7,430)) else: screen.blit(quitg,(7,430)) ############################################################ elif SCENE==3: screen.blit(bg2,(0,0)) screen.blit(textcred,(10,10)) screen.blit(textcred1,(210,100)) screen.blit(textcred2,(270,130)) screen.blit(textcred3,(290,180)) screen.blit(textcred4,(285,210)) screen.blit(textcred5,(350, 450)) if knpM==0: screen.blit(mainb,(7,350)) else: screen.blit(maing,(7,350)) if knpQ==0: screen.blit(quitb,(7,390)) else: screen.blit(quitg,(7,390)) ########################################## pygame.display.update()
pygame.org/news
,Best Wish 不负年华