python写个小游戏之跳跳球

阅读: 评论:0

python写个小游戏之跳跳球

python写个小游戏之跳跳球

1、功能介绍:

空格键:起跳

键盘:方向键左右可以移动

在线安装命令是:

pip install  *****

pip更新命令如下:

< -m pip install --upgrade pip

源码如下,可以直接复制,使用,需要有第三方库pygame、sys。

import pygame
import sys

# 初始化
pygame.init()

# 屏幕设置
screen_width = 400
screen_height = 300
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Jumping Ball Game")

# 颜色设置
black = (0, 0, 0)
ball_color = (0, 128, 255)

class Ball:
def __init__(self, x, y, radius, gravity):
self.x = x
self.y = y
self.radius = radius
self.dx = 0
self.dy = 0
avity = gravity

def jump(self):
if self.y == screen_height - self.radius:
self.dy = -15

def move_left(self):
self.dx = -5

def move_right(self):
self.dx = 5

def stop(self):
self.dx = 0

def update(self):
self.dy += avity
self.y += self.dy
self.x += self.dx

if self.y >= screen_height - self.radius:
self.y = screen_height - self.radius
self.dy = 0

if self.x <= 0:
self.x = 0
elif self.x >= screen_width:
self.x = screen_width

def draw(self):
pygame.draw.circle(screen, ball_color, (int(self.x), int(self.y)), self.radius)

# 创建小球
ball = Ball(screen_width // 2, screen_height - 20, 20, 1)

# 游戏循环
clock = pygame.time.Clock()
running = True
while running:
for event in ():
pe == pygame.QUIT:
running = False

pe == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
ball.jump()
if event.key == pygame.K_LEFT:
ve_left()
if event.key == pygame.K_RIGHT:
ve_right()

pe == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
ball.stop()

# 更新和绘制
ball.update()

screen.fill(black)
ball.draw()
pygame.display.flip()

clock.tick(30)

# 退出游戏
pygame.quit()
it()

本文发布于:2024-02-01 12:27:01,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170676162536587.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:小游戏   python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23