Let's play a python game today.
Using python's game framework, pygame develops a self-created petal-eating game for beans [not named yet? ]
First of all, look at what files are in the main directory of the downstream play.
Just an img folder and a py file
Under the img folder are these Photo resources made with photoshop, as well as Chinese fonts.
Open sublime text development software and write to the original game framework
#-*- coding:utf-8 -*- import pygame from pygame.locals import * import random from random import randint import time from sys import exit pygame.init() def main(): SCREEN_SIZE = [640,480] screen=pygame.display.set_mode(SCREEN_SIZE,0,32) title=pygame.display.set_caption('Delicious') pygame.display.flip() #Refresh interface while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() screen.fill([0,0,0]) #The filling plane has a black background. pygame.display.update()#Game Update if __name__ == '__main__': main()
The output is as follows:
A frame with a black background appears.
This proves that the game has been built successfully!
Now it's popular. pygame Various methods in the game:
screen = pygame.display.set_mode([640,480]) #Create a window box 640, 480 high
bg = pygame.image.load('bg.png') #Image loaded as bg is converted to plane variable bg
screen.blit(bg,(0,0)) #Draw a picture plane for bg
mysecond_font=pygame.font.Font("img/poster.ttf",35) #Load fonts with a font size of 35px
screen.fill([0,0,0]) #Fill the screen in black [three 255, white, rgb filled]
#Here is the circular rotation event detection with keystrokes, mice, etc. for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == KEYDOWN: if event.key == K_w: myman[1]-=10 if event.key == K_s: myman[1]+=10 if event.key == K_a: mydirection='left' myman[0]-=10 if event.key == K_d: mydirection='right' myman[0]+=10 if event.type == KEYUP: pass
Here is the quotation
#Wallpaper switching in left and right directions def screenblit(): #Drawing left and right state of food if mydirection=='left': screen.blit(man1a,myman) if mydirection=='right': screen.blit(man1a_back,myman) #This is the role's position around the wall detection def overwall(): if myman[0]<10: myman[0]=10 if myman[0]>595: myman[0]=595 if myman[1]<10: myman[1]=10 if myman[1]>435: myman[1]=435
But in the pygame framework, many animations need to be written by themselves, setting and refreshing all need to be done by themselves, but it's hard for the author to make a demo function to change the appearance of the characters.
def blit_animation(mybgflag,myman): #1. Mouth animation if mybgflag[1]==0: mybgflag[1]=50 if mydirection=='left': if mybgflag[0]==1: screen.blit(man1b,myman) mybgflag[0]=0 else: screen.blit(man1a,myman) mybgflag[0]=1 if mydirection=='right': if mybgflag[0]==1: screen.blit(man1b_back,myman) mybgflag[0]=0 else: screen.blit(man1a_back,myman) mybgflag[0]=1 mybgflag[1]-=5 #2. Up and down jumping animation if mybgflag[3]==0: mybgflag[3]=50 if mybgflag[2]==1: myman[0]-=1.5 myman[1]+=1.5 mybgflag[2]=0 else: myman[0]+=1.5 myman[1]-=1.5 mybgflag[2]=1 mybgflag[3]-=5 return mybgflag,myman#Final return
# Next you need to create monsters!
def createenemys(enemytimers,enemysa,enemyseveral):#Create monsters if enemytimers==0 and enemyseveral>0: #Create 5 monsters print 'create-enemy' enemyrand=random.randint(1, 8) if enemyrand==1 or enemyrand == 3:#top enemysa.append([random.randint(30, 610),-35,40]) elif enemyrand==2 or enemyrand == 4:#down enemysa.append([random.randint(30, 610),515,40]) elif enemyrand==5 or enemyrand == 7:#left enemysa.append([-35,random.randint(30, 610),40]) elif enemyrand==6 or enemyrand == 8:#right enemysa.append([675,random.randint(30, 610),40]) enemytimers=100 enemyseveral-=1 enemytimers-=5 for enemy in enemysa:#Random movement of monsters if enemy[1]<0: enemy[1]+=1 else: if enemy[2]==0: enemy[2]=40 enemydirection=random.randint(1, 45000) if enemydirection>0 and enemydirection<10000:#top enemy[1]-=2 for i in range(random.randint(5, 10)): enemy[1]-=3 elif enemydirection>=11000 and enemydirection<20000:#down enemy[1]+=3 for i in range(random.randint(5, 10)): enemy[1]+=3 elif enemydirection>=21000 and enemydirection<30000:#left enemy[0]-=3 for i in range(random.randint(5, 10)): enemy[0]-=3 elif enemydirection>=31000 and enemydirection<=40000:#right enemy[0]+=3 for i in range(random.randint(5, 10)): enemy[0]+=3 elif enemydirection>40000 and enemydirection<=45000:#none pass enemy[2]-=5 for enemy in enemysa:#Crossing the Borders of Monsters if enemy[0]<10: enemy[0]=10 if enemy[0]>595: enemy[0]=595 if enemy[1]<10: enemy[1]=10 if enemy[1]>435: enemy[1]=435 for enemy in enemysa: #Drawing monsters screen.blit(enemysaimg,(enemy[0],enemy[1])) return enemytimers,enemysa,enemyseveral#Final return
def createfoods(food1,food1s,foodserverl): if foodserverl: print 'food' food1s.append((random.randint(10, 615),random.randint(10, 445))) foodserverl-=1 if foodserverl==0 and len(food1s)==0: foodserverl=random.randint(1, 5) for food in food1s: screen.blit(food1,food) return food1,food1s,foodserverl#Final return
#Did I eat any food? def eachcheck(myman,enemysa,food1s,myblood,score): #My collision with food foodn=0 for food in food1s: if myman[0]+30>food[0] and myman[0]<food[0]+25: if myman[1]+30>food[1] and myman[1]<food[1]+25: print 'MY AND FOOD' food1s.pop(foodn) if score>=0: score+=random.randint(1, 5)#Eat a small flower and add 1-5 points foodn+=1 for enemy in enemysa:#The collision between monsters and me if myman[0]+30>enemy[0] and myman[0]<enemy[0]+30: if myman[1]+30>enemy[1] and myman[1]<enemy[1]+30: print 'I AND ENEMY' if myblood>=0: myblood-=1#My Blood Button for food in food1s:#I got food. if myman[0]+30>food[0] and myman[0]<food[0]+25: if myman[1]+30>food[1] and myman[1]<food[1]+25: print 'I AND FOOD' for enemy in enemysa:#The Collision of Monsters with Food foodn=0 for food in food1s: if enemy[0]+30>food[0] and enemy[0]<food[0]+25: if enemy[1]+30>food[1] and enemy[1]<food[1]+25: print 'ENEMY AND FOOD' food1s.pop(foodn) myblood-=5 foodn+=1 return myman,enemysa,food1s,myblood,score
# The next step is to draw the win-lose interface of the character's blood game and the 60-second countdown function in the game as follows:
def blitmyblood(myblood,myblood_surface,score): screen.blit(myblood_surface,[5,0]) screen.blit(myscore_surface,[450,0]) bloodbordor=pygame.draw.rect(screen,[0,0,0],[60,5,200,15],2) bloodvalue=pygame.draw.rect(screen,[255,255,0],[62,7,myblood,12]) myscoretts_surface=myscore_font.render('%s'%score,True,(0,0,255)) screen.blit(myscoretts_surface,[535,0]) second_surface=mysecond_font.render('%s'%gamesecond,True,(255,255,0)) screen.blit(second_surface,[0,440]) return myblood,myblood_surface,score def GameOver(myblood,gameoverwindows,myman): if myblood<=0: screen.blit(gameoverbg2,[0,0]) gameoverwindows=1 return gameoverwindows,myman def GameVictory(myblood,score,gamesecond): if myblood>0 and gamesecond<=0 and score>=100: screen.blit(victorybg,[0,0]) return myblood,score,gamesecond def gametimes(timeflag,gamesecond): if timeflag==20: if gamesecond>0: gamesecond-=1 timeflag=0 timeflag+=1 return timeflag,gamesecond
Now provide source code
Insert code slices here
#-- coding:utf-8 --
import pygame
from pygame.locals import *
import random
from random import randint
import time
from sys import exit
pygame.init()
# Principal function
def main():
SCREEN_SIZE=[640,480]
FULLFLAG=0
systemtime=0.05
timeflag=0
gamesecond=120
# Painting in seconds
mysecond_font=pygame.font.Font("img/poster.ttf",35)
second_surface=mysecond_font.render('%s'%gamesecond,True,(0,0,255))
screen=pygame.display.set_mode(SCREEN_SIZE,0,32) title=pygame.display.set_caption('Delicious') screen.fill([0,0,0]) bg=pygame.image.load('img/bg.png').convert_alpha() wall=pygame.image.load('img/wall.png').convert_alpha() man1a=pygame.image.load('img/man1a.png').convert_alpha() man1a_back=pygame.image.load('img/man1a_back.png').convert_alpha() man1b=pygame.image.load('img/man1b.png').convert_alpha() man1b_back=pygame.image.load('img/man1b_back.png').convert_alpha() myman=[SCREEN_SIZE[0]/2-35,SCREEN_SIZE[1]/2-35] mybgflag=[1,50,1,30]#The first is the opening of the mouth for eating, the second is how often to open it 50 times, and the third is the jumping picture. mydirection='right' myblood=196#Eating blood strips #food food1=pygame.image.load('img/food1.png').convert_alpha() food1s=[[]] food1s.pop(0) foodserverl=random.randint(3, 6)#Random Number of Food #Fraction score=0 myblood_font=pygame.font.Font("img/poster.ttf",25) myblood_surface=myblood_font.render(u'physical strength',True, (255,255,0)) myscore_font=pygame.font.Font("img/poster.ttf",35) myscore_surface=myscore_font.render(u'Fraction',True,(0,0,255)) myscoretts_surface=myscore_font.render('%s'%score,True,(0,0,255)) #Monster 1 enemysa=[[]]#Monster List enemysa.pop(0) enemytimers=100#Number of monster generation times enemyseveral=random.randint(10,15)#Number of monsters enemysaimg=pygame.image.load('img/enemy1.png').convert_alpha()#Monster pictures gameoverwindows=0#The end of the game is false. gameoverbg2=pygame.image.load('img/GameOverbg2.png').convert_alpha() victorybg=pygame.image.load('img/victory.png').convert_alpha() pygame.key.set_repeat(1,100)#Repeat a key pygame.display.flip()#frame refresh def screenblit(): #Drawing left and right state of food if mydirection=='left': screen.blit(man1a,myman) if mydirection=='right': screen.blit(man1a_back,myman) def pygame_event(mydirection): for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == KEYDOWN: if event.key == K_w: myman[1]-=10 if event.key == K_s: myman[1]+=10 if event.key == K_a: mydirection='left' myman[0]-=10 if event.key == K_d: mydirection='right' myman[0]+=10 if event.type == KEYUP: pass return mydirection def overwall(): if myman[0]<10: myman[0]=10 if myman[0]>595: myman[0]=595 if myman[1]<10: myman[1]=10 if myman[1]>435: myman[1]=435 def blit_animation(mybgflag,myman): #1. Mouth animation if mybgflag[1]==0: mybgflag[1]=50 if mydirection=='left': if mybgflag[0]==1: screen.blit(man1b,myman) mybgflag[0]=0 else: screen.blit(man1a,myman) mybgflag[0]=1 if mydirection=='right': if mybgflag[0]==1: screen.blit(man1b_back,myman) mybgflag[0]=0 else: screen.blit(man1a_back,myman) mybgflag[0]=1 mybgflag[1]-=5 #2. Up and down jumping animation if mybgflag[3]==0: mybgflag[3]=50 if mybgflag[2]==1: myman[0]-=1.5 myman[1]+=1.5 mybgflag[2]=0 else: myman[0]+=1.5 myman[1]-=1.5 mybgflag[2]=1 mybgflag[3]-=5 return mybgflag,myman#Final return def createenemys(enemytimers,enemysa,enemyseveral):#Create monsters if enemytimers==0 and enemyseveral>0: #Create 5 monsters print 'create-enemy' enemyrand=random.randint(1, 8) if enemyrand==1 or enemyrand == 3:#top enemysa.append([random.randint(30, 610),-35,40]) elif enemyrand==2 or enemyrand == 4:#down enemysa.append([random.randint(30, 610),515,40]) elif enemyrand==5 or enemyrand == 7:#left enemysa.append([-35,random.randint(30, 610),40]) elif enemyrand==6 or enemyrand == 8:#right enemysa.append([675,random.randint(30, 610),40]) enemytimers=100 enemyseveral-=1 enemytimers-=5 for enemy in enemysa:#Random movement of monsters if enemy[1]<0: enemy[1]+=1 else: if enemy[2]==0: enemy[2]=40 enemydirection=random.randint(1, 45000) if enemydirection>0 and enemydirection<10000:#top enemy[1]-=2 for i in range(random.randint(5, 10)): enemy[1]-=3 elif enemydirection>=11000 and enemydirection<20000:#down enemy[1]+=3 for i in range(random.randint(5, 10)): enemy[1]+=3 elif enemydirection>=21000 and enemydirection<30000:#left enemy[0]-=3 for i in range(random.randint(5, 10)): enemy[0]-=3 elif enemydirection>=31000 and enemydirection<=40000:#right enemy[0]+=3 for i in range(random.randint(5, 10)): enemy[0]+=3 elif enemydirection>40000 and enemydirection<=45000:#none pass enemy[2]-=5 for enemy in enemysa:#Crossing the Borders of Monsters if enemy[0]<10: enemy[0]=10 if enemy[0]>595: enemy[0]=595 if enemy[1]<10: enemy[1]=10 if enemy[1]>435: enemy[1]=435 for enemy in enemysa: #Drawing monsters screen.blit(enemysaimg,(enemy[0],enemy[1])) return enemytimers,enemysa,enemyseveral#Final return def createfoods(food1,food1s,foodserverl): if foodserverl: print 'food' food1s.append((random.randint(10, 615),random.randint(10, 445))) foodserverl-=1 if foodserverl==0 and len(food1s)==0: foodserverl=random.randint(1, 5) for food in food1s: screen.blit(food1,food) return food1,food1s,foodserverl#Final return def eachcheck(myman,enemysa,food1s,myblood,score): #My collision with food foodn=0 for food in food1s: if myman[0]+30>food[0] and myman[0]<food[0]+25: if myman[1]+30>food[1] and myman[1]<food[1]+25: print 'MY AND FOOD' food1s.pop(foodn) if score>=0: score+=random.randint(1, 5)#Eat a small flower and add 1-5 points foodn+=1 for enemy in enemysa:#The collision between monsters and me if myman[0]+30>enemy[0] and myman[0]<enemy[0]+30: if myman[1]+30>enemy[1] and myman[1]<enemy[1]+30: print 'I AND ENEMY' if myblood>=0: myblood-=1#My Blood Button for food in food1s:#I got food. if myman[0]+30>food[0] and myman[0]<food[0]+25: if myman[1]+30>food[1] and myman[1]<food[1]+25: print 'I AND FOOD' for enemy in enemysa:#The Collision of Monsters with Food foodn=0 for food in food1s: if enemy[0]+30>food[0] and enemy[0]<food[0]+25: if enemy[1]+30>food[1] and enemy[1]<food[1]+25: print 'ENEMY AND FOOD' food1s.pop(foodn) myblood-=5 foodn+=1 return myman,enemysa,food1s,myblood,score def blitmyblood(myblood,myblood_surface,score): screen.blit(myblood_surface,[5,0]) screen.blit(myscore_surface,[450,0]) bloodbordor=pygame.draw.rect(screen,[0,0,0],[60,5,200,15],2) bloodvalue=pygame.draw.rect(screen,[255,255,0],[62,7,myblood,12]) myscoretts_surface=myscore_font.render('%s'%score,True,(0,0,255)) screen.blit(myscoretts_surface,[535,0]) second_surface=mysecond_font.render('%s'%gamesecond,True,(255,255,0)) screen.blit(second_surface,[0,440]) return myblood,myblood_surface,score def GameOver(myblood,gameoverwindows,myman): if myblood<=0: screen.blit(gameoverbg2,[0,0]) gameoverwindows=1 return gameoverwindows,myman def GameVictory(myblood,score,gamesecond): if myblood>0 and gamesecond<=0 and score>=100: screen.blit(victorybg,[0,0]) return myblood,score,gamesecond def gametimes(timeflag,gamesecond): if timeflag==20: if gamesecond>0: gamesecond-=1 timeflag=0 timeflag+=1 return timeflag,gamesecond while True:#Game subject screen.blit(bg,[0,0]) #Mapping mydirection=pygame_event(mydirection) #Key event overwall()#Judgment of Shipment Out of Interface if myblood>0 and gamesecond>0: screenblit()#Eating Drawing mybgflag,myman=blit_animation(mybgflag,myman)#Eating Animation enemytimers,enemysa,enemyseveral=createenemys(enemytimers,enemysa,enemyseveral)#Monster Drawing food1,food1s,foodserverl=createfoods(food1,food1s,foodserverl)#Produce food myblood,myblood_surface,score=blitmyblood(myblood,myblood_surface,score)#Drawing blood strip scores, etc. myman,enemysa,food1s,myblood,score=eachcheck(myman,enemysa,food1s,myblood,score)#collision detection gameoverwindows,myman=GameOver(myblood,gameoverwindows,myman)#Game over myblood,score,gamesecond=GameVictory(myblood,score,gamesecond)#victory timeflag,gamesecond=gametimes(timeflag,gamesecond) time.sleep(systemtime)#How many seconds is the delay? pygame.display.update()#Game Update if __name__ == "__main__": main()
Haha, let's finally see the result of the game compilation. interesting
Haha, it's not bad. The game is shared here. You need to tell the author about the source code. A hard-pressed author who specializes in developing small games