猫史档案馆


Smiley Pong(乒乓球)

用户:可亲的独角蛛mXYt可亲的独角蛛mXYt查看:0 回复:0 评论:0 创建时间:2023-01-01T14:39:04


center_image保存这个图片,将他设为jpg文件并命名为喵iley(1),放到py文件所处的文件夹。

import pygame
pygame.init()
screen = pygame.display.set_mode([800,600])
keep = True
font = pygame.font.SysFont('Times',24)
pic = pygame.image.load('喵ile(1).jpg')
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
picw = 100
pich = 100
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
WHITE = (0,0,0)
BLACK = (255,255,255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
point = 0
lives = 5

while keep:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep = False
    picx += speedx
    picy += speedy

    if picx <= 0 or picx + pic.get_width() >= 800:
        speedx = -speedx
    if picy <= 0 :
        speedy = -speedy
    if picy >= 500:
        lives -= 1
        speedy = -speedy

    if picy + pich >= paddley and picy + pich <= paddley +paddleh and speedy > 0:
        if picx + picw/2 >= paddlex and picx + picw/2 <= paddlex + paddlew:
            point += 1
            speedy = -speedy

    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))

    draw_string = 'Lives:' + str(lives) + '  Points:' + str(point)
    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()

部分参考了教程书
移动鼠标即可移动托盘


回复

上一页1 页 / 共 0下一页