pygame study notes (6) : complete a simple game
- 2020-05-10 18:22:32
- OfStack
After learning Pygame for such a long time, 1 wanted to write a real game 1. What seems like an easy game to write is how hard it is. Originally want to write a tetris, think for a long time how to achieve, want to think, also did not write out, so simply download someone else's code to read. Later, I wanted to write a game to help me memorize the treasure box, but I didn't finish it. Only 1 has been completed, which is the following game of gold coin catching by the little man. It is super simple, controlling the little man's movement to catch the gold coin from the air through the left and right keys. If you catch the gold coin, you will get 5 points. After finishing this code, I still feel that this code is poorly written. Indeed, I have only scratched the surface of pygame, but I still don't have a thorough understanding of surface and sprite. Here to write out the code, have time to help the cattle can point 1, let me also improve.
# -*- coding: cp936 -*-
'''
1 Super simple game
Around the key control SIMS move to catch the air down the gold, gold to catch 5 Points, can not catch the end of the game, gold coin speed will follow level The number of customs
And faster and faster
'''
import pygame,sys,os,random
pygame.init()
class rect():# Draw a dog
def __init__(self,filename,initial_position):
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
class goldrect(pygame.sprite.Sprite):# Draw gold COINS
def __init__(self,gold_position,speed):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load('image\\gold.png')
self.rect=self.image.get_rect()
self.rect.topleft=gold_position
self.speed=speed
def move(self):
self.rect=self.rect.move(self.speed)
def drawback(): # Draw the background picture
my_back=pygame.image.load('image\\qi3.jpg')
bakscreen.blit(my_back,[0,0])
def loadtext(levelnum,score,highscore):# Draw grades, level , highest grade
my_font=pygame.font.SysFont(None,24)
levelstr='Level:'+str(levelnum)
text_screen=my_font.render(levelstr, True, (255, 0, 0))
bakscreen.blit(text_screen, (650,50))
highscorestr='Higescore:'+str(highscore)
text_screen=my_font.render(highscorestr, True, (255, 0, 0))
bakscreen.blit(text_screen, (650,80))
scorestr='Score:'+str(score)
text_screen=my_font.render(scorestr, True, (255, 0, 0))
bakscreen.blit(text_screen, (650,110))
def loadgameover(scorenum,highscore):# draw GAME OVER
my_font=pygame.font.SysFont(None,50)
levelstr='GAME OVER'
over_screen=my_font.render(levelstr, True, (255, 0, 0))
bakscreen.blit(over_screen, (300,240))
highscorestr='YOUR SCORE IS '+str(scorenum)
over_screen=my_font.render(highscorestr, True, (255, 0, 0))
bakscreen.blit(over_screen, (280,290))
if scorenum>int(highscore):# Write maximum score
highscorestr='YOUR HAVE GOT THE HIGHEST SCORE!'
text_screen=my_font.render(highscorestr, True, (255, 0, 0))
bakscreen.blit(text_screen, (100,340))
highfile=open('highscore','w')
highfile.writelines(str(scorenum))
highfile.close()
def gethighscore(): # Read maximum score
if os.path.isfile('highscore'):
highfile=open('highscore','r')
highscore=highfile.readline()
highfile.close()
else:
highscore=0
return highscore
bakscreen=pygame.display.set_mode([800,600])
bakscreen.fill([0,160,233])
pygame.display.set_caption('Dig!Dig!')
drawback()
levelnum=1 #level
scorenum=0 # score
highscore=gethighscore()# The highest
ileft=1 # Record the number of left moves to control the image
iright=10 # Record the number of steps to the right to control the image
x=100
y=450
filename='image\\1.png'
backimg_ren=rect(filename,[x,y])
bakscreen.blit(backimg_ren.image,backimg_ren.rect)
loadtext(levelnum,scorenum,highscore)
goldx=random.randint(50,580)
speed=[0,levelnum]
mygold=goldrect([goldx,100],speed)
pygame.display.update()
while True:
if scorenum>0 and scorenum/50.0==int(scorenum/50.0):# When the score was 50 Is modified when multiple of level
levelnum=scorenum/50+1
speed=[0,levelnum]
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
#make gold
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_LEFT]:# Press the left key
drawback()
loadtext(levelnum,scorenum,highscore)
if iright > 14 :iright=10
iright=iright+1
filename='image\\'+str(iright)+'.png'
if x<50 :
x=50
else:
x=x-10
backimg_surface=rect(filename,[x,y])
bakscreen.blit(backimg_surface.image,backimg_surface.rect)
if pressed_keys[pygame.K_RIGHT]:# Press the right
drawback()
loadtext(levelnum,scorenum,highscore)
if ileft > 4 :ileft=0
ileft=ileft+1
filename='image\\'+str(ileft)+'.png'
if x>560:
x=560
else:
x=x+10
backimg_surface=rect(filename,[x,y])
bakscreen.blit(backimg_surface.image,backimg_surface.rect)
drawback()
loadtext(levelnum,scorenum,highscore)
mygold.move()
bakscreen.blit(mygold.image,mygold.rect)
backimg_surface=rect(filename,[x,y])
bakscreen.blit(backimg_surface.image,backimg_surface.rect)
if mygold.rect.top>600:# To see if the gold coin touched the ground, 1 But land, game over
loadgameover(scorenum,highscore)
if mygold.rect.colliderect(backimg_surface.rect):# Judge whether the gold coin collides with the little man. If the collision means that the little man catches the gold coin
scorenum+=5
loadtext(levelnum,scorenum,highscore)
goldx=random.randint(50,580)
mygold=goldrect([goldx,100],speed)
pygame.display.update()
Program used in resources can be downloaded from here: file name: gold. 7 z, access address: http: / / www kuaipan. cn/file/id_16699292408348719 htm