猫史档案馆


【Python作品分享】pingpong(3)【作品秀】

用户:苦力怕本尊苦力怕本尊查看:0 回复:0 评论:0 创建时间:2019-07-02T10:20:53


【作品展示】

center_image

 

【作品介绍】

ennnnjnn

;apasdfj;laskdj

 

【作品源代码】

import pygame
import sys
import random
import time
pygame.init()
size = width, height = 400, 600
screen = pygame.display.set_mode(size)
WHITE = (255, 255, 255)
BLACK = (0, 255, 255)
GREENBLUE = (0, 255, 25)
ball = pygame.image.load('2B.png')
timer = pygame.time.Clock()
init_x = random.randint(0, size[0]-ball.get_width())
rect_init_x = random.randint(0, size[0]-80)
rect1_init_x = random.randint(0, size[0]-80)
ball_speed = [2, 2]
ball_x = init_x
ball_y = 50
rect_x = rect_init_x
rect1_x = rect1_init_x
rect_y = size[1]-20
rect_speed = 15
喵 = True
score = 0
score1 = 0
font = pygame.font.SysFont("Arial", 30)
while True:
    screen.fill(BLACK)
    key_pressed = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if(rect_x >= 0):
                    rect_x -= rect_speed
            if event.key == pygame.K_RIGHT:
                if rect_x <= size[0]-80:
                    rect_x += rect_speed
            if event.key == pygame.K_a:
                if(rect1_x >= 0):
                    rect1_x -= rect_speed
            if event.key == pygame.K_d:
                if rect1_x <= size[0]-80:
                    rect1_x += rect_speed
    if 喵:
        if ball_x < 0 or ball_x > size[0]-ball.get_width():
            ball_speed[0] = -ball_speed[0]
        if ball_y < 20 or ball_y > size[1]-ball.get_height()-20:
            ball_speed[1] = -ball_speed[1]
        ball_x += ball_speed[0]
        ball_y += ball_speed[1]
        screen.blit(ball, (ball_x, ball_y))
    else:
        final_text1 = "Game Over"
        ft1_surf = font.render(final_text1, True, (255, 255, 255))
        screen.blit(ft1_surf, [120, 300])
    rect = pygame.Rect(rect_x, size[1]-20, 80, 20)
    pygame.draw.rect(screen, GREENBLUE, rect, 0)
    rect1 = pygame.Rect(rect1_x, 0, 80, 20)
    pygame.draw.rect(screen, GREENBLUE, rect1, 0)
    if ball_y >= size[1]-20-ball.get_height() and (ball_x+float(ball.get_width())/2 > rect_x+80 or
                                                   ball_x+float(ball.get_width())/2 < rect_x):
        喵 = False
    if ball_y <= 20 and (ball_x+float(ball.get_width())/2 > rect1_x+80 or
                         ball_x+float(ball.get_width())/2 < rect1_x):
        喵 = False
    if ball_y == size[1]-20-ball.get_height() and 喵:
        score += 1
    if ball_y == 20 and 喵:
        score1 += 1
    score_text = "Score: " + str(int(score/2))
    ft2_surf = font.render(score_text, True, (252, 3, 36))
    screen.blit(ft2_surf, [40, 0])
    score1_text = "Score1: " + str(int(score1 / 2))
    ft2_surf1 = font.render(score1_text, True, (252, 3, 36))
    screen.blit(ft2_surf1, [240, 0])
    pygame.display.update()
    timer.tick(60)

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页