Alex-接小球游戏

更多内容可以联系少儿编程侯老师,微信data_ecology
本文链接: https://blog.csdn.net/houlaos/article/details/102594512
import pygame
from pygame.locals import *
import sys

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (0,255,255)

Ball_x = 0
Ball_y = 0
Ball_color = (255,0,0)
Ball_RADIUS = 45
Ball_speed_x = 100
Ball_speed_y = 100

BANG_color = (0,0,0)
BANG_W = 300
BANG_H = 75

BANG_X = SCREEN_WIDTH/2-BANG_W/2
BANG_y = SCREEN_HEIGHT-BANG_H
BANG_speed_x=1

score =0

lives = 300000

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('ball games')
gamefont = pygame.font.Font("ziti.ttf",30)

while True:
    if lives == 0:
        break

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_SPACE]:
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == QUIT:
                    pygame.quit()
                    sys.exit()
            imgMenu = gamefont.render("回到游戏模式请按 Esc" ,True,(255,255,255))
            screen.blit(imgMenu,(SCREEN_WIDTH/2,SCREEN_HEIGHT/2))
            pygame.display.update()

            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_SPACE]:
                break

    events = pygame.event.get()
    for e in events:
        if e.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.fill(BG_COLOR)
    Ball_x += Ball_speed_x
    Ball_y += Ball_speed_y
    if Ball_x >SCREEN_WIDTH:
        Ball_speed_x = -1
    if Ball_x < 0:
        Ball_speed_x = 1
    if Ball_y > SCREEN_HEIGHT:
        Ball_x = 0
        Ball_y = 0
        lives = lives - 1

    if Ball_y < 0:
        Ball_speed_y = 1

    pygame.draw.circle(screen,Ball_color,(Ball_x,Ball_y),Ball_RADIUS)

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_LEFT]:
        BANG_X -=BANG_speed_x
    if key_pressed[K_RIGHT]:
        BANG_X += BANG_speed_x
    if BANG_X >SCREEN_WIDTH-BANG_W:
        BANG_X = SCREEN_WIDTH - BANG_W
    if BANG_X < 0:
        BANG_X = 0

    if BANG_X < Ball_x and BANG_X<(BANG_X+BANG_W) and (BANG_y - Ball_y) <5:
        BANG_speed_y = -1
        score += 1

    pygame.draw.rect(screen,BANG_color,(BANG_X,BANG_y,BANG_W,BANG_H))
    imlives = gamefont.render('命数{}'.format(lives),True,(255,0,0))

    imgScore = gamefont.render('得分{}'.format(score),True,(255,0,0))
    screen.blit(imgScore,(SCREEN_WIDTH-imgScore.get_width(),0))
    screen.blit(imlives, (0, 0))
    pygame.display.update()




猜你喜欢

转载自blog.csdn.net/houlaos/article/details/102594512