猫史档案馆


【Python作品分享】CRAZY SMILE【作品秀】

用户:余昕睿余昕睿查看:0 回复:0 评论:0 创建时间:2023-01-21T15:42:39


【作品展示】

center_image

 

【作品介绍】

#疯狂的笑脸

#弹它!!

#鼠标控制

 

【作品源代码】

import pygame

#初始化pygame

pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Smily Pong')
keepGoing = True
pic = pygame.image.load('E:\PYGAME\Smile.bmp')
#  如果路径问题,请切换路径^^^^^^^^^^^^^^^^^^^
colorkey = pic.get_at((0, 0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 30
paddley = 550
picw = 100
pich = 100
points = 0
lives = 5
font = pygame.font.SysFont('Times', 24)

#游戏循环

while keepGoing:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_z:
                points = 0
                lives = 5
                picx = 0
                picy = 0
                speedx = 5
                speedy = 5
    picx += speedx
    picy += speedy
    if picx <= 0 or picx >= 700:
        speedx = -speedx * 1.1
    if picy <= 0:
        speedy = -speedy + 1
    if picy >= 500:
        lives -= 1
        speedy = -5
        speedx = 5
        picy = 499
    #绘制笑脸
    screen.fill(BLACK)
    screen.blit(pic, (picx, picy))
    #绘制挡板
    paddlex = pygame.mouse.get_pos()[0]
    paddlex -= paddlew/2
    pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))

    #Check for paddle bounce 
    if picy + pich >= paddley and picy + pich <= paddley + paddleh and speedy > 0:
        if picx + picw/2 >= paddlex and picx + picw/2 <= paddlex + paddlew:
            speedy =- speedy
            points += 1
    #Draw text on screen 
    draw_string = " Lives :" + str(lives) + "Points :" + str(points)
    #Check whether the game is over 
    if lives < 1:
        speedx = speedy = 0
        draw_string = " Game Over  Your score was:"+ str(points)
        draw_string += "  Press Z to play again."
    text = font.render(draw_string, True, WHITE)
    text_rect = text.get_rect()
    text_rect.centerx = screen.get_rect().centerx
    text_rect.y = 10
    screen.blit(text, text_rect)
    pygame.display.update()
    timer.tick(60)

pygame.quit ()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页