zoukankan      html  css  js  c++  java
  • 壁球小游戏详解(附有源码.cpp)

    1.在python中安装pygame
    2.将下列源码复制过去,运行。

    #引用
    import pygame, sys
    #初始化
    pygame.init()
    size = width, height = 600, 400
    speed = [1, 1]
    BLACK = 0, 0, 0
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption(“Pygame壁球”)
    ball = pygame.image.load(“PYG02-ball.gif”)
    ballrect = ball.get_rect()
    fps = 300
    fclock = pygame.time.Clock()
    #事件处理
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    sys.exit()
    elif event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
    speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)* int(speed[0]/abs(speed[0]))
    elif event.key == pygame.K_RIGHT:
    speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
    elif event.key == pygame.K_UP:
    speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
    elif event.key == pygame.K_DOWN:
    speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)* int(speed[1]/abs(speed[1]))
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right >
    speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
    speed[1] = - speed[1]
    #窗口刷新
    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    pygame.display.update()
    fclock.tick(fps)

    壁球小游戏
    ↑: 纵向绝对速度增加1个像素{纵向加速}
    ↓: 纵向 {纵向减速}
    ←:横向 {横向减速}
    →:横向 {横向加速}

    永远热泪盈眶。
  • 相关阅读:
    支持向量机SVM知识点概括
    决策树知识点概括
    HDU 3081 Marriage Match II
    HDU 3572 Task Schedule
    HDU 4888 Redraw Beautiful Drawings
    Poj 2728 Desert King
    HDU 3926 Hand in Hand
    HDU 1598 find the most comfortable road
    HDU 4393 Throw nails
    POJ 1486 Sorting Slides
  • 原文地址:https://www.cnblogs.com/2021WGF/p/14253241.html
Copyright © 2011-2022 走看看