在这里,您可以查看我们到目前为止编写的Pygame RPG系列的完整代码。
这次代码审查背后的主要原因是,到目前为止,我们一直在讨论小片段的代码(由于绝对的大小)。对于那些在将这些片段连接成一个整体时遇到麻烦的人,您可以使用下面的代码作为参考。
Pygame RPG Code
下面的代码涵盖了以下教程(1 - 6):
import pygame
from pygame.locals import *
import sys
import random
import time
from tkinter import filedialog
from tkinter import *
pygame.init() # Begin pygame
# Declaring variables to be used through the program
vec = pygame.math.Vector2
HEIGHT = 350
WIDTH = 700
ACC = 0.3
FRIC = -0.10
FPS = 60
FPS\_CLOCK = pygame.time.Clock()
COUNT = 0
# Create the display
displaysurface = pygame.display.set\_mode((WIDTH, HEIGHT))
pygame.display.set\_caption("Game")
# Run animation for the RIGHT
run\_ani\_R = [pygame.image.load("Player\_Sprite\_R.png"), pygame.image.load("Player\_Sprite2\_R.png"),
pygame.image.load("Player\_Sprite3\_R.png"),pygame.image.load("Player\_Sprite4\_R.png"),
pygame.image.load("Player\_Sprite5\_R.png"),pygame.image.load("Player\_Sprite6\_R.png"),
pygame.image.load("Player\_Sprite\_R.png")]
# Run animation for the LEFT
run\_ani\_L = [pygame.image.load("Player\_Sprite\_L.png"), pygame.image.load("Player\_Sprite2\_L.png"),
pygame.image.load("Player\_Sprite3\_L.png"),pygame.image.load("Player\_Sprite4\_L.png"),
pygame.image.load("Player\_Sprite5\_L.png"),pygame.image.load("Player\_Sprite6\_L.png"),
pygame.image.load("Player\_Sprite\_L.png")]
class Background(pygame.sprite.Sprite):
def \_\_init\_\_(self):
super().\_\_init\_\_()
self.bgimage = pygame.image.load("Background.png")
self.rectBGimg = self.bgimage.get\_rect()
self.bgY = 0
self.bgX = 0
def render(self):
displaysurface.blit(self.bgimage, (self.bgX, self.bgY))
class Ground(pygame.sprite.Sprite):
def \_\_init\_\_(self):
super().\_\_init\_\_()
self.image = pygame.image.load("Ground.png")
self.rect = self.image.get\_rect(center = (350, 350))
self.bgX1 = 0
self.bgY1 = 285
def render(self):
displaysurface.blit(self.image, (self.bgX1, self.bgY1))
class Player(pygame.sprite.Sprite):
def \_\_init\_\_(self):
super().\_\_init\_\_()
self.image = pygame.image.load("Player\_Sprite\_R.png")
self.rect = self.image.get\_rect()
# Position and direction
self.vx = 0
self.pos = vec((340, 240))
self.vel = vec(0,0)
self.acc = vec(0,0)
self.direction = "RIGHT"
# Movement
self.jumping = False
self.running = False
self.move\_frame = 0
def move(self):
# Keep a constant acceleration of 0.5 in the downwards direction (gravity)
self.acc = vec(0,0.5)
# Will set running to False if the player has slowed down to a certain extent
if abs(self.vel.x) > 0.3:
self.running = True
else:
self.running = False
# Returns the current key presses
pressed\_keys = pygame.key.get\_pressed()
# Accelerates the player in the direction of the key press
if pressed\_keys[K\_LEFT]:
self.acc.x = -ACC
if pressed\_keys[K\_RIGHT]:
self.acc.x = ACC
# Formulas to calculate velocity while accounting for friction
self.acc.x += self.vel.x * FRIC
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc # Updates Position with new values
# This causes character warping from one point of the screen to the other
if self.pos.x > WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = WIDTH
self.rect.midbottom = self.pos # Update rect with new pos
def gravity\_check(self):
hits = pygame.sprite.spritecollide(player ,ground\_group, False)
if self.vel.y > 0:
if hits:
lowest = hits[0]
if self.pos.y < lowest.rect.bottom:
self.pos.y = lowest.rect.top + 1
self.vel.y = 0
self.jumping = False
def update(self):
# Return to base frame if at end of movement sequence
if self.move\_frame > 6:
self.move\_frame = 0
return
# Move the character to the next frame if conditions are met
if self.jumping == Falseand self.running == True:
if self.vel.x > 0:
self.image = run\_ani\_R[self.move\_frame]
self.direction = "RIGHT"
else:
self.image = run\_ani\_L[self.move\_frame]
self.direction = "LEFT"
self.move\_frame += 1
# Returns to base frame if standing still and incorrect frame is showing
if abs(self.vel.x) < 0.2and self.move\_frame != 0:
self.move\_frame = 0
if self.direction == "RIGHT":
self.image = run\_ani\_R[self.move\_frame]
elif self.direction == "LEFT":
self.image = run\_ani\_L[self.move\_frame]
def attack(self):
pass
def jump(self):
self.rect.x += 1
# Check to see if payer is in contact with the ground
hits = pygame.sprite.spritecollide(self, ground\_group, False)
self.rect.x -= 1
# If touching the ground, and not currently jumping, cause the player to jump.
if hits andnot self.jumping:
self.jumping = True
self.vel.y = -12
class Enemy(pygame.sprite.Sprite):
def \_\_init\_\_(self):
super().\_\_init\_\_()
player = Player()
Playergroup = pygame.sprite.Group()
background = Background()
ground = Ground()
ground\_group = pygame.sprite.Group()
ground\_group.add(ground)
whileTrue:
player.gravity\_check()
for event in pygame.event.get():
# Will run when the close window button is clicked
if event.type == QUIT:
pygame.quit()
sys.exit()
# For events that occur upon clicking the mouse (left click)
if event.type == pygame.MOUSEBUTTONDOWN:
pass
# Event handling for a range of different key presses
if event.type == pygame.KEYDOWN:
if event.key == pygame.K\_SPACE:
player.jump()
# Player related functions
player.update()
player.move()
# Display and Background related functions
background.render()
ground.render()
# Rendering Player
displaysurface.blit(player.image, player.rect)
pygame.display.update()
FPS\_CLOCK.tick(FPS)
https://github.com/zh794390558/Games/blob/master/games\_code/RPG/lession6/Code\_review\_1.zip
点个「赞」+「在看」❤️
让我们知道这份文字有温暖到你,也是 我们持续 创作的最大动力!
推荐
语音合成(TTS)跳跃与重复问题的解析:成因、机制及解决方案
大模型训练新思路:GEPA 靠 “反思” 赢过 RL,看完秒懂
F5-TTS:用 Flow Matching 玩转语音,流畅度和真实感都 “拉满” 了
E2 TTS:令人尴尬地简单、完全非自回归、零样本的语音合成技术
为什么都在聊 Kimi K2?Open Agentic Intelligence 藏着哪些新惊喜
OPENCSG 中文语料库:一系列高质量的中文数据集,用于语言模型训练
不要对 2+3=?想太多:关于类 o1 大语言模型的过度思考
Conditional Flow Matching : 连续标准流 Continuous Normalizing Flow
Conditional Flow Matching : 常微分方程ODE、欧拉方法和Neural ODE
当 Normalizing flow 遇上语音生成:AI 说话变 “真人” 的秘密在这里!
深度剖析:Kimi - Audio 中 BigVGAN 的神奇作用
为什么说分布变换是 Normalizing flow 的「灵魂操作」?
MiniMax-Speech,零样本语音合成新突破,32 种语言轻松拿捏!
手把手教你创建 evol-instruct 数据集!附完整流程~
最新!SpeechLLM 综述:架构、能力、挑战与未来全揭秘
从数量到质量:通过自引导数据选择来提升语言模型性能以实现指令调优
GeForce RTX 3090, 4090, A10, A40, A100, A800, L20, L40 显卡性能对比
基础模型中的新范式:为什么o1是不同的,以及它将如何改变LLM应用
从数量到质量:通过自引导数据选择来提升语言模型性能以实现指令调优
Fully Sharded Data Parallelism (FSDP)
CosyVoice 2:基于大型语言模型的可扩展流式语音合成技术
Mini-Omni2: with Vision, Speech and Duplex Capabilities
亲测有效!如何用 Address Sanitizer 精准定位内存漏洞?附保姆级操作指南
要用 AI 裁员 50% 的千亿独角兽,公开认错,重启招聘!
single codebook和dual codebook在LLM中向量量化上有什么区别?
亲测有效!如何用 Address Sanitizer 精准定位内存漏洞?附保姆级操作指南
CosyVoice:一种基于监督式语义标记的可扩展多语言 Zero-Shot 语音合成器
近日还在想要不要建个群呢?感觉自己是个i人,又懒,打理不来呀。但这个想法不自主的就冒出来了,还是要思考下。天人交战良久,得,一位朋友私我要入群,那就建一个吧,感谢。
欢迎入群,希望能有一个交流的地方。但群主是个i人,没事儿让他想静静,有事儿圈他。
群主不是万能的,不是万能的,不是能的,能的。