Jul 252021
 

这节课的内容,主要是碰撞。还有就是统计分数。

  • 设定字体,显示分数
  • 更换图片,这块需要理解,不能更改blit的图片,而是设置一个变量来替换
import pygame, time, random 
from random import randint as rd
pygame.init()
pygame.key.set_repeat(50)

screen = pygame.display.set_mode([1024,640])
bg=pygame.image.load('res/c1.png')

hero = pygame.sprite.Sprite()
hero.image = pygame.image.load('res/hero.png')
hero.rect = pygame.Rect(0,200,70,90)
hero.money = 0

d = pygame.sprite.Sprite()
d.image = pygame.image.load('res/diamonds.png')
d.rect = pygame.Rect(500,250,50,50)


door_open = pygame.image.load('res/house_open.png')
door_close = pygame.image.load('res/house_close.png')
house = pygame.sprite.Sprite()
house.image = door_close
house.rect = pygame.Rect(700,250,80,80)

f = pygame.font.SysFont('Times New Roman',60)


def draw():        
        screen.blit(bg,(0,0))
        screen.blit(hero.image,hero.rect)
        screen.blit(d.image,d.rect)
        core = f.render('Core :'+str(hero.money),1,0)
        screen.blit(core,(0,0))
        screen.blit(house.image,house.rect)

        if hero.rect.colliderect(d.rect):
                d.rect.x+=1000
                hero.money+=50

        if hero.rect.colliderect(house.rect) and hero.money >= 50:                
                house.image=door_open


                
        
        pygame.display.flip()

def keydown(i):
    if i.key == pygame.K_UP:
        hero.rect.y-=10
    if i.key == pygame.K_DOWN:
        hero.rect.y+=10
    if i.key == pygame.K_RIGHT:
        hero.rect.x+=10
    if i.key == pygame.K_LEFT:
        hero.rect.x-=10

p=1
while p ==1:
    draw()
    
    e = pygame.event.get()
    for i in e:
        if i.type == pygame.QUIT:
            p = 0     
        if i.type == pygame.KEYDOWN:
            keydown(i)   
pygame.quit()

  One Response to “hero game 02”

  1. 技术文章,学习了。
    80% WordPress/Typecho,都是在讲程序开发

Leave a Reply to 马内 Cancel reply

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.